Course outline · 0% complete

0/29 lessons0%

Course overview →

Your first docker run

lesson 2-1 · ~12 min · 4/29

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:

  1. Looks for an image called hello-world on your machine
  2. Doesn't find it, so it pulls (downloads) it from Docker Hub, the default public image registry
  3. Creates a new container from the image
  4. Starts the container, which prints a greeting
  5. 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 python

Reading it piece by piece:

  • docker run: create and start a container
  • -it: interactive mode with a terminal attached, so you can type into it
  • python:3.12: the image, as name:tag. The tag after the colon picks a version. Omit it and Docker assumes :latest
  • python (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.

you@laptop $ 

Problem

In the image reference `python:3.12`, what is the part after the colon called?