Comments (#)
How to write comments in Docker configuration files. In a Dockerfile, # is the only comment syntax available. However, inline comments (writing a comment after code on the same line) are not supported — comments must be placed at the start of a line. Also note that # syntax= and # escape= at the very top of a Dockerfile are not comments but "parser directives" — special instructions that change how the Dockerfile is interpreted.
Syntax
# Dockerfile: comment — everything from # to the end of the line is a comment. # Write at the start of a line. # For multi-line comments, put # at the start of each line. # Dockerfile has no multi-line comment syntax like /* */. # Parser directives (not comments — they change how the Dockerfile is interpreted): # # syntax=docker/dockerfile:1.4 # # escape=` FROM ubuntu:22.04 # Inline comments are not supported. The # below is at the start of a line only. RUN apt-get update # This causes an error (inline comments are not allowed)
Comment Types and Notes
| Type | Syntax | Description |
|---|---|---|
| Comment | # text | A line beginning with # is treated as a comment. A leading space before # is also valid. |
| Inline comment (not supported) | RUN command # comment | Inline comments are not available in Dockerfile. Writing RUN apt-get update # comment passes # comment to the shell as part of the command (which usually causes an error). |
| Parser directive | # key=value | A special instruction written at the very top of the Dockerfile. It uses the same syntax as a comment but has a different meaning. Once a blank line or a build instruction appears, any subsequent # key=value lines are no longer recognized as parser directives. |
Parser Directives
Parser directives are special instructions written at the very top of a Dockerfile using the same # key=value syntax as comments. They modify the behavior of the parser that processes the Dockerfile.
| Directive | Description |
|---|---|
| # syntax=docker/dockerfile:1.4 | Specifies the BuildKit frontend syntax version to use. Required when using newer features such as here-document syntax (<<EOF). |
| # escape=` | Changes the line-continuation character (default is \). Sometimes switched to a backtick (`) on Windows so that \ can be used in path names. |
Parser directives must be placed at the very top of the file. If a blank line, a regular comment, or a build instruction (such as FROM) appears before them, they are not recognized as parser directives and are treated as ordinary comments.
Patterns for Placing Comments
| Decision | Situation | Reason |
|---|---|---|
| Write a comment | Why this base image was chosen | Leaving a note about why a version was pinned, why Alpine was chosen, or why a specific OS is needed provides a reference when updating the image later. |
| Write a comment | Complex RUN instructions | When multiple commands are chained with &&, explaining the overall intent in a preceding comment improves readability. |
| Write a comment | The meaning of port numbers or environment variables | Adding a note explaining what an EXPOSE or ENV value means gives future maintainers a reference when making changes. |
| Often becomes redundant | Logic that is obvious from the instruction | Writing # Install Python before RUN pip install... is a pattern where the comment and the instruction tend to overlap. |
| No comment needed | Change history | Because version control (git) is available, there is no need to record change dates in comments. |
Sample Code
Basic Dockerfile with comments.
Dockerfile
# syntax=docker/dockerfile:1.4 # Parser directive: enables BuildKit 1.4+ syntax. # Required when using here-document syntax. # Base image: using the slim variant of Python 3.12. # The slim variant omits debugging tools, making it suitable for production. FROM python:3.12-slim # Set metadata. LABEL maintainer="team@example.com" LABEL description="Sample Python application" # Set the working directory. WORKDIR /app # Copy and install dependencies first. # Copying only requirements.txt first allows the pip install layer # to be cached even when the application source code changes. COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy the application source code. COPY . . # Declare the port the application uses (actual port publishing is done with docker run -p). EXPOSE 8000 # Default command executed when the container starts. CMD ["python", "main.py"]
Dockerfile (multi-stage build example)
# Example of a multi-stage build. # Separating the build image from the runtime image keeps the final image small. # ---- Build stage ---- # golang:1.22 is a large image that includes the full toolchain. # It is used only for compilation and is not included in the final image. FROM golang:1.22 AS builder WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . # Disable CGO to produce a statically linked binary. # Required to run on the distroless image. RUN CGO_ENABLED=0 go build -o server . # ---- Runtime stage ---- # distroless is a minimal image that does not even include a shell. # Used to minimize the attack surface. FROM gcr.io/distroless/static-debian12 WORKDIR /app # Copy only the binary produced in the build stage. COPY --from=builder /app/server . EXPOSE 8080 CMD ["/app/server"]
Common Mistakes
Writing inline comments after a RUN instruction
Inline comments are not supported in Dockerfile. Writing RUN command # comment passes # comment to the shell as part of the command. Some shells silently ignore the # and everything after it, but the behavior is not guaranteed and can cause unexpected results. Always place comments on the line before the instruction.
# NG: inline comment — # onward is passed as part of the command RUN apt-get update # update package list # OK: place the comment on the line before the instruction # Update the package list and install curl. RUN apt-get update && apt-get install -y curl
Placing parser directives after other content
Parser directives must be placed at the very top of the file. If a blank line, a regular comment, or a build instruction (such as FROM) appears first, any # syntax= etc. written afterward is not recognized as a parser directive and is treated as an ordinary comment.
# NG: a parser directive written after FROM is ignored. FROM ubuntu:22.04 # syntax=docker/dockerfile:1.4 ← invalid here (treated as a regular comment) # OK: place it at the very top of the file. # syntax=docker/dockerfile:1.4 FROM ubuntu:22.04
Overview
The only comment syntax available in a Dockerfile is #. A line beginning with # is treated as a comment. Unlike many other languages, Dockerfile does not support inline comments (writing a comment after code on the same line). Writing # comment at the end of a RUN instruction passes that text to the shell as part of the command. Comments must always be placed on the line before the instruction they describe.
# syntax= and # escape= at the very top of a Dockerfile use the same syntax as comments but are parser directives — special instructions interpreted by Docker BuildKit before parsing the rest of the file. They change the syntax version or the escape character. If a blank line or build instruction appears before them, they are no longer recognized as parser directives, so they must be placed at the very beginning of the file.
If you find any errors or copyright issues, please contact us.