Script Valley
Docker: Complete Course
Building Docker Images with DockerfileLesson 2.1

Dockerfile syntax and instructions explained

FROM, RUN, COPY, ADD, CMD, ENTRYPOINT, WORKDIR, EXPOSE, ENV, ARG

The Dockerfile: Blueprint for Your Image

Dockerfile to image build flow

A Dockerfile is a plain text file listing instructions that Docker executes top-to-bottom to produce an image. Each instruction creates a new layer.

Essential Instructions

FROM node:20-alpine          # Base image — always first
WORKDIR /app                 # Set working directory (creates it if missing)
COPY package*.json ./        # Copy dependency manifest first
RUN npm ci --omit=dev        # Install dependencies (cached layer)
COPY . .                     # Copy app source
EXPOSE 3000                  # Document which port the app uses
CMD ["node", "server.js"]    # Default command when container starts

CMD vs ENTRYPOINT

CMD provides default arguments that can be overridden at docker run. ENTRYPOINT sets the executable that always runs. Combine them for flexible containers:

ENTRYPOINT ["node"]
CMD ["server.js"]       # docker run myapp server.js (default)
                        # docker run myapp other.js  (override CMD only)

ENV vs ARG

ENV sets environment variables that persist into the running container. ARG sets build-time variables only — they are not present at runtime. Use ARG for build secrets and version pins, ENV for runtime config.

ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-alpine
ENV NODE_ENV=production

Up next

How Docker layer caching works and how to exploit it

Sign in to track progress