For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/dockerfile-multiple-cmd-instructions-listed.md.
A documentation index is available at /llms.txt.
Dockerfiles must contain at most one CMD instruction because only the last CMD is used at container runtime. Extra CMD instructions are ignored and can cause required startup steps or security controls to be skipped, resulting in unexpected or insecure runtime behavior.
This rule flags Dockerfile documents that include more than one CMD instruction. Ensure your Dockerfile defines zero or one CMD. If you need multiple initialization steps, combine them into a single CMD (exec form), use ENTRYPOINT for the main process and CMD for default arguments, or run a wrapper script that performs setup and then execs the main process.
Secure examples:
# single exec-form CMDCMD["nginx","-g","daemon off;"]
# ENTRYPOINT for main process, CMD for defaults/argumentsENTRYPOINT["/usr/local/bin/start.sh"]CMD["--config","/etc/app/config"]
Compliant Code Examples
FROMgolang:1.7.3WORKDIR/go/src/github.com/foo/href-counter/RUN go get -d -v golang.org/x/net/html COPY app.go .RUNCGO_ENABLED=0GOOS=linux go build -a -installsuffix cgo -o app .CMD["./app"]FROMalpine:latestRUN apk --no-cache add ca-certificatesWORKDIR/root/COPY --from=0 /go/src/github.com/foo/href-counter/app .CMD["./app"]
FROMgolang:1.16ASbuilderWORKDIR/go/src/github.com/foo/href-counter/RUN go get -d -v golang.org/x/net/html COPY app.go ./RUNCGO_ENABLED=0GOOS=linux go build -a -installsuffix cgo -o app .CMD["./app"]CMD["./apps"]FROMalpine:latestRUN apk --no-cache add ca-certificatesWORKDIR/root/COPY --from=builder /go/src/github.com/foo/href-counter/app ./CMD["./app"]RUN useradd -ms /bin/bash patrickUSERpatrick
Non-Compliant Code Examples
FROMgolang:1.7.3WORKDIR/go/src/github.com/foo/href-counter/RUN go get -d -v golang.org/x/net/html COPY app.go .RUNCGO_ENABLED=0GOOS=linux go build -a -installsuffix cgo -o app .FROMalpine:latestRUN apk --no-cache add ca-certificatesWORKDIR/root/COPY --from=0 /go/src/github.com/foo/href-counter/app .CMD["./app"]CMD["./apps"]
1
2
rulesets:- Dockerfile # Rules to enforce .
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.