What is the purpose of the CMD instruction in a Dockerfile?
CMD sets the default command and arguments for a container. It can be overridden when running the container.
Click to reveal answer
beginner
What does the ENTRYPOINT instruction do in a Dockerfile?
ENTRYPOINT sets the main command that always runs when the container starts. It is not easily overridden.
Click to reveal answer
intermediate
How do ENTRYPOINT and CMD work together in a Dockerfile?
ENTRYPOINT defines the command to run, and CMD provides default arguments to that command. CMD arguments can be overridden at runtime.
Click to reveal answer
intermediate
If a Dockerfile has both ENTRYPOINT and CMD, and you run the container with extra arguments, what happens?
The extra arguments replace CMD arguments but are passed to ENTRYPOINT command. ENTRYPOINT command always runs.
Click to reveal answer
beginner
Which instruction is easier to override when running a Docker container: ENTRYPOINT or CMD?
CMD is easier to override by providing new arguments when running the container. ENTRYPOINT is harder to override.
Click to reveal answer
What happens if you specify both ENTRYPOINT and CMD in a Dockerfile?
ADocker throws an error
BCMD runs as the command, ENTRYPOINT is ignored
CBoth ENTRYPOINT and CMD run simultaneously
DENTRYPOINT runs as the command, CMD provides default arguments
✗ Incorrect
ENTRYPOINT sets the main command, and CMD provides default arguments to it.
Which Dockerfile instruction can be overridden by passing arguments to 'docker run'?
ANeither ENTRYPOINT nor CMD
BCMD
CBoth ENTRYPOINT and CMD
DENTRYPOINT
✗ Incorrect
CMD arguments can be replaced by arguments passed to 'docker run'. ENTRYPOINT is not replaced.
If ENTRYPOINT is set to '/bin/bash' and CMD is set to '-c "echo Hello"', what command runs by default?
A/bin/bash -c "echo Hello"
BOnly /bin/bash
COnly echo Hello
DDocker will fail to run
✗ Incorrect
ENTRYPOINT is the command, CMD provides default arguments.
How can you override ENTRYPOINT when running a container?
AUse the --entrypoint flag with 'docker run'
BPass new arguments after the image name
CModify CMD in the Dockerfile
DYou cannot override ENTRYPOINT
✗ Incorrect
The --entrypoint flag lets you override ENTRYPOINT at runtime.
Which instruction is best to use if you want your container to always run a specific command?
ACMD
BRUN
CENTRYPOINT
DENV
✗ Incorrect
ENTRYPOINT ensures the command always runs when the container starts.
Explain the difference between ENTRYPOINT and CMD in Dockerfiles.
Think about which one is the main command and which one provides default options.
You got /4 concepts.
Describe how ENTRYPOINT and CMD work together when both are used in a Dockerfile.
Consider how the container runs the command with arguments.
You got /4 concepts.
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
Step 1: Understand ENTRYPOINT purpose
ENTRYPOINT defines the main command that runs every time the container starts, making it fixed.
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.
Final Answer:
It sets the main command that always runs when the container starts. -> Option C
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
Step 1: Recall ENTRYPOINT syntax
ENTRYPOINT uses JSON array syntax to specify the executable and its arguments, e.g. ["executable", "arg1"].
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.
Final Answer:
ENTRYPOINT ["/bin/bash", "-c"] -> Option B
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
Step 1: Understand ENTRYPOINT and CMD interaction
ENTRYPOINT sets the fixed command 'echo'. CMD provides default arguments 'Hello' and 'World'.
Step 2: Combine command and arguments
Running the container runs 'echo Hello World', printing 'Hello World'.
Final Answer:
Hello World -> Option A
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
Step 1: Check ENTRYPOINT syntax
ENTRYPOINT without JSON array uses shell form, but 'echo' alone may cause parsing issues or unexpected behavior.
Step 2: Correct syntax requirement
Using JSON array form like ["echo"] ensures proper command execution without shell interpretation errors.
Final Answer:
ENTRYPOINT must be in JSON array form to avoid shell parsing errors. -> Option D
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
Step 1: Fix main command with ENTRYPOINT
Set ENTRYPOINT to ["python", "app.py"] so this command always runs.
Step 2: Provide default extra arguments with CMD
Set CMD to ["--debug"] so users can override or add arguments when running the container.
Step 3: Understand combined behavior
Docker runs ENTRYPOINT command plus CMD arguments, allowing flexibility with fixed main command.
Final Answer:
ENTRYPOINT ["python", "app.py"] and CMD ["--debug"] -> Option A