Loading...

Lab 156: Docker Basics

Validate Docker service health, pull a known image, run a test web container with port mapping, verify it responds, inspect logs, exec into the container for inspection, then cleanly remove the container and image.

containers services troubleshooting

Scenario

Ops ticket: “Validate Docker is running, launch a test web container, verify it works, inspect logs, exec into it, then clean up all resources.” You will use a known image (nginx:latest) to keep the workflow reproducible.

Operator context

Treat this like a production smoke test. Prove daemon health first, then prove client-to-daemon connectivity, then prove the workload is reachable and observable before cleanup.

Objective

  • Verify Docker service is active using systemctl is-active.
  • Confirm the Docker CLI can talk to the daemon using docker version.
  • Pull nginx:latest to ensure the image is present.
  • Run a detached container named web1 and publish 8080:80.
  • Validate the container is running and the service responds on http://localhost:8080.
  • Inspect recent logs and exec into the container for basic filesystem inspection.
  • Remove the container and the image to leave the host clean.

Concepts

  • Docker is a client-server model: the CLI talks to the daemon (engine).
  • Images are immutable templates; containers are runtime instances created from images.
  • Port publishing (-p host:container) exposes a container service to the host network namespace.
  • Logs and exec are your first-line observability tools for container triage.
  • Cleanup is part of ops quality: remove containers and images to prevent drift and disk bloat.

Walkthrough

Step 1 : Verify Docker service health.
Command
systemctl is-active docker

This confirms the daemon is running. If this returns anything other than active, fix service health before attempting container operations.

active
Step 2 : Confirm the Docker CLI can talk to the daemon.
Command
docker version

This provides a quick client/server connectivity check. It also gives you version evidence that helps when debugging API mismatches or expected feature behavior.

# Example output (condensed):
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
Step 3 : Pull a known image for reproducibility.
Command
docker pull nginx:latest

Pulling explicitly avoids “it worked on my box” issues and provides evidence that the image exists locally. It also proves network access to the configured registry.

# Example output (condensed):
latest: Pulling from library/nginx
Digest: sha256:...
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
Step 4 : Run a detached container with a name and port mapping.
Command
docker run -d --name web1 -p 8080:80 nginx:latest

-d runs the container in the background, --name gives a stable handle for logs and exec, and -p 8080:80 publishes container port 80 on host port 8080.

# Container ID returned:
9f2c3b0a1d2e3f4a5b6c7d8e9f00112233445566778899aabbccddeeff0011
Step 5 : Verify the container is running.
Command
docker ps

This is the basic ops check. Confirm name, status, and that the port mapping exists as expected.

CONTAINER ID   IMAGE          COMMAND                  STATUS          PORTS                  NAMES
9f2c3b0a1d2e   nginx:latest   "/docker-entrypoint…"   Up 5 seconds    0.0.0.0:8080->80/tcp   web1
Step 6 : Validate the service responds.
Command
curl -I http://localhost:8080

A successful HTTP response proves the container is reachable through the published port and the process inside is serving traffic.

HTTP/1.1 200 OK
Server: nginx/1.25.x
Content-Type: text/html
Step 7 : Inspect recent logs for quick triage.
Command
docker logs --tail 5 web1

Logs confirm the request hit the container. This is often enough to distinguish “network path problem” from “service inside container problem.”

127.0.0.1 - - [10/Feb/2026:23:00:00 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.88.1"
Step 8 : Exec into the container and inspect nginx config.
Command
docker exec -it web1 ls -l /etc/nginx

docker exec runs a command inside the container context. This is the fastest way to validate filesystem and config state without building a new image.

total 12
-rw-r--r-- 1 root root 1007 Feb 10 22:59 nginx.conf
drwxr-xr-x 2 root root 4096 Feb 10 22:59 conf.d
drwxr-xr-x 2 root root 4096 Feb 10 22:59 modules
Step 9 : Stop and remove the container in one command.
Command
docker rm -f web1

This is the cleanup step for runtime resources. -f stops the container if needed, then removes it.

web1
Step 10 : Remove the image for disk hygiene.
Command
docker rmi nginx:latest

Removing the image prevents disk bloat on training hosts and resets the lab back to a clean baseline.

Untagged: nginx:latest
Deleted: sha256:...

Common breakpoints

systemctl is-active returns inactive or failed

Fix service health first. Check systemctl status docker and logs before assuming the CLI is the problem.

docker version: “Cannot connect to the Docker daemon”

The daemon is not running, socket permissions are wrong, or you are targeting the wrong context. Confirm service state and ensure you have the required privileges.

curl fails to connect on localhost:8080

Confirm container is running and the port mapping exists via docker ps. Also confirm no host firewall rules or port conflicts are blocking 8080.

docker rmi fails due to “image is being used”

One or more containers still reference the image. Remove containers first, then retry image removal.

Cleanup checklist

This lab includes cleanup as part of the workflow. Confirm the container is removed and the image no longer exists locally.

Commands
docker ps
docker images | grep nginx || true
Success signal

web1 is gone from running containers and nginx:latest is not present in local images.

Reference

  • systemctl is-active docker : Check Docker service state.
  • docker version : Confirm client and daemon connectivity and versions.
  • docker pull <image:tag> : Download an image to the local host.
  • docker run -d --name <name> -p <host>:<container> <image> : Run a container in detached mode with a name and port mapping.
  • docker ps : List running containers.
  • docker logs --tail <n> <container> : Show the last N log lines for a container.
  • docker exec -it <container> <cmd> : Run a command inside a running container.
  • docker rm -f <container> : Stop and remove a container.
    • -f : Force stop then remove.
  • docker rmi <image:tag> : Remove a local image.
  • curl -I <url> : Fetch HTTP headers to validate service response.