Stage 1 · Foundations · Chapter 2 of 20 · 8 min read

Images & containers

The whole lifecycle in five verbs: pull, run, ps, stop, rm. Know which object each one touches.

? What do the diagram colours mean

key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral

The container lifecycle

A container moves through a few states, and a different command moves it between each.

Container lifecycleRegistry to image with pull, image to running container with run, running to exited with stop, exited to removed with rm.Registrynginx:1.27pullImageon your diskrunRunningdocker psstopExiteddocker ps -armRemovedgonermi removes the Image box. rm removes the container. They are different objects.
docker run does the first three arrows in one command.
What docker run does1 · Image local?pull it if notif not2 · Createnew container + layerthen3 · Startlaunch the process
Run is three commands in one: pull if missing, create, start.

Tags are labels, not versions

Without a tag, Docker assumes latest, which can change under you.

The latest tag driftsA tag is a movable label. On Monday latest points to one image; on Friday it points to a different one.MondayFridayapp:latestimage abc123app:latestimage def456Same command, same tag......different code. Surprise in production.Pin it: nginx:1.27 (or an @sha256 digest for byte-exact images)

Add a label with docker tag SOURCE TARGET. SOURCE is a name:tag you already have, or an image ID. TARGET is the new name:tag. Leave the tag off the target and Docker writes latest.

Adding a tagdocker tag nginx:1.27 nginx:pinned adds a second label. Both names point at image 1b8b7c4a0c1a. Nothing is copied.nginx:1.27name you already havenginx:pinnedname you just addeddocker tagone image1b8b7c4a0c1aBoth names show the same IMAGE ID. docker tag stored a label, not a second copy.
The source can be a name:tag or an image ID. The target is the new name:tag.
$ docker tag nginx:1.27 nginx:pinned
$ docker tag 1b8b7c4a0c1a my-nginx:1.27
$ docker images
REPOSITORY   TAG     IMAGE ID       CREATED       SIZE
nginx        1.27    1b8b7c4a0c1a   2 weeks ago   192MB
nginx        pinned  1b8b7c4a0c1a   2 weeks ago   192MB
my-nginx     1.27    1b8b7c4a0c1a   2 weeks ago   192MB

docker rmi nginx:pinned drops that label only. The image stays while any other tag still points at it. Building your own image writes the label at creation time: docker build -t name:tag (chapter 4).

Removing things

Containers and images are separate objects with separate remove commands.

Why rmi is blockedBoth a running and an exited container reference the image, so removing the image fails until the containers are removed.nginx:1.27imagewebrunningold-webexitedusesusesdocker rmi: refusedRemove every container that uses an image first, then remove the image.
CommandRemovesFails when
docker rma containerit is still running (use stop first)
docker rmian imageany container, even stopped, still uses it
docker ps -a(lists)shows stopped containers that still exist

Try it yourself

  1. Step 1 · Pull a pinned tag
    $ docker pull nginx:1.27
    1.27: Pulling from library/nginx
    Status: Downloaded newer image for nginx:1.27
  2. Step 2 · The image is local
    $ docker images nginx
    REPOSITORY   TAG    IMAGE ID       CREATED       SIZE
    nginx        1.27   1b8b7c4a0c1a   2 weeks ago   192MB
  3. Step 3 · Add another tag

    docker tag adds a name. The image ID does not change, so this copied nothing.

    $ docker tag nginx:1.27 nginx:pinned
    $ docker images nginx
    REPOSITORY   TAG     IMAGE ID       CREATED       SIZE
    nginx        1.27    1b8b7c4a0c1a   2 weeks ago   192MB
    nginx        pinned  1b8b7c4a0c1a   2 weeks ago   192MB
    $ docker rmi nginx:pinned
    Untagged: nginx:pinned

    The last command drops the extra label. nginx:1.27 still points at the image, so the bytes stay.

  4. Step 4 · Run creates and starts a container

    -d returns the terminal immediately. --name web is the name you will use next.

    $ docker run -d --name web nginx:1.27
    a91c0e2f4b10…
    $ docker ps
    CONTAINER ID   IMAGE        STATUS         NAMES
    a91c0e2f4b10   nginx:1.27   Up 3 seconds   web
  5. Step 5 · Stop hides it from docker ps
    $ docker stop web
    web
    $ docker ps
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    $ docker ps -a
    CONTAINER ID   IMAGE        STATUS                      NAMES
    a91c0e2f4b10   nginx:1.27   Exited (0) 2 seconds ago    web
  6. Step 6 · rm deletes the container, rmi deletes the image

    The second command fails while web still exists. Remove the container, then the image.

    $ docker rmi nginx:1.27
    Error: conflict: unable to delete nginx:1.27 (must be forced) - container a91c0e2f4b10 is using its referenced image
    $ docker rm web
    web
    $ docker rmi nginx:1.27
    Untagged: nginx:1.27
    Deleted: sha256:1b8b7c4a0c1a…
Common mistakeRunning docker stop and expecting the container to be gone. Stop only halts it: it still exists in docker ps -a, still holds its name and still blocks rmi. Use docker rm, or start throwaway containers with --rm.

Where does your program actually run?

Three questions that come up as soon as you run your first image. Each answer builds on the one before.

“Does CMD node server.js run in my local terminal?”

No. It runs inside the container, not on your Mac. Take this Dockerfile:

FROM node:22-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY server.js .
EXPOSE 3000
CMD ["node", "server.js"]
WhenWhat happens
docker buildRUN npm ci runs now, inside a temporary build container. CMD does not run. It is only saved as the image’s default start command.
docker runDocker starts a container from the image and runs node server.js as its main process. It uses the Node 22 inside node:22-alpine and the files copied to /app, not anything on your machine.

What follows from that:

“But I saw Hello, World! in my own terminal”

$ docker run --name hellow hello-w
Hello, World!

It looks local, but the program ran inside the container. Docker forwarded its output to your terminal. You can prove it three ways.

  1. Proof 1 · Ask Node where it is running

    Anything after the image name replaces CMD for this one run.

    $ docker run --rm hello-w node -e "console.log(process.platform, require('os').hostname())"
    linux 3f2a9c1b7e4d
    $ node -e "console.log(process.platform)"      # directly on your Mac
    darwin

    The container says linux, and its hostname is the container ID. Your Mac says darwin.

  2. Proof 2 · Run it detached

    Nothing prints, but the output still exists.

    $ docker run -d --name hellow2 hello-w
    $ docker logs hellow2
    Hello, World!
  3. Proof 3 · Find the container afterwards
    $ docker ps -a
    CONTAINER ID   IMAGE     STATUS                     NAMES
    7d1e0b9a2c44   hello-w   Exited (0) 1 minute ago    hellow

    Your program was that container’s main process. It finished, so the container exited.

On a MacContainers need a Linux kernel, so Docker Desktop runs a small Linux virtual machine in the background, and your containers run inside it. Your terminal is only a window onto their output.

“What do attached and detached actually mean?”

Picture the container as a separate little computer inside your Mac. When your program prints Hello, World!, it prints to that computer’s screen. That computer has no screen of its own, so Docker gives you two choices.

Attached vs detached outputThe program prints inside the container. Docker always records the output. When attached, Docker also streams it live to your terminal. When detached, you read the recording later with docker logs.Containerprints Hello, World!outputDockeralways records itattached-dYour terminalshows it livedocker logsread it later
Either way the program runs in the container. Only who is watching changes.

Attached (the default). docker run hello-w is like starting the program on that little computer and pointing a live camera at its screen. Docker shows you what the camera sees, in your terminal. It’s the default because when you start something, you usually want to see what it says. The catch: your terminal is busy while it runs. A server like server.js keeps the terminal showing its logs until you press Ctrl+C.

Detached (-d). docker run -d hello-w is like starting the program and walking away without the camera. It still runs and still prints, and Docker still records the output. Read it with docker logs <name>, or follow it live with docker logs -f <name>. Your terminal is free immediately, which is what you want for servers you leave running.

Attached (default)Detached (-d)
Where the program runsInside the containerInside the container
Output in your terminal?Yes, liveNo
Output saved?YesYes (docker logs)
Terminal free?No, it is watchingYes, right away
Common mistakeThinking attached means “runs on my machine” and detached means “runs in Docker”. The first row of the table never changes. -d only decides whether your terminal watches.

Recap

Quick check

Why does docker rmi nginx:1.27 fail even though docker ps is empty?

Show answer

Answer: A stopped container still uses the image.

A stopped container still references the image. docker ps -a lists it; remove it with docker rm, then the image.

You run nginx with no tag on Monday and again on Friday. Why might you get different versions?

Show answer

Answer: No tag means latest, and the publisher can move latest to a new image at any time.

latest is a label the publisher moves. Pin a version such as nginx:1.27 to get the same image every time.

You run docker tag nginx:1.27 nginx:pinned. What is on disk afterwards?

Show answer

Answer: One image with two names. Both rows share an image ID.

docker tag adds a label. The image ID does not change, and no extra layers are stored.

You run docker run hello-w and Hello, World! appears in your terminal. Where did the program run?

Show answer

Answer: Inside the container. Docker forwarded its output because the run was attached.

Attached or detached, the process always runs in the container. Attached just streams its output to you; with -d you read it with docker logs.

Images and containers: common questions

What is the difference between a Docker image and a container?

An image is a read-only template. A container is a running instance of it with its own writable layer.