Card 01/ 06

ConceptDifficulty: Beginner1 min

docker run: The Three Steps Behind One Command

Why the first start of an image takes thirty seconds and every start after it takes one.

docker run is the command that starts a container (a program running in its own isolated space) from an image (the packaged files and settings that program needs). It is one command for three separate steps: getting the image, creating a container from it, and starting the program inside. Knowing which step is which explains why some starts are slow and why a container can exist while nothing runs in it.

In order, docker run does this:

text
1. Pull the image, if this machine does not already have it
2. Create a container from it, with its own writable layer
3. Start the process the image names, and attach to it

The first step, the pull, downloads the image from a registry (an online library of images; Docker Hub by default). It is the only step that touches the network, and it only happens when the image is not on your machine yet. A second docker run of the same image skips straight to step two, which is why the first start of an image can take thirty seconds and every start after it takes about one.

Steps two and three are separate too. Step two creates the container, with its own writable layer (a private area that holds every file the container changes), and step three starts the program the image names. When that program finishes, the container still exists: step two happened and step three ended. That is what you are looking at when a container appears in docker ps -a (every container) and not in docker ps (only running ones).

So a slow first start is a download, not a problem, and a container that is no longer running has not disappeared: it is waiting to be inspected or removed.

← → move through sections and cards