Dockerfile instructions, from FROM to CMD
What each of the 18 Dockerfile instructions does, which ones change files and which only edit the image config, and the shell and exec forms.
docker run and the container lifecycleLayers, overlayfs and copy-on-write
Dockerfile instructions, from FROM to CMD
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A Dockerfile is a list of instructions. Each one either changes the filesystem (RUN, COPY, ADD) or only edits the image config (ENV, USER, WORKDIR, EXPOSE, CMD and the rest).
- FROM starts a build stage from a base image. Only ARG may come before the first FROM, and every later FROM starts a fresh stage.
- RUN, CMD and ENTRYPOINT have a shell form, run through /bin/sh -c, and an exec form, a JSON array run directly. The exec form needs double quotes.
- COPY copies from the build context or another stage. ADD can also fetch URLs and Git repositories and unpack local tar archives, so prefer COPY unless you need those.
- WORKDIR and USER set defaults for every later instruction and for the running container. Set both explicitly rather than trusting the base image.
- EXPOSE only documents a port. It publishes nothing; -p at run time does.
- Instructions are case-insensitive, but upper case is the convention, and BuildKit's checks flag a file that mixes cases.
A recipe the builder can replay
You could build an image by starting a container, installing things by hand and committing it. Nobody could repeat that next month. A Dockerfile writes the same steps down as text, so the build can be replayed on any machine, reviewed in a pull request and cached step by step.
The devops lesson on images showed that an image is layers plus a JSON config. This lesson is the vocabulary: the 18 instructions in the Dockerfile reference, what each one writes, and the two ways of spelling a command.
Two kinds of instruction
Every instruction does one of two things.
| Changes files (makes a layer) | Only edits the config |
|---|---|
RUN, COPY, ADD |
ENV, ARG, LABEL, USER, WORKDIR, EXPOSE, VOLUME, ENTRYPOINT, CMD, STOPSIGNAL, HEALTHCHECK, SHELL, ONBUILD |
FROM starts a stage, and MAINTAINER is deprecated in favour of LABEL. WORKDIR is a slight exception: it creates the directory if it is missing.
Think of the image as a shipping crate with a packing slip taped on top. RUN and COPY put things in the crate. ENV, USER and CMD only write on the slip. That is why an ENV line costs no space, and why the file-changing lines are where size and cache misses come from.
The instructions you will use every day
# syntax=docker/dockerfile:1
FROM python:3.14-slim
LABEL org.opencontainers.image.source="https://github.com/example/notes"
RUN useradd --system --uid 10001 notes
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
ENV PORT=8080
EXPOSE 8080
USER notes
CMD ["python", "-m", "src.server"]
FROMpicks the base image and starts a build stage. OnlyARGmay appear before the first one. Pin a tag, or a digest for full repeatability.RUNexecutes a command in a temporary container and commits the changed files as a layer.COPYcopies files from the build context, or with--fromfrom another stage or image.--chownand--chmodset ownership and mode as it copies.ADDdoes whatCOPYdoes, plus it can download a URL, clone a Git repository, and unpack a local tar archive. Docker’s best-practices page says to preferCOPYunless you need one of those.WORKDIRsets the directory for laterRUN,COPY,ADD,CMDandENTRYPOINT. A relative path is relative to the previousWORKDIR, which is why the reference recommends absolute paths.ENVsets a variable for the rest of the build and for every container.ARGis build-time only, covered in build arguments and environment variables.USERsets the user for laterRUNlines and for the container’s process.EXPOSErecords a port in the config. It publishes nothing.-pat run time does, as publishing ports explains.CMDandENTRYPOINTsay what the container runs. How they combine, and why it matters for signals, is the next lesson.
The less common ones: VOLUME declares a mount point that gets an anonymous volume at run time. HEALTHCHECK gives the container a health command, covered in restart policies and health checks. STOPSIGNAL changes the signal docker stop sends, SHELL changes the shell used by shell-form commands, and ONBUILD registers instructions that run when someone else builds FROM your image.
Shell form and exec form
RUN, CMD and ENTRYPOINT accept two spellings.
RUN apt-get update && apt-get install -y curl # shell form
CMD ["nginx", "-g", "daemon off;"] # exec form
The shell form is wrapped in /bin/sh -c, so you get pipes, && and $VARIABLES. The exec form is a JSON array run directly, with no shell. Two traps follow from the reference. The array must use double quotes, or it is not valid JSON and the builder silently treats it as shell form. And RUN ["echo", "$HOME"] prints the literal $HOME, because nothing expands it.
For long RUN steps, heredocs keep things readable without a wall of && \:
RUN <<EOF
set -eux
apt-get update
apt-get install -y --no-install-recommends curl
rm -rf /var/lib/apt/lists/*
EOF
The # syntax=docker/dockerfile:1 line at the top is a parser directive. It tells BuildKit which Dockerfile frontend to use, so newer syntax such as heredocs works regardless of the Docker version installed.
Inherited state
A stage inherits the base image’s config: its ENV, WORKDIR, USER, ENTRYPOINT and CMD. You can check what you are inheriting before you write a line. I read the configs of a few official images through Docker Hub’s registry API: postgres:18 sets ENTRYPOINT ["docker-entrypoint.sh"], CMD ["postgres"] and STOPSIGNAL SIGINT. nginx sets STOPSIGNAL SIGQUIT. python:3.14-slim sets no entrypoint and CMD ["python3"]. Setting your own ENTRYPOINT resets an inherited CMD, a rule worth knowing before it surprises you.
About the video
The embed is Cloud Champ’s “Dockerfile creation Tutorial - Dockerfile Instructions Explained with example!”, March 2024, 26 minutes. Its chapters go instruction by instruction: the file format, FROM, RUN, WORKDIR, USER, SHELL, ENV, COPY, ADD, EXPOSE, ENTRYPOINT and CMD, and LABEL, each with a demo. It has no chapters on ARG, HEALTHCHECK, STOPSIGNAL or heredocs, which the lesson covers or links to. English was judged from the title, the description and the English caption track. The DockerCon talk in deeper is by a Docker documentation writer and builds one Dockerfile up to an optimized multi-stage build.
What was run: the instruction list, the shell and exec form rules and the COPY and ADD differences are from the Dockerfile reference vendored in the docker/docs repository, checked 2026-11-28. The postgres, nginx and python configs were fetched from Docker Hub’s registry API with curl. The example Dockerfile was written from the reference and not built, as there is no Docker here.
Where this fits
You know what each line does. Next, the two lines that decide what actually runs, and why getting them wrong makes docker stop take ten seconds: CMD, ENTRYPOINT and PID 1.
Check yourself
Your Dockerfile has ENV APP_HOME=/srv/app and then RUN ["ls", "$APP_HOME"]. The build fails with ls: $APP_HOME: No such file. Why?
The exec form runs ls directly, with no shell, so nobody expands the variable. Use the shell form RUN ls $APP_HOME, or run sh -c explicitly.
You add EXPOSE 8080 to an image and run it with docker run myimage. Can a browser on the host reach port 8080?
No. EXPOSE only records the port in the image config. Nothing is published until you run with -p 8080:8080, or -P to publish every exposed port on random host ports.
Which three instructions create filesystem layers, and why does the distinction matter?
RUN, COPY and ADD. They are the ones that change files, so they decide the image's size and are where the build cache can miss. The others only edit the config.
A base image sets WORKDIR /usr/src/app. Your Dockerfile does COPY . . without its own WORKDIR. Where do the files land, and why is that fragile?
In /usr/src/app, inherited from the base image. If the base image changes its WORKDIR, your files move with it, so set WORKDIR yourself with an absolute path.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.