Running containers
The flags on docker run decide how you interact with a container and how it cleans up after itself.
? What do the diagram colours mean
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
Three ways to start
Pick the mode by what you want your terminal to do.
Configuration at run time
Inject settings with -e so one image serves every environment.
Env files, Dockerfile defaults, ARG versus ENV and secrets are covered in chapter 6.
States and cleanup
run versus exec
| Flag | Use it for |
|---|---|
-d | servers: run in the background, read with docker logs name |
-it | a shell to look around: docker run -it --rm alpine sh |
--rm | throwaway containers: migrations, tests, experiments |
-e KEY=value | configuration that differs per environment |
--name | a stable name to use with logs, exec and stop |
Logs, exec, inspect and stats
When a container misbehaves, four read-only commands answer almost every first question.
| Command | Answers |
|---|---|
docker logs -f --tail 50 api | what is it printing right now? -f follows, --tail skips old lines |
docker exec -it api sh | what does it see? files, env, DNS, from the inside |
docker inspect -f '{{.State.Status}} {{.State.ExitCode}}' api | is it running, and why did it stop? |
docker stats --no-stream | how much CPU and memory is each container using? |
docker top api | which processes run inside it? |
Restart policies
A long-running service should come back after a crash or a reboot. That is a flag on docker run, not a script.
| Policy | Restarts when | Good for |
|---|---|---|
no (default) | never | one-off jobs |
on-failure[:5] | the process exits non-zero, at most 5 times | workers that may crash |
always | it stops for any reason, and when the daemon starts | services that must run |
unless-stopped | like always, but not after you docker stop it | most long-running services |
--restart cannot be combined with --rm: one says “keep it”, the other says “delete it”.
Try it yourself
-
Step 1 · An interactive shell
-itattaches your keyboard.exitstops the container, and--rmdeletes it.$ docker run -it --rm --name shell alpine:3.20 sh / # ls / bin etc lib root tmp / # exit $ docker ps -a --filter name=shell CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -
Step 2 · Detached, named, with an environment variable
$ docker run -d --name api --rm -e PORT=3000 alpine:3.20 sleep 300 b22e91aa10c4 $ docker ps --filter name=api CONTAINER ID IMAGE STATUS NAMES b22e91aa10c4 alpine:3.20 Up 2 seconds api -
Step 3 · logs and exec use the name
execstarts a second process in the same container. It can readPORTbecause-eset it atrun.$ docker logs api $ docker exec api printenv PORT 3000 -
Step 4 · stop plus --rm removes the container
$ docker stop api api $ docker ps -a --filter name=api CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -
Step 5 · A restart policy in action
This container fails on purpose. With
on-failure:3Docker tries three more times, then gives up.$ docker run -d --name flaky --restart on-failure:3 alpine:3.20 sh -c 'echo starting; exit 1' $ sleep 10 $ docker inspect -f '{{.RestartCount}} restarts, status={{.State.Status}} exit={{.State.ExitCode}}' flaky 3 restarts, status=exited exit=1 $ docker logs flaky starting starting starting starting $ docker rm flaky -
Step 6 · Live resource use
$ docker run -d --name busy --rm alpine:3.20 sh -c 'while true; do :; done' $ docker stats --no-stream busy NAME CPU % MEM USAGE / LIMIT busy 99.8% 412KiB / 7.6GiB $ docker stop busyThe limit column is the whole machine. Chapter 18 shows how to cap it.
docker logs is empty when the app writes its log to a file inside the container. Docker only captures stdout and stderr. In containers, log to the console.Recap
-ddetaches,-itattaches your keyboard,--rmdeletes on exit.- Use
-efor config that changes per environment, never rebuild for it. - A container lives while its main process lives. When that exits, so does the container.
docker execadds a process to a running container;runcreates a new one.logs,exec,inspectandstatslook inside a container without changing it.- Use
--restart unless-stoppedfor services that should survive crashes and reboots.
Quick check
A container started with -d exits after one second. Where do you look first?
Show answer
Answer: docker ps -a for the exit code, then docker logs.
docker ps -a shows the exit code and docker logs shows what the process printed. A container lives only as long as its main process.
Which restart policy brings a service back after a crash and after a reboot, but leaves it stopped after you run docker stop?
Show answer
Answer: unless-stopped
unless-stopped restarts on crashes and when the daemon starts, unless you stopped the container yourself.