Session 5 · Background containers, names, logs and inspect#
The big idea#
Real services (web servers, databases) must keep running while you do other things. Run them detached
with -d, give them a name, and then use docker logs to read what they print, docker inspect to
see their settings, and docker stop / docker rm to end them. These four commands are your day-to-day
toolkit.
Everyday example: a CCTV camera at a shop. It runs in the background all day; you don’t stand and watch it. When something happens you play back the recording (logs), check the camera’s settings page (inspect), and switch it off (stop) when closing. You also label each camera (“Entrance”, “Cash counter”) so you know which is which.
The commands in action#
docker rm -f dm-ticker >/dev/null 2>&1
echo '--- start a container in the background with a name'
docker run -d --name dm-ticker alpine sh -c 'for i in 1 2 3 4 5; do echo "tick $i"; sleep 1; done; sleep 60' >/dev/null
sleep 6
docker ps --filter name=dm-ticker --format '{{.Names}} {{.Image}} {{.Status}}'
echo '--- read its output with logs'
docker logs dm-ticker
echo '--- inspect: pull single facts out of the JSON'
docker inspect --format 'state={{.State.Status}} image={{.Config.Image}} cmd={{.Config.Cmd}}' dm-ticker
echo '--- stop and remove'
docker stop dm-ticker >/dev/null
docker ps -a --filter name=dm-ticker --format '{{.Names}}: {{.Status}}' | sed 's/Exited (\([0-9]*\)) .*/Exited (\1)/'
docker rm dm-ticker
echo '--- names must be unique'
docker run -d --name dm-ticker alpine sleep 5 >/dev/null
docker run -d --name dm-ticker alpine sleep 5 2>&1 | head -1 | sed 's/by container "[0-9a-f]*"/by container "<id>"/; s/^docker: //'
docker rm -f dm-ticker >/dev/null
--- start a container in the background with a name
dm-ticker alpine Up 6 seconds
--- read its output with logs
tick 1
tick 2
tick 3
tick 4
tick 5
--- inspect: pull single facts out of the JSON
state=running image=alpine cmd=[sh -c for i in 1 2 3 4 5; do echo "tick $i"; sleep 1; done; sleep 60]
--- stop and remove
dm-ticker: Exited (137)
dm-ticker
--- names must be unique
Error response from daemon: Conflict. The container name "/dm-ticker" is already in use by container "<id>". You have to remove (or rename) that container to be able to reuse that name.
Reading the output:
-dprinted nothing but the container id (we hid it) and gave the terminal back.docker psshows itUp.docker logsreplays everything the main process wrote to standard output: ticks 1 to 5. Add-fto follow live and--tail 20for the last lines only.docker inspect --formatpulls single facts from the long JSON: state, image, command. Without--formatyou get the full document (networks, mounts, environment).Exit code 137 after
docker stop: Docker first asks the process to stop (SIGTERM), waits 10 seconds, then kills it (SIGKILL = 128 + 9 = 137). Oursleepdid not react to SIGTERM, so it was killed. A well-behaved server exits with 0 straight away.Unique names. Starting a second container called
dm-tickeris refused until the first is removed or renamed.
Naming and cleaning#
Task |
Command |
|---|---|
Start in the background, named |
|
Follow the log live |
|
Stop politely |
|
Stop and delete in one go |
|
Delete all stopped containers |
|
Rename |
|
Common mistakes#
Running a server without
-d. It blocks your terminal (Ctrl+C stops it).A detached container that exits at once. Check
docker logs NAMEfirst: the error is almost always printed there.Reusing a name. Remove the old container (
docker rm NAME) or choose a new name.Reading old logs after a rebuild. Logs belong to a container; a new container starts with a clean log.
Using
docker killas the normal way to stop. It skips graceful shutdown. Usedocker stopand fix programs that ignore it.
Hands-on exercises#
Exercise 1. Start alpine in the background so that it prints “ping” every second, forever, then show
only the last 3 lines of its log.
Solution
docker run -d --name pinger alpine sh -c 'while true; do echo ping; sleep 1; done'
sleep 4
docker logs --tail 3 pinger
docker rm -f pinger
The last three log lines are each ping.
Exercise 2. Use docker inspect to print only the image name of a running container.
Solution
docker run -d --name nap alpine sleep 30
docker inspect --format '{{.Config.Image}}' nap
docker rm -f nap
It prints alpine.
Exercise 3. Why does docker stop take about 10 seconds on sleep 60 but is instant for most web
servers?
Solution
docker stop sends SIGTERM and waits up to 10 seconds before SIGKILL. As PID 1 in a container, sleep
ignores SIGTERM unless it has a handler, so Docker has to wait out the timeout. Servers that handle
SIGTERM shut down at once.
Practice#
Take the Session 5 quiz. The original page Diving Deeper with Detached Mode, Naming and Inspections is in the reference section.