Dockerfile: write and understand image build instructions
Learn how Dockerfile instructions create an image, how build context and cache affect the result, and how to handle startup commands and build secrets.
Dockerfile: write and understand image build instructions
A Dockerfile is a text recipe that an image builder processes in order. FROM selects a base image and begins a build stage; RUN executes during the build; COPY adds files from the build context. CMD and ENTRYPOINT define the default command when a container starts. A Dockerfile describes how to create an image, not how to configure a container that is already running.
Build stages and context
Each FROM begins a stage. Multiple stages can separate build tools from the files needed at runtime; a later stage can copy selected outputs from an earlier one. The final argument to docker build is the build context. In docker build -t local/app:1.0 ., the dot means the current directory. Local COPY paths are resolved within that context. A large context sends unnecessary files and can expose files or invalidate cache. Use .dockerignore to exclude items such as .git, node_modules, temporary output, and .env, while keeping files the build actually needs.
Follow a Node project example
Assume a Node project has package.json, package-lock.json, and src/server.js. npm ci requires the lockfile, and the server should stay in the foreground.
FROM node:lts-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY src/ ./src/
CMD ["node", "src/server.js"]FROM chooses the base image; WORKDIR sets the directory for later relative paths. Copying dependency manifests before source files can let Docker reuse the dependency-install layer when only source changes. CMD supplies the default start command. Choose a base tag and platform that match the project; consider the trade-off between receiving tag updates and pinning a digest for repeatability.
Build and inspect the image:
docker build -t local/app:1.0 .
docker image inspect local/app:1.0A successful build and expected tag/configuration confirm image creation. Verify application behavior separately by starting it and checking its logs or response. This is a documentation-based example, not a claim that it was built or run here.
RUN, CMD, and ENTRYPOINT
RUN executes while building and saves the resulting filesystem changes. For example, RUN npm ci installs dependencies into the image. CMD does not run at build time; it sets a default command or default arguments for ENTRYPOINT when a container starts. Running the application with RUN is therefore a common mistake: the builder tries to start it during the image build.
ENTRYPOINT is useful when an image should behave like a fixed executable; CMD commonly provides its default arguments. When combining them, exec-form JSON arrays help the process receive container signals directly. Shell-form ENTRYPOINT runs beneath a shell and changes argument and signal behavior. Choose the form based on whether users should replace the command or only supply arguments.
Cache and base image updates
Docker can reuse a layer when the instruction and its inputs match a prior build. A changed step can invalidate later layers, so copy stable dependency manifests and install dependencies before copying frequently changed source. This can improve cache reuse but does not guarantee it; changed inputs still require rebuilding.
Package-install RUN steps can reuse cached results. --pull checks for a newer base image; --no-cache reruns build steps without cache but does not itself fetch a fresh base. Decide how often to rebuild for security updates and whether mutable tags or pinned digests better fit your update and reproducibility policy.
Keep secrets out of the image
Do not pass passwords, API tokens, or private keys through ARG or ENV. Build arguments can appear in image history or provenance; ENV values are stored in image configuration. With a supported BuildKit setup, pass a secret using docker build --secret and consume it for one RUN instruction with RUN --mount=type=secret. Confirm that the selected builder supports the syntax.
Review checklist
Check that FROM selects the intended base and stages; COPY sources stay in context and .dockerignore excludes irrelevant or sensitive files; RUN means build-time work while CMD/ENTRYPOINT define startup; cache ordering and base update policy are deliberate; secrets use a supported secret mount; image inspect and a separate runtime check confirm the expected result.
Sources and scope
Dockerfile reference · Build best practices · Build context · Cache optimization · Build secrets
Based on Docker documentation reviewed 22 November 2026. Some syntax and options depend on Dockerfile syntax version and builder.