Skip to main content
⚑ OpenDevDocs
Docker
devopsdockerGuideBeginner

Dockerfile Reference & Instructions

Complete reference for writing clean, efficient, and secure Dockerfiles using standard instructions like FROM, WORKDIR, COPY, RUN, CMD, and ENTRYPOINT.

Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#dockerfile#devops#containers

A Dockerfile is a plain text recipe containing sequential instructions that Docker uses to automatically build a container image.


Core Dockerfile Instructions

InstructionPurposeExample
FROMSets the base image for subsequent instructionsFROM node:22-alpine
WORKDIRSets the active working directory inside the imageWORKDIR /app
COPYCopies files/directories from host to image filesystemCOPY package*.json ./
ADDLike COPY, but supports auto-extracting tar archives and URLsADD archive.tar.gz /data/
RUNExecutes commands during image build time (creates a layer)RUN npm ci --only=production
ENVSets persistent environment variablesENV NODE_ENV=production
EXPOSEDocuments network ports the application listens onEXPOSE 3000
VOLUMEDefines a mount point for external data persistenceVOLUME ["/var/lib/data"]
USERSets the non-root user UID/GID for subsequent commandsUSER node
CMDDefault command and arguments executed when container runsCMD ["node", "dist/index.js"]
ENTRYPOINTConfigures a container to run as an executableENTRYPOINT ["docker-entrypoint.sh"]

Standard Dockerfile Example

# 1. Choose minimal base image
FROM node:22-alpine

# 2. Set working directory
WORKDIR /usr/src/app

# 3. Copy package manifests first for optimal layer caching
COPY package.json package-lock.json ./

# 4. Install production dependencies
RUN npm ci --omit=dev

# 5. Copy remaining application source code
COPY . .

# 6. Set unprivileged user for security
USER node

# 7. Expose documentation port
EXPOSE 3000

# 8. Start application with exec form
CMD ["node", "src/server.js"]

Shell Form vs. Exec Form

Instructions like RUN, CMD, and ENTRYPOINT support two syntax forms:

JSON array syntax that invokes the binary directly without spawning a shell:

CMD ["node", "server.js"]
  • Passes Linux signals (SIGTERM, SIGINT) directly to the process (PID 1).
  • Allows graceful container shutdown.

2. Shell Form

Raw string parsed and executed through /bin/sh -c:

CMD node server.js
  • Spawns /bin/sh as PID 1, which may swallow termination signals and prevent clean exit.

ENTRYPOINT vs CMD

  • ENTRYPOINT: Defines the fixed command that is always executed.
  • CMD: Supplies default parameters to ENTRYPOINT or acts as default command if no ENTRYPOINT is defined.
ENTRYPOINT ["node"]
CMD ["server.js"]

Overriding at runtime:

docker run my-image app.js # Executes: node app.js

On this page