Bird
Raised Fist0
Dockerdevops~10 mins

ENTRYPOINT vs CMD difference in Docker - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Process Flow - ENTRYPOINT vs CMD difference
Dockerfile Build
↓
Set ENTRYPOINT?
No→Default ENTRYPOINT: none
|Yes↓
Set CMD?
No→Default CMD: none
|Yes↓
Container Run
↓
Run ENTRYPOINT + CMD
↓
Override CMD with args?
Yes→Run ENTRYPOINT + new args
No↓
Container runs with ENTRYPOINT + CMD
Docker builds image setting ENTRYPOINT and CMD. When running container, ENTRYPOINT runs always, CMD provides default arguments. CMD can be overridden at run time.
Execution Sample
Docker
FROM alpine
ENTRYPOINT ["echo"]
CMD ["Hello, World!"]
This Dockerfile sets ENTRYPOINT to 'echo' and CMD to 'Hello, World!'. Running container prints 'Hello, World!'.
Process Table
StepActionENTRYPOINTCMDRun CommandOutput
1Build imageechoHello, World!N/AN/A
2Run container without argsechoHello, World!echo Hello, World!Hello, World!
3Run container with args 'Hi there'echoHello, World!echo Hi thereHi there
4Run container overriding ENTRYPOINT (not typical)/bin/echoHello, World!/bin/echo Hello, World!Hello, World!
5Run container with no ENTRYPOINT or CMDnonenonenoneNo command runs
💡 Container stops after running the combined ENTRYPOINT and CMD or overridden command.
Status Tracker
VariableStartAfter BuildAfter Run No ArgsAfter Run With ArgsAfter ENTRYPOINT Override
ENTRYPOINTnoneechoechoecho/bin/echo
CMDnoneHello, World!Hello, World!Hi thereHello, World!
Run CommandnoneN/Aecho Hello, World!echo Hi there/bin/echo Hello, World!
Key Moments - 3 Insights
Why does the container run 'echo Hello, World!' when no arguments are given?
Because ENTRYPOINT is set to 'echo' and CMD provides default arguments 'Hello, World!'. See execution_table row 2.
What happens if you provide arguments when running the container?
The CMD default arguments are replaced by the new arguments, but ENTRYPOINT stays the same. See execution_table row 3.
Can ENTRYPOINT be overridden at runtime like CMD?
Yes, but it is less common and requires special syntax. Overriding ENTRYPOINT changes the main command run. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the run command at step 3?
Aecho Hi there
B/bin/sh Hello, World!
Cecho Hello, World!
Dnone
💡 Hint
Check the 'Run Command' column at step 3 in the execution_table.
At which step does the ENTRYPOINT get overridden?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'ENTRYPOINT' column changes in variable_tracker and execution_table.
If CMD is not set, but ENTRYPOINT is 'echo', what happens when you run the container with no arguments?
AContainer runs default shell
BContainer fails to run
CContainer runs 'echo' with no arguments and prints nothing
DContainer runs 'echo Hello, World!'
💡 Hint
Think about how ENTRYPOINT and CMD combine; CMD provides default args if present.
Concept Snapshot
ENTRYPOINT sets the main command always run.
CMD provides default arguments to ENTRYPOINT.
CMD can be overridden at container run.
ENTRYPOINT can be overridden but less common.
Without ENTRYPOINT, CMD runs as command.
Together they define container start behavior.
Full Transcript
Docker images can define ENTRYPOINT and CMD. ENTRYPOINT is the main command that always runs when the container starts. CMD provides default arguments to ENTRYPOINT or acts as the command if ENTRYPOINT is not set. When running a container, you can override CMD by passing new arguments, but ENTRYPOINT stays unless explicitly overridden. This lets you set a fixed command with flexible arguments. The execution table shows how ENTRYPOINT and CMD combine to form the final run command and output. Overriding ENTRYPOINT changes the main command, which is less common. Understanding this helps control container startup behavior clearly.

Practice

(1/5)
1. What is the primary role of ENTRYPOINT in a Dockerfile?
easy
A. It provides default arguments for the command.
B. It specifies environment variables for the container.
C. It sets the main command that always runs when the container starts.
D. It defines the working directory inside the container.

Solution

  1. Step 1: Understand ENTRYPOINT purpose

    ENTRYPOINT defines the main command that runs every time the container starts, making it fixed.
  2. Step 2: Compare with CMD

    CMD provides default arguments or a default command only if ENTRYPOINT is not set, so it is not the main command itself.
  3. Final Answer:

    It sets the main command that always runs when the container starts. -> Option C
  4. Quick Check:

    ENTRYPOINT = main fixed command [OK]
Hint: ENTRYPOINT fixes the command, CMD sets defaults [OK]
Common Mistakes:
  • Confusing CMD as the main command
  • Thinking ENTRYPOINT sets environment variables
  • Mixing ENTRYPOINT with working directory
2. Which of the following is the correct syntax to set ENTRYPOINT in a Dockerfile?
easy
A. ENTRYPOINT = "/bin/bash -c"
B. ENTRYPOINT ["/bin/bash", "-c"]
C. ENTRYPOINT [/bin/bash, "-c"]
D. ENTRYPOINT: /bin/bash -c

Solution

  1. Step 1: Recall ENTRYPOINT syntax

    ENTRYPOINT uses JSON array syntax to specify the executable and its arguments, e.g. ["executable", "arg1"].
  2. Step 2: Identify correct option

    ENTRYPOINT ["/bin/bash", "-c"] uses the correct JSON array format. Options A, B, and D use invalid syntax for Dockerfile ENTRYPOINT.
  3. Final Answer:

    ENTRYPOINT ["/bin/bash", "-c"] -> Option B
  4. Quick Check:

    ENTRYPOINT uses JSON array syntax [OK]
Hint: Use JSON array syntax for ENTRYPOINT [OK]
Common Mistakes:
  • Using shell form without brackets for ENTRYPOINT
  • Adding equal sign or colon incorrectly
  • Using plain string without quotes
3. Given this Dockerfile snippet:
ENTRYPOINT ["echo"]
CMD ["Hello", "World"]

What will be the output when running the container without extra arguments?
medium
A. Hello World
B. echo Hello World
C. No output
D. Error: command not found

Solution

  1. Step 1: Understand ENTRYPOINT and CMD interaction

    ENTRYPOINT sets the fixed command 'echo'. CMD provides default arguments 'Hello' and 'World'.
  2. Step 2: Combine command and arguments

    Running the container runs 'echo Hello World', printing 'Hello World'.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    ENTRYPOINT + CMD = echo Hello World [OK]
Hint: ENTRYPOINT command + CMD args run together [OK]
Common Mistakes:
  • Thinking CMD overrides ENTRYPOINT command
  • Expecting full command string printed
  • Assuming no output without arguments
4. You wrote this Dockerfile:
ENTRYPOINT echo
CMD Hello World

But when you run the container, it produces unexpected output. What is the problem?
medium
A. Dockerfile must end with CMD, not ENTRYPOINT.
B. CMD cannot have multiple words without quotes.
C. ENTRYPOINT cannot be used with CMD.
D. ENTRYPOINT must be in JSON array form to avoid shell parsing errors.

Solution

  1. Step 1: Check ENTRYPOINT syntax

    ENTRYPOINT without JSON array uses shell form, but 'echo' alone may cause parsing issues or unexpected behavior.
  2. Step 2: Correct syntax requirement

    Using JSON array form like ["echo"] ensures proper command execution without shell interpretation errors.
  3. Final Answer:

    ENTRYPOINT must be in JSON array form to avoid shell parsing errors. -> Option D
  4. Quick Check:

    ENTRYPOINT needs JSON array for safety [OK]
Hint: Use JSON array for ENTRYPOINT to avoid errors [OK]
Common Mistakes:
  • Using plain string ENTRYPOINT causing errors
  • Misunderstanding CMD syntax rules
  • Thinking ENTRYPOINT and CMD cannot be combined
5. You want a Docker container that always runs python app.py, but allows users to add extra arguments like --debug. How should you write ENTRYPOINT and CMD in the Dockerfile?
hard
A. ENTRYPOINT ["python", "app.py"] and CMD ["--debug"]
B. ENTRYPOINT ["python"] and CMD ["app.py", "--debug"]
C. CMD ["python", "app.py", "--debug"] only
D. ENTRYPOINT ["python", "app.py", "--debug"] only

Solution

  1. Step 1: Fix main command with ENTRYPOINT

    Set ENTRYPOINT to ["python", "app.py"] so this command always runs.
  2. Step 2: Provide default extra arguments with CMD

    Set CMD to ["--debug"] so users can override or add arguments when running the container.
  3. Step 3: Understand combined behavior

    Docker runs ENTRYPOINT command plus CMD arguments, allowing flexibility with fixed main command.
  4. Final Answer:

    ENTRYPOINT ["python", "app.py"] and CMD ["--debug"] -> Option A
  5. Quick Check:

    ENTRYPOINT fixes command, CMD adds default args [OK]
Hint: ENTRYPOINT fixes command, CMD adds default args [OK]
Common Mistakes:
  • Putting all commands in CMD only
  • Using ENTRYPOINT without CMD for defaults
  • Mixing order causing command override
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard