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.
Tags are labels, not versions
Without a tag, Docker assumes latest, which can change under you.
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.
$ 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.
| Command | Removes | Fails when |
|---|---|---|
docker rm | a container | it is still running (use stop first) |
docker rmi | an image | any container, even stopped, still uses it |
docker ps -a | (lists) | shows stopped containers that still exist |
Try it yourself
-
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 -
Step 2 · The image is local
$ docker images nginx REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.27 1b8b7c4a0c1a 2 weeks ago 192MB -
Step 3 · Add another tag
docker tagadds 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:pinnedThe last command drops the extra label.
nginx:1.27still points at the image, so the bytes stay. -
Step 4 · Run creates and starts a container
-dreturns the terminal immediately.--name webis 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 -
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 -
Step 6 · rm deletes the container, rmi deletes the image
The second command fails while
webstill 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…
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"]
| When | What happens |
|---|---|
docker build | RUN 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 run | Docker 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:
- You don’t need Node installed locally. The image brings its own.
EXPOSE 3000is only documentation. To open the app atlocalhost:3000on your Mac, publish the port:docker run -p 3000:3000 <image>(chapter 6).- The server must listen on
0.0.0.0, not127.0.0.1. Bound to localhost, it only accepts connections from inside the container, so even-pwon’t let you reach it. - The container lives as long as that process. When
node server.jsexits or crashes, the container stops.
“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.
- Proof 1 · Ask Node where it is running
Anything after the image name replaces
CMDfor 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 darwinThe container says
linux, and its hostname is the container ID. Your Mac saysdarwin. - Proof 2 · Run it detached
Nothing prints, but the output still exists.
$ docker run -d --name hellow2 hello-w $ docker logs hellow2 Hello, World! - Proof 3 · Find the container afterwards
$ docker ps -a CONTAINER ID IMAGE STATUS NAMES 7d1e0b9a2c44 hello-w Exited (0) 1 minute ago hellowYour program was that container’s main process. It finished, so the container exited.
“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 (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 runs | Inside the container | Inside the container |
| Output in your terminal? | Yes, live | No |
| Output saved? | Yes | Yes (docker logs) |
| Terminal free? | No, it is watching | Yes, right away |
-d only decides whether your terminal watches.Recap
pulldownloads an image,runcreates and starts a container from it.docker psshows running containers;-aadds the stopped ones.rmdeletes a container;rmideletes an image.- Always pin a tag.
latestis a moving target. docker tag SOURCE TARGETadds a label. Both names share one image ID.CMDruns inside the container atdocker run, never on your machine and never at build time.- Attached streams the container’s output to your terminal;
-ddoesn’t, anddocker logsreads it later.
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.