Common DockerFile directives:
Estimated reading: 4 minutes 52 views

🐳 DockerFile CMD Instruction – The Complete Guide with Examples & Best Practices

🧲 Introduction – Why Learn the CMD Instruction in Docker?

When building Docker images, defining the default behavior of your container is crucial. The CMD instruction in a Dockerfile allows you to specify what command should run when a container startsβ€”unless overridden at runtime.

It’s a key component for scripting lightweight, reusable containers. Whether you’re launching a Python app, running a web server, or testing a CLI tool, CMD makes it easy to automate default actions.

🎯 In this article, you’ll learn:

  • What CMD does and how it works
  • Differences between Shell form, Exec form, and combined use with ENTRYPOINT
  • How to override CMD at runtime
  • Real-world use cases and best practices

πŸ“Œ What is the CMD Instruction in a DockerFile?

The CMD instruction provides default arguments or commands that Docker executes when a container is started without specifying a command.

🧠 Important Note:
If you provide a command during docker run, it overrides the CMD.


πŸ” Syntax of the CMD Instruction

Docker supports three forms of CMD:

βœ… 1. Shell Form

CMD echo "Hello, World"
  • Executes via shell: /bin/sh -c
  • Supports shell features like &&, variables, redirection

βœ… 2. Exec Form (Recommended)

CMD ["echo", "Hello, World"]
  • Executes directly without a shell
  • More secure and predictable
  • Preferred for most use cases

βœ… 3. Parameter Form (Used with ENTRYPOINT)

ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]
  • CMD supplies default arguments to the ENTRYPOINT
  • Easily overridden with runtime parameters

πŸ’‘ Example: CMD in Action

🧾 Dockerfile:

FROM python:3.11
COPY app.py /app.py
CMD ["python", "/app.py"]

▢️ Build & Run:

docker build -t python-app .
docker run python-app

βœ… The container runs /app.py by default using the CMD line.


πŸ†š CMD vs ENTRYPOINT – Key Differences

FeatureCMDENTRYPOINT
PurposeDefault arguments or commandMain command
Overridableβœ… Yes🚫 Not by default
Common UsageApp execution, default scriptsLong-running services or core logic
TogetherYes, CMD provides args to ENTRYPOINTENTRYPOINT runs regardless

πŸ”„ CMD Override at Runtime

docker run python-app echo "Overridden CMD"

πŸ” This completely overrides the Dockerfile CMD.


πŸ“¦ Real-World Use Case: Running Nginx

FROM nginx:alpine
CMD ["nginx", "-g", "daemon off;"]

βœ… Keeps the Nginx server running in the foreground by default.


🧰 DockerFile CMD Command – Functional Table

πŸ› οΈ FunctionπŸ“ DescriptionπŸ“Œ Example
1. Set default commandDefines what to run at container startCMD ["python", "app.py"]
2. Shell form usageUses shell for executionCMD echo "Hello World"
3. Exec form usageDirect command without shellCMD ["echo", "Hello"]
4. Default args for ENTRYPOINTWorks with ENTRYPOINTENTRYPOINT ["nginx"] CMD ["-g", "daemon off;"]
5. Overridable at runtimeCustom command replaces CMDdocker run myimage ls -l
6. One CMD per DockerfileOnly last CMD is usedCMD ["last one wins"]
7. ENV variable supportSupports ENV in shell formCMD echo "User: $USER"
8. Runs at container startExecutes on docker rundocker run myimage
9. Fallback behaviorActs as default if no command is passedCMD ["npm", "start"]
10. Useful for testingRun ping, echo, sleep, etc.CMD ["ping", "google.com"]
11. PositioningPlace CMD at the end of DockerfileCMD ["start.sh"]
12. Affects exit codeA failing CMD affects container statusCMD ["false"] results in code 1
13. Works with ENTRYPOINTCan be paired for flexibilityUse --entrypoint to override ENTRYPOINT
14. Runs scriptsCan launch a shell scriptCMD ["./start.sh"]
15. Ignored if overriddenCMD is skipped if custom command givendocker run myimage bash

🧡 Final Thoughts

The CMD instruction is your way of telling Docker, “If the user doesn’t specify anything, this is what I want the container to do.”

βœ… Use CMD for flexible defaults
βœ… Combine it with ENTRYPOINT for more control
βœ… Always test behavior using docker run


❓ FAQs – DockerFile CMD Command

❓ 1. Can I use multiple CMD instructions in one DockerFile?
🚫 No. Only the last CMD will be used.


❓ 2. When should I use CMD over ENTRYPOINT?
βœ… Use CMD when you want to set a default that can be overridden. Use ENTRYPOINT when the command should always run.


❓ 3. Can I use both CMD and ENTRYPOINT together?
βœ… Yes, and it’s a common best practice. ENTRYPOINT defines the command; CMD defines the default arguments.


❓ 4. How do I override CMD at runtime?
▢️ Provide a new command at the end:

docker run image-name custom-command

❓ 5. What happens if I don’t use CMD or ENTRYPOINT?
πŸ“­ The container starts and exits immediately unless a command is passed during docker run.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

DockerFile CMD

Or Copy Link

CONTENTS
Scroll to Top