π§Ύ Dockerfile Command Reference
Each command in a Dockerfile defines a specific instruction for how to build a Docker image. Hereβs a detailed breakdown:
1οΈβ£ FROM
β Set the Base Image ποΈ
FROM node:20-alpine
β
What it does:
Defines the base image your custom image will build on top of.
π Must be the first instruction in the Dockerfile (except
ARG
sometimes).
2οΈβ£ WORKDIR
β Set Working Directory π
WORKDIR /app
β
What it does:
Sets the working directory inside the container where all subsequent commands (COPY
, RUN
, etc.) will execute.
π Automatically creates the directory if it doesnβt exist.
3οΈβ£ COPY
β Copy Files into the Image π¦
COPY package*.json ./ COPY . .
β
What it does:
Copies files/directories from your host into the container filesystem.
π‘ Use
.dockerignore
to exclude unnecessary files.
4οΈβ£ RUN
β Execute a Shell Command π οΈ
RUN npm install RUN apk add --no-cache curl
β
What it does:
Runs a command (like installing packages, cleaning up files) at build time, and creates a new layer.
π‘ Combine multiple commands to reduce image size:
RUN apk add --no-cache curl && npm install
5οΈβ£ CMD
β Default Command to Run on Container Start π
CMD ["npm", "start"]
β
What it does:
Specifies the default command that gets run when the container starts.
π‘ Only one
CMD
allowed β if multiple, only the last is used.
6οΈβ£ EXPOSE
β Document the Appβs Port π
EXPOSE 8000
β
What it does:
Informs Docker that the app runs on a specific port β used for documentation and port binding.
π‘ Does not actually publish the port (use
-p
indocker run
).
7οΈβ£ ENV
β Set Environment Variables π§ͺ
ENV PORT=8000 ENV NODE_ENV=production
β
What it does:
Defines environment variables inside the container, usable by the app or shell.
π‘ Use
process.env.PORT
in Node.js.
8οΈβ£ ARG
β Build-Time Variables βοΈ
ARG NODE_VERSION=20-alpine FROM node:${NODE_VERSION}
β
What it does:
Defines variables that you can pass during docker build
with --build-arg
.
π Unlike
ENV
,ARG
is not available during runtime.
9οΈβ£ ENTRYPOINT
β Hard-Coded Command π
ENTRYPOINT ["node", "index.js"]
β
What it does:
Defines the main executable β like CMD
, but less override-able.
π‘ Combine with
CMD
for arguments:
ENTRYPOINT ["npm"] CMD ["start"]
π LABEL
β Metadata About the Image π·οΈ
LABEL maintainer="Darshan Vasani" LABEL version="1.0"
β
What it does:
Adds metadata like author, version, description to your image.
π‘ Useful for scanning tools and image documentation.
π VOLUME
β Define Persistent Storage Volumes πΎ
VOLUME ["/app/data"]
β
What it does:
Creates a mount point with a persistent volume at build/run time.
π‘ Avoid putting app logic here; use it for data.
π USER
β Switch to a Non-root User π
RUN adduser -D myuser USER myuser
β
What it does:
Sets the user under which the container runs (for security best practices).
π‘ Donβt run production apps as
root
.
π§ͺ BONUS: Multi-Stage Build Example π§
# Stage 1: Build FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . # Stage 2: Runtime FROM node:20-alpine WORKDIR /app COPY --from=builder /app . CMD ["npm", "start"]
β Why?
- Keep final image clean
- Remove dev dependencies
- Build-only stuff stays out of final image
β Summary Table
Command | Purpose |
---|---|
FROM | Set base image |
WORKDIR | Set working directory |
COPY | Copy files into image |
RUN | Execute shell commands at build time |
CMD | Run command on container start |
EXPOSE | Document listening port |
ENV | Set environment variables |
ARG | Pass build-time variables |
ENTRYPOINT | Set main executable |
LABEL | Add metadata to image |
USER | Switch to non-root user |
VOLUME | Declare persistent storage |
Top comments (0)