Bird
Raised Fist0
Dockerdevops~5 mins

ENTRYPOINT vs CMD difference in Docker - CLI Comparison

Choose your learning style10 modes available
Introduction
When running a Docker container, you often want to specify what program runs first. ENTRYPOINT and CMD both set this, but they work differently. Understanding their difference helps you control how your container starts and behaves.
When you want to set a fixed main command that always runs in your container.
When you want to provide default arguments that users can override when running the container.
When you want to combine a fixed command with flexible arguments provided at runtime.
When you want to create reusable images with a default behavior but allow customization.
When you want to avoid users accidentally replacing the main command by mistake.
Config File - Dockerfile
Dockerfile
FROM alpine:3.18

# Set ENTRYPOINT to always run the echo command
ENTRYPOINT ["echo"]

# Set CMD to provide default arguments to echo
CMD ["Hello, Docker!"]

FROM alpine:3.18 sets the base image.

ENTRYPOINT ["echo"] means the container will always run the echo command first.

CMD ["Hello, Docker!"] provides default arguments to echo, which can be overridden when running the container.

Commands
Builds the Docker image named entrypoint-cmd-example using the Dockerfile in the current directory.
Terminal
docker build -t entrypoint-cmd-example .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/4 : FROM alpine:3.18 3.18: Pulling from library/alpine Digest: sha256:... Status: Downloaded newer image for alpine:3.18 ---> a24bb4013296 Step 2/4 : ENTRYPOINT ["echo"] ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789def012345 Step 3/4 : CMD ["Hello, Docker!"] ---> Running in 456def789abc Removing intermediate container 456def789abc ---> 0123456789ab Successfully built 0123456789ab Successfully tagged entrypoint-cmd-example:latest
Runs the container from the image. It uses ENTRYPOINT as the command and CMD as the default argument.
Terminal
docker run entrypoint-cmd-example
Expected OutputExpected
Hello, Docker!
Runs the container but overrides CMD arguments with 'Goodbye, World!'. ENTRYPOINT remains the same.
Terminal
docker run entrypoint-cmd-example Goodbye, World!
Expected OutputExpected
Goodbye, World!
Overrides ENTRYPOINT to run 'ls' command instead of 'echo', and passes '-l' as argument.
Terminal
docker run --entrypoint ls entrypoint-cmd-example -l
Expected OutputExpected
bin etc lib media mnt proc root sys tmp usr var
→
--entrypoint - Overrides the ENTRYPOINT command for this run
Key Concept

ENTRYPOINT sets the fixed command that always runs, while CMD provides default arguments that can be overridden when starting the container.

Common Mistakes
Using CMD when you want a fixed command that should never be replaced.
CMD can be overridden by arguments passed to docker run, so the main command might change unexpectedly.
Use ENTRYPOINT to set the main command and CMD to set default arguments.
Writing ENTRYPOINT and CMD as strings instead of JSON arrays.
String form runs the command in a shell which can cause unexpected behavior and harder argument passing.
Always use JSON array syntax like ["executable", "arg1", "arg2"] for ENTRYPOINT and CMD.
Expecting CMD to run alone without ENTRYPOINT.
CMD only provides default arguments; without ENTRYPOINT, CMD runs as the command itself but can be overridden.
Use CMD alone only if you want a default command that can be replaced easily.
Summary
ENTRYPOINT sets the main command that always runs in the container.
CMD provides default arguments to ENTRYPOINT or a default command if ENTRYPOINT is not set.
Arguments passed to docker run override CMD but not ENTRYPOINT unless --entrypoint is used.

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