DEV Community

Darshan Vasani
Darshan Vasani Subscriber

Posted on • Edited on

🧾 Dockerfile Command Reference

🧾 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 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 . . 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 
Enter fullscreen mode Exit fullscreen mode

5️⃣ CMD β€” Default Command to Run on Container Start 🏁

CMD ["npm", "start"] 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 in docker run).


7️⃣ ENV β€” Set Environment Variables πŸ§ͺ

ENV PORT=8000 ENV NODE_ENV=production 
Enter fullscreen mode Exit fullscreen mode

βœ… 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} 
Enter fullscreen mode Exit fullscreen mode

βœ… 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"] 
Enter fullscreen mode Exit fullscreen mode

βœ… What it does:
Defines the main executable β€” like CMD, but less override-able.

πŸ’‘ Combine with CMD for arguments:

ENTRYPOINT ["npm"] CMD ["start"] 
Enter fullscreen mode Exit fullscreen mode

πŸ”Ÿ LABEL β€” Metadata About the Image 🏷️

LABEL maintainer="Darshan Vasani" LABEL version="1.0" 
Enter fullscreen mode Exit fullscreen mode

βœ… 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"] 
Enter fullscreen mode Exit fullscreen mode

βœ… 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 
Enter fullscreen mode Exit fullscreen mode

βœ… 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"] 
Enter fullscreen mode Exit fullscreen mode

βœ… 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)