How Do I Make a Comment in a Dockerfile?

TLDR
To add comments in a Dockerfile, use the # symbol. Comments are ignored during the build process and are useful for documenting your Dockerfile.
Comments in Dockerfiles are essential for documenting your code, explaining instructions, and improving readability. This guide will show you how to use comments effectively in Dockerfiles.
Use the # Symbol
In Dockerfiles, comments start with the # symbol. Anything following the # on the same line is ignored by Docker during the build process.
Example
# Use the official Node.js image as the base image
FROM node:18
# Set the working directory inside the container
WORKDIR /app
# Copy application files into the container
COPY . .
# Install dependencies
RUN npm install
# Start the application
CMD ["node", "app.js"]
Explanation
# Use the official Node.js image as the base image: Documents the purpose of theFROMinstruction.# Set the working directory inside the container: Explains theWORKDIRinstruction.# Copy application files into the container: Describes theCOPYinstruction.# Install dependencies: Notes the purpose of theRUNinstruction.# Start the application: Clarifies theCMDinstruction.
Best Practices
- Be Concise: Write short and clear comments.
- Explain Complex Instructions: Use comments to explain non-obvious instructions.
- Avoid Redundancy: Do not comment on instructions that are self-explanatory.
- Keep Comments Updated: Ensure comments reflect the current state of the Dockerfile.
By following these practices, you can make your Dockerfiles more readable and maintainable.
Related Resources
- Difference Between RUN and CMD in a Dockerfile: choose the right instruction
- Docker Build Requires 1 Argument: fix common build errors
- Docker Security Best Practices: write secure Dockerfiles
- Introduction to Docker Guide: learn Docker from scratch
- Docker Quiz: test your Docker knowledge
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Docker Terminal Simulator
Practice Docker commands in an interactive browser terminal. Learn images, containers, logs, exec, Dockerfile builds, volumes, networks, and Docker Compose with a live Docker daemon visualization.
How Docker Works Under the Hood
Watch what really happens when you run docker run -p 8080:80 nginx, one layer at a time. Step down the whole stack: the CLI, the daemon, the registry pull, containerd, the OCI runtime bundle, runc, the running container, and the shared Linux kernel. Every stage shows the real low-level command you can run yourself, so it doubles as a tour of the primitives that make a container: namespaces, cgroups, and runc. A container is not a small VM, and this shows you why.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorTags
Found an issue?
Related Posts
Also worth your time on this topic
COPY with Docker but with Exclusion
Learn how to use the COPY instruction in Docker with exclusion patterns to optimize your Docker builds.
Dockerfile Best Practices
What is the difference between a Docker image and a container? What are some Dockerfile best practices?
junior
Docker Multi-Stage Build Optimization
Learn to create efficient Docker images using multi-stage builds to reduce image size and improve security.
60 minutes