Quiz
Warm-up from lesson 1-2: `docker run nginx` starts a container. What is the relationship between that container and the nginx image?
What docker run actually does
docker run is the docker command working engineers type most. It is how you get a Postgres for local development in one line, try a tool without installing it, or reproduce a bug in a clean environment. The classic first command:
docker run hello-world
Step by step, Docker:
- Looks for an image called
hello-worldon your machine - Doesn't find it, so it pulls (downloads) it from Docker Hub, the default public image registry
- Creates a new container from the image
- Starts the container, which prints a greeting
- The program ends, so the container exits
That last step matters: a container lives exactly as long as the one program it was started to run. When the program stops, the container stops. hello-world prints and quits, so its container is done in under a second.
Anatomy of the command
docker run -it python:3.12 pythonReading it piece by piece:
docker run: create and start a container-it: interactive mode with a terminal attached, so you can type into itpython:3.12: the image, asname:tag. The tag after the colon picks a version. Omit it and Docker assumes:latestpython(the last word): the command to run inside the container, overriding the image's default
That one line drops you into a real Python 3.12 prompt inside an isolated container, even if your laptop has no Python installed at all. Type exit() and the container stops.
Pin your tags. python:3.12 today is python:3.12 next year, but latest silently moves, which is the works-on-my-machine bug from lesson 1-1 sneaking back in.
Code exercise · bash
Your turn: image references are strings, and scripts parse them constantly. Bash gives you ${var%%:*} (everything before the first colon) and ${var##*:} (everything after the last colon). Print the image name on the first line and the tag on the second.
Docker isn't available in this sandbox, so practice in this simulated shell. Run your first container and then look at what it left behind.
Step 1/4: Run the hello-world image. Docker will pull it first since this machine has never seen it.
Problem
In the image reference `python:3.12`, what is the part after the colon called?