Dockerfile basics
A Dockerfile is a recipe. Every step becomes a layer, and CMD decides what runs when the container starts.
? What do the diagram colours mean
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
From recipe to layers
Docker executes the file top to bottom and saves the result of each step as a layer.
Build time versus run time
The confusion between RUN and CMD disappears once you separate the two moments.
The instructions you will use daily
| Instruction | Job | Can be replaced by docker run image ...? |
|---|---|---|
RUN | runs at build time, adds a layer | no, already baked in |
CMD | default command at start | yes: whatever you type replaces it |
ENTRYPOINT | fixed program at start | no, your words become its arguments |
COPY | host files into the image | no (use -v for runtime files) |
ADD | COPY plus URLs and tar extract | prefer COPY; ADD surprises people |
RUN vs CMD in plain words
Both run commands. The difference is when.
RUNruns at build time (docker build). Whatever it does is saved into the image.CMDruns at start time (docker run). It’s the command each container runs when it starts.
Analogy: packing a lunchbox. RUN is cooking and packing the food. You do it once, in the kitchen, and the result goes into the box. CMD is the note on the lid: “When you open this, eat the sandwich.” Writing the note does nothing by itself. It’s followed every time someone opens the box.
In a Node app’s Dockerfile:
RUN npm ci --omit=dev # build time: installs packages INTO the image
CMD ["node", "server.js"] # start time: what each container runs
npm ci runs once during docker build, and the installed node_modules become part of the image, so every container starts with them already there. CMD does nothing during the build. At docker run, Docker reads it and starts node server.js inside the new container.
RUN | CMD | |
|---|---|---|
| When it runs | docker build | docker run (container start) |
| How often | Once per build | Every time a container starts |
| Result | Saved into the image as a layer | A running process |
| Several in one file | All run, in order | Only the last counts |
| Typical use | Install packages, compile, create folders | Start your app or server |
Override at docker run? | No, it’s baked into the image | Yes: words after the image name replace it |
RUN node server.js tries to start your server during the build. The server never exits, so the build hangs. Even if it finished, nothing would be left running, because a build doesn’t keep processes alive. Quick test: should the result be saved inside the image? Use RUN. Is it what should happen every time a container starts? Use CMD.Try it yourself
Work in an empty directory. Each instruction below becomes one layer.
-
Step 1 · Write the files
$ mkdir greet && cd greet $ printf 'hello from the image\n' > hello.txt $ cat > Dockerfile << 'EOF' FROM alpine:3.20 WORKDIR /app COPY hello.txt . RUN printf 'built in the image\n' > /app/built.txt CMD ["cat", "/app/hello.txt"] EOF -
Step 2 · Build, and read each step
The numbers are the Dockerfile instructions.
FROMdownloads Alpine.WORKDIRcreates/app.COPYaddshello.txt.RUNwritesbuilt.txtinto the image.$ docker build -t greet:1 . [1/4] FROM docker.io/library/alpine:3.20 [2/4] WORKDIR /app [3/4] COPY hello.txt . [4/4] RUN printf 'built in the image\n' > /app/built.txt naming to docker.io/library/greet:1 -
Step 3 · CMD is the default process
$ docker run --rm greet:1 hello from the image -
Step 4 · A command on docker run replaces CMD
The file from
RUNis in the image, so this container can read it.--rmdeletes the container whencatexits. The image stays.$ docker run --rm greet:1 cat /app/built.txt built in the image $ docker images greet REPOSITORY TAG IMAGE ID SIZE greet 1 9f0c1a2b3c4d 8MB
CMD node server.js in shell form. It runs under /bin/sh, so the stop signal goes to the shell instead of node, and docker stop waits 10 seconds before killing it. Use the exec form: CMD ["node", "server.js"].How a command on docker run replaces CMD
Step 4 ran docker run --rm greet:1 cat /app/built.txt, and hello.txt was never printed. Here is why.
CMD is written on the image’s settings card
An image is not just files. Docker also saves a small settings card (its config) with it. CMD ["cat", "/app/hello.txt"] doesn’t run anything at build time. It writes Cmd = [cat /app/hello.txt] on that card. You can read the card yourself:
$ docker inspect greet:1 --format '{{.Config.Cmd}}'
[cat /app/hello.txt]
Words after the image name are the command
Docker reads docker run in this shape:
docker run [options] IMAGE [command to run]
docker run --rm greet:1 cat /app/built.txt
Options such as --rm go before the image name. Everything after it is the command. Then Docker follows one rule:
| You type | Docker runs |
|---|---|
docker run --rm greet:1 | The card’s default: cat /app/hello.txt |
docker run --rm greet:1 cat /app/built.txt | Your command: cat /app/built.txt |
It’s a straight swap, not a merge. The default command doesn’t run at all, before or after. The swap only applies to the container this one command creates. The image isn’t changed, so the next plain docker run greet:1 gets the default back. That’s why the instruction is a default command: it’s used only when you don’t give one.
Analogy: a café. The menu says “Today’s default: coffee” (CMD). Say nothing and you get coffee. Say “tea, please” (cat /app/built.txt) and you get tea, and no coffee. The menu isn’t rewritten. The next customer who says nothing still gets coffee.
The whole run, step by step
| Part | Job |
|---|---|
docker run | Create a new container and start it |
--rm | Delete the container automatically when it finishes |
greet:1 | The image: name greet, tag 1 |
cat /app/built.txt | The command to run inside the container. It replaces CMD for this run |
- 1 · Find the image
greet:1is already local fromdocker build, so nothing is downloaded. - 2 · Create a container
A fresh container whose files are a copy of the image’s, including
/app/built.txt, whichRUNwrote at build time. - 3 · Start your command inside it
cat /app/built.txtbecomes the container’s main process. It reads the container’s own files, not your machine’s folders. - 4 · Output reaches your terminal
There’s no
-d, so you’re attached and Docker forwards whatcatprints (chapter 2). - 5 · cat exits, so the container stops
A container lives exactly as long as its main process, which here is a split second.
- 6 · --rm deletes the container
Only the unchanged image is left.
RUN line to RUN date > /app/built.txt and rebuild. Run cat /app/built.txt twice: the time is the same both times, because it was written once, at build. Rebuild with docker build --no-cache -t greet:1 . and you get a new time. Without --no-cache, Docker may reuse the saved step and keep the old time.The exception: ENTRYPOINT
ENTRYPOINT is not replaced this way. With an ENTRYPOINT, the words after the image name are added to the end of it instead (the CMD vs ENTRYPOINT diagram above shows both cases). When both are set, CMD supplies the default arguments:
ENTRYPOINT ["node"]
CMD ["server.js"]
$ docker run app # runs: node server.js
$ docker run app other.js # runs: node other.js (only CMD was swapped)
To replace an ENTRYPOINT you need the separate flag --entrypoint. A Dockerfile with only CMD, like greet, gets the simple swap.
Recap
- Each
FROM,COPY,RUNcreates a layer;CMDadds only metadata. RUNruns at build time.CMDruns when a container starts.CMDis a replaceable default;ENTRYPOINTis the fixed program.- Prefer
COPYoverADD, and a small pinned base likealpine. CMDis stored in the image config. Words after the image name indocker runreplace it for that one container.- Never start your app with
RUN. The build would hang waiting for it.
Quick check
A Dockerfile has two CMD lines. What runs?
Show answer
Answer: Only the last CMD.
Only the last one. Earlier CMD lines are ignored. Use ENTRYPOINT when the command must not be replaced by arguments.
Where does npm ci belong: RUN or CMD?
Show answer
Answer: RUN: it runs once at build time and its result is saved as a layer.
RUN, so the result is baked into the image. CMD is the command that starts your app.
The image has CMD ["cat", "/app/hello.txt"]. What does docker run --rm greet:1 cat /app/built.txt print?
Show answer
Answer: Only built.txt. Your command replaced CMD for this container.
Words after the image name replace CMD. The image is unchanged, so a plain docker run greet:1 still prints hello.txt.
Dockerfiles: common questions
What is the difference between CMD and ENTRYPOINT?
ENTRYPOINT sets the program that always runs. CMD gives default arguments, or a default command, that docker run arguments replace.