Session 4 · Running your first containers#
The big idea#
docker run is the command you will type most. It creates a container from an image and runs a
command in it. The container lives exactly as long as its main process: when that command finishes,
the container stops. That one rule explains most beginner surprises.
Everyday example: a taxi booked for a single trip. It comes, drives where you asked, and the booking ends when the ride ends. A container is booked for one command; when the command is done, it is done. (If you want a “taxi on standby”, run a command that keeps going, like a web server.)
Run, look around, run more#
docker rm -f dm-shell >/dev/null 2>&1
echo '--- run a command and let the container disappear (--rm)'
docker run --rm alpine echo "Hello, Docker!"
echo '--- the same container has its own hostname and its own process list'
docker run --rm alpine sh -c 'hostname; ps'
echo '--- a container lives only as long as its main process'
docker run --name dm-shell alpine sh -c 'echo working; sleep 1; echo done'
docker ps -a --filter name=dm-shell --format '{{.Names}}: {{.Status}}'
echo '--- run a second command inside a running container (exec)'
docker run -d --name dm-shell2 alpine sleep 30 >/dev/null
docker exec dm-shell2 sh -c 'echo "I am inside: $(hostname)"; cat /etc/alpine-release'
docker rm -f dm-shell dm-shell2 >/dev/null
--- run a command and let the container disappear (--rm)
Hello, Docker!
--- the same container has its own hostname and its own process list
ab7c3a046a65
PID USER TIME COMMAND
1 root 0:00 ps
--- a container lives only as long as its main process
working
done
dm-shell: Exited (0) Less than a second ago
--- run a second command inside a running container (exec)
I am inside: 1f30554a3e2c
3.24.1
What happened:
--rmdeleted the container as soon as it exited, so nothing was left to clean up.A private world. Inside,
hostnameis a random id andpsshows only one process (PID 1), not your host’s hundreds. Containers have their own process list.Main process = lifetime. The
sh -c '... sleep 1 ...'ran for a second, then the container showedExited (0). Exit code 0 means success.docker execruns an extra command in a container that is already running. We started one withsleep 30, then ran another shell command inside it.Both
hostnamevalues differ because those are two different containers.
Interactive mode#
For a shell you can type into, add -it: docker run -it alpine sh. -i keeps input open and -t gives a
terminal. Type exit to leave; the container stops because its main process (the shell) ended.
Reading docker ps#
docker ps lists running containers; docker ps -a also lists the stopped ones. The columns you
will use most are NAMES (the handle you type in other commands), IMAGE (what it was made from) and
STATUS (Up 5 minutes, or Exited (0) 2 minutes ago). If you do not give --name, Docker invents a
funny name like quirky_hopper. Naming containers yourself makes commands readable, and you’ll do that
properly in the next session.
Common mistakes#
Expecting a container to stay up.
docker run alpineexits immediately because the default command has nothing to do. Give it a long-running command or a server.Forgetting
-itfor a shell. Without itshsees no input and exits at once.Typing
execon a stopped container.docker execneeds a running container. Usedocker runordocker startfirst.Never cleaning up. Each
docker runwithout--rmleaves a stopped container. Checkdocker ps -a.Reading exit codes wrongly.
0is success, anything else is an error (137means it was killed).
Hands-on exercises#
Exercise 1. Run a container that prints today’s date and then disappears.
Solution
docker run --rm alpine date
It prints the date (in UTC) and leaves nothing behind, since --rm removed it.
Exercise 2. Start an Alpine container that sleeps for 60 seconds in the background, run cat /etc/alpine-release
inside it, then remove it.
Solution
docker run -d --name nap alpine sleep 60
docker exec nap cat /etc/alpine-release
docker rm -f nap
On this machine the version printed was 3.24.1 (yours may differ). rm -f stops and removes in one step.
Exercise 3. Run a container whose command fails and read its exit code.
Solution
docker run --name bad alpine sh -c 'exit 3'
docker ps -a --filter name=bad --format '{{.Status}}'
docker rm bad
The status shows Exited (3): the container’s exit code is the command’s exit code.
Practice#
Take the Session 4 quiz. The original BusyBox “Hello, World” adventure is in the reference section.