Skip to content
Theory · Your first container · lesson 1 of 7

What docker run does

What you'll learn

What you'll learn:

  • start a container with one command and understand what it prints;
  • list containers and their output, stop them and remove them;
  • reach a server in a container with -p host:container;
  • read an image name: registry, repository and tag.
A cookie cutter nginx and three identical cookies cut from it; cookie web-2 is bitten, the cutter is intact

Picture a cookie cutter. It is made once and never changes afterwards. Every cookie it cuts comes out the same, and then each one goes its own way: one gets bitten, another gets eaten, and the cutter stays as it was.

In Docker the image is the cutter and the container is the cookie. You can start as many containers from one image as you like, and none of them changes the image itself. In this theme you start the nginx web server in a container, learn to manage it and open its page.

The docker run command starts a container:

Shell
docker run -d --name web nginx:1.27.3

Let's take it apart:

  • docker run — create a container from an image and start it;
  • -d — run the container in the background and give the terminal back at once;
  • --name web — name the container web, the name other commands use to reach it;
  • nginx:1.27.3 — the image: the nginx web server, version 1.27.3.

The flags sit between docker run and the image name. This order matters, and at the end of the lesson we'll see why.

What the first run prints

If the image is not on your machine yet, the first run prints many lines:

TEXT
Unable to find image 'nginx:1.27.3' locally
1.27.3: Pulling from library/nginx
ab571a6216e3: Pull complete
a982d09283a6: Pull complete
8c2914db26a3: Pull complete
c29f5b76f736: Pull complete
ee083de5ceda: Pull complete
1e8aefce6919: Pull complete
5afd6583b29c: Pull complete
328935d6d818: Download complete
60782693e519: Download complete
Digest: sha256:bc2f6a7c8ddbccf55bdb19659ce3b0a92ca6559e86d42677a5a02ef6bda2fcef
Status: Downloaded newer image for nginx:1.27.3
9298410138c09b1747a649cc5b3e4152083802e4bd509fb9c234e1f67073bdac

Let's go through them in order:

  1. Unable to find image 'nginx:1.27.3' locally — Docker looked for the image on the machine and did not find it.
  2. Pulling from library/nginx — it started downloading the image from a registry, a shared store of images.
  3. Seven Pull complete lines — seven layers of the image are downloaded and unpacked. Each line starts with a short id of the layer.
  4. Two Download complete lines — service data that describes the image, not layers.
  5. Digest: sha256:… — a fingerprint of the image's content: different content would give a different fingerprint.
  6. Status: Downloaded newer image for nginx:1.27.3 — the image is downloaded and stored on the machine.
  7. The last long line is the id of the new container. Once it is printed, the container is created and running.

A second docker run of the same image downloads nothing: the image is already on the machine, and the command prints the new container's id right away. Without --name, Docker makes up a name itself, such as hardcore_stonebraker.

Three steps in one command

docker run performs three steps, and each one has its own command:

  1. docker pull downloads the image if it is not on the machine.
  2. docker create creates a container from the image but does not start it.
  3. docker start starts the created container.

Let's create a second container in two steps. The image is already downloaded, so no pull is needed:

Shell
docker create --name web2 nginx:1.27.3
docker start web2
TEXT
3ff808bc6421fd816c636737e12809efce8a7cfabc0c7ee8559f0587fa5998d8
web2

docker create printed the container's id, and docker start printed its name. Between the two commands the container already exists, but its program is not running yet.

This is where the cookie picture stops working. A cookie lies still on the table, while inside a container a program runs — here nginx, waiting for requests.

Background and foreground

Without the -d flag, the container runs in the foreground. The terminal shows everything nginx prints and does not return the prompt. Ctrl+C in that terminal stops the container too, which is why servers are usually started with -d.

Common mistake

The flags went after the image name:

Shell
docker run nginx:1.27.3 -d --name web
TEXT
/docker-entrypoint.sh: 47: exec: -d: not found

Docker treats as its own flags only what comes before the image name. Everything after it goes to the program in the container as its command. Here the container tried to run a program called -d, did not find it and exited at once, under a random name rather than web. Put the flags before the image: docker run -d --name web nginx:1.27.3.

Key points

  • docker run downloads the image if it is missing, creates a container from it and starts it.
  • The image is the cutter, the container the cookie: one image starts any number of containers.
  • -d runs the container in the background, --name gives it a name.
  • Flags go before the image name; everything after it is a command for the program in the container.