Docker becomes much easier once you separate an image—the immutable template—from a container—the running or stopped instance created from that image. This lab follows one Nginx container through its complete lifecycle without deleting unrelated resources.
1. Inspect the Docker host
docker version
docker info --format 'Server={{.ServerVersion}} Driver={{.Driver}} Containers={{.Containers}}'
docker system df
2. Pull and inspect a pinned image
docker pull nginx:1.28-alpine
docker image ls nginx
docker image inspect nginx:1.28-alpine --format '{{json .RepoDigests}}'
docker history nginx:1.28-alpine
Pinning a version is more reproducible than relying on latest. For critical deployments, pin an immutable digest recorded by RepoDigests.
3. Create and run a container
docker run -d
--name web-demo
--restart unless-stopped
-p 127.0.0.1:8080:80
nginx:1.28-alpine
Binding to 127.0.0.1 keeps the demo off the public network. Verify it:
docker ps
curl -I http://127.0.0.1:8080
docker port web-demo
4. Read logs and inspect runtime state
docker logs --tail 50 web-demo
docker logs -f --since 5m web-demo
docker inspect web-demo --format 'Status={{.State.Status}} PID={{.State.Pid}} IP={{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
docker stats --no-stream web-demo
docker top web-demo
Exit the follow mode with Ctrl+C; it does not stop the container.
5. Execute a diagnostic command
docker exec web-demo nginx -t
docker exec web-demo cat /etc/nginx/conf.d/default.conf
docker exec -it web-demo sh
Type exit to leave the shell. Changes made inside the writable container layer disappear when the container is replaced, so configuration belongs in an image or mount.
6. Stop, start, and recreate
docker stop --time 10 web-demo
docker ps -a --filter name=web-demo
docker start web-demo
curl -I http://127.0.0.1:8080
To replace the instance, remove it and run the same declarative command again:
docker rm -f web-demo
docker run -d --name web-demo -p 127.0.0.1:8080:80 nginx:1.28-alpine
7. Targeted cleanup
docker rm -f web-demo
docker image rm nginx:1.28-alpine
docker container prune --filter 'until=24h'
docker image prune
docker system df
Review prune prompts carefully. Avoid docker system prune --volumes on a host with data you have not backed up.
Command map
docker ps: running containers.docker ps -a: all containers.docker logs: stdout/stderr.docker exec: run a process in an existing container.docker inspect: low-level JSON state.docker rm: remove containers;docker image rm: remove images.
References: Docker documentation and the Server World topic index.