Docker module
Lesson 3
Run a container
docker run, flags, Alpine, and the difference between a command that exits and a command that stays up.
beginner30 minRun on your machine
What you will be able to do
- Run a container from Alpine with a command you choose
- Use -it for an interactive shell and --rm to clean up
- Explain why a container exits when its main process exits
Why this matters for DE and AI
docker run is “start this image with this command.” A Spark submit, a dbt run, a one-shot extract — same shape. If you only ever click Start in a GUI, you cannot tell a job that finished from a job that never started.
Concepts
Shape of the command:
docker run [flags] IMAGE [command] [args...]
Examples you will type today:
docker run --rm alpine:3.20 uname -a
docker run --rm -it alpine:3.20 sh
- IMAGE —
alpine:3.20means repositoryalpine, tag3.20. First use downloads it. - command — replaces the image’s default. Alpine’s default is
sh;uname -ais more useful for a one-liner. --rm— remove the container when it exits. Good for experiments.-i— keep stdin open.-t— allocate a terminal. Together-itis how you get a prompt inside.
Why it stops
A container lives as long as its main process. uname -a prints and exits — container exits. sleep 300 stays until you stop it or 300 seconds pass. Postgres stays because its server process stays.
That is why a job image with command: python extract.py “dies” when the script ends. That can be success.
--name
Without a name, Docker assigns a random one (funny_einstein). For anything you will logs or stop, pass --name dcubes-…. Names must be unique among existing containers.
Practice on your machine
cd ~/dcubes/docker-lab
Run a one-shot command on Alpine. The first pull is a few megabytes.
docker run --rm alpine:3.20 uname -a
What you should see: a Linux kernel line. You are looking at the container’s uname, not your Mac’s Darwin line. On WSL/Linux it still prints the host kernel (containers share it) but the userland is Alpine.
Print the OS release inside the image:
docker run --rm alpine:3.20 cat /etc/os-release
You should see Alpine Linux.
Now take an interactive shell. -it matters; without it sh looks “stuck” with no prompt.
docker run --rm -it alpine:3.20 sh
Your prompt changes (often / #). You are not in ~/dcubes/docker-lab on the host. This is a tiny Linux filesystem.
Inside the container, run:
pwd
ls /
echo hello from alpine
exit
What you should see: / as the working directory, folders like bin etc tmp, then your host prompt again after exit.
If you get stuck inside, type exit or press Ctrl + D.
Common mistakes
- Forgetting
-it.shwith no TTY prints nothing useful. Add-itor pass a non-interactive command (uname -a). - Typing
docker run alpineforever. Without a tag you getlatest, which moves. We pin3.20in this module. - Thinking the container has your files. It does not, until you mount them (lesson 8).
ls ~/dcubesinside Alpine will fail.
Next
Container lifecycle — names, ps, logs, stop, and rm.