Skip to content
Latchkey

Dockerfile Instructions Cheat Sheet: Every Keyword Explained

Every Dockerfile instruction with the gotchas that trip people up.

Build instructions, runtime config, and multi-stage patterns.

Instructions

InstructionPurpose
FROM img AS stageBase image / named stage
RUN cmdExecute at build time (new layer)
COPY src dstCopy files (preferred over ADD)
ADD url/tar dstCOPY + auto-extract / fetch
WORKDIR /appSet working dir
ENV K=VPersistent env var
ARG KBuild-time variable
EXPOSE 8080Document port (no publish)
USER nodeDrop root
VOLUME /dataDeclare mount point
HEALTHCHECKContainer liveness probe

ENTRYPOINT vs CMD

FormBehavior
ENTRYPOINT ["app"]Fixed executable
CMD ["--flag"]Default args (overridable)
CMD ["app", "--flag"]Default command, fully overridable

Multi-stage build

Dockerfile
FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html

Key takeaways

  • Prefer COPY over ADD unless you need auto-extract or remote fetch.
  • ENTRYPOINT sets the fixed binary; CMD supplies overridable defaults.
  • Order layers least-to-most volatile to maximize cache hits.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →