From Image to Running Container
A Docker image is a read-only template; a container is a running (or stopped) instance of that image with its own writable layer, process namespace, and network stack. Managing containers day-to-day means creating them, observing their state, interacting with them, and cleaning them up.
Cricket analogy: Like a coaching manual (image) being a fixed reference while an actual training session (container) run from it can be paused, adjusted, or ended, a Docker image is a read-only template and a container is its running instance.
Starting a Container with docker run
The docker run command creates and starts a container in one step. Common flags control detachment, naming, port mapping, and automatic removal.
Cricket analogy: Like a captain calling 'play' to send the whole XI onto the field in one command rather than walking each player out individually, docker run creates and starts a container in a single step with flags for naming and ports.
# Run an nginx web server in the background, name it, map host port 8080 to container port 80
docker run -d --name web -p 8080:80 nginx:1.27
# Run an interactive shell in a temporary container that is removed on exit
docker run -it --rm ubuntu:22.04 bash
# Run a container with a restart policy
docker run -d --name api --restart unless-stopped myapp:1.0-d runs the container detached (in the background); without it, the container's process output streams to your terminal and Ctrl+C stops the container.
Listing and Inspecting Containers
docker ps shows running containers; add -a to include stopped ones. docker inspect returns detailed JSON metadata such as IP address, mounts, and environment variables.
Cricket analogy: Like checking the live scoreboard for teams currently batting versus pulling the full match archive including completed games, docker ps shows running containers, and -a includes stopped ones; docker inspect gives detailed match metadata.
docker ps # running containers only
docker ps -a # all containers, including exited
docker inspect web # full JSON details for container 'web'
docker inspect -f '{{.State.Status}}' web # just the status fieldViewing Logs and Executing Commands
docker logs retrieves stdout/stderr from a container's main process, which is essential for debugging without attaching a terminal. docker exec runs an additional command inside an already-running container, commonly used to open a debug shell.
Cricket analogy: Like reviewing a player's ball-by-ball commentary feed to debug a poor performance without interviewing them live, docker logs retrieves stdout/stderr, while docker exec lets you step into the dressing room mid-match to talk to them directly.
docker logs web # print all logs
docker logs -f --tail 100 web # follow the last 100 lines live
docker exec -it web sh # open an interactive shell inside the running container
docker exec web cat /etc/nginx/nginx.confStopping, Restarting, and Removing Containers
docker stop sends SIGTERM (then SIGKILL after a grace period) to gracefully shut down a container's main process. docker rm deletes the container's writable layer and metadata; it must be stopped first unless you use -f.
Cricket analogy: Like an umpire signaling a batter off the field with a warning before forcibly removing them if they linger (SIGTERM then SIGKILL), docker stop gracefully shuts a container down, and docker rm clears their name from the scorebook, requiring they've left first.
docker stop web # graceful stop (default 10s grace period)
docker stop -t 30 web # give it 30 seconds before SIGKILL
docker start web # restart a stopped container
docker restart web # stop then start
docker rm web # remove a stopped container
docker rm -f web # force-stop and remove in one step
docker container prune # remove ALL stopped containersdocker rm -f and docker container prune are destructive and cannot be undone — any data in the container's writable layer (not stored in a volume) is lost permanently.
docker run= create + start;-ddetaches,--namelabels,-p host:containermaps ports,--rmauto-cleans on exitdocker ps -alists all containers including stopped ones;docker psalone shows only running onesdocker stopsends SIGTERM first, then SIGKILL after the grace period (default 10s, configurable with-t)docker exec -it <container> shopens a shell inside an already-running container; it does not start a new containerdocker logs -ffollows log output live, similar totail -f- Container data in the writable layer is ephemeral and lost on
docker rmunless backed by a volume
Practice what you learned
1. Which command creates AND starts a new container from an image?
2. What does the -d flag do in `docker run -d nginx`?
3. Which command lists containers that have already stopped, in addition to running ones?
4. What signal does `docker stop` send to a container's main process first?
5. How do you get an interactive shell inside an already-running container named 'web'?
Was this page helpful?
You May Also Like
Docker Images Explained
An introduction to what Docker images are, how they differ from containers, and how their layered, read-only filesystem structure works.
Container Networking Basics
Understand Docker's default bridge network, port publishing, and how containers discover and communicate with each other on user-defined networks.
Health Checks and Restart Policies
How Docker uses HEALTHCHECK instructions and restart policies to detect unhealthy containers and automatically recover from failures.
Installing and Using the Docker CLI
A practical guide to installing Docker and using its core CLI commands for pulling images, running containers, and inspecting Docker's state.
Related Reading
Related Study Notes in DevOps
Browse all study notesNginx Study Notes
DevOps · 30 topics
DevOpsAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsCI/CD Tools & Pipelines Study Notes
YAML · 37 topics