Using CMD Instruction for Default Command in Dockerfile
📖 Scenario: You are creating a Docker image for a simple web server application. You want to set a default command that runs the server when the container starts.
🎯 Goal: Build a Dockerfile that sets up a base image and uses the CMD instruction to specify the default command to run the web server.
📋 What You'll Learn
Use the official python:3.12-slim base image
Create a CMD instruction that runs python3 -m http.server 8000 as the default command
💡 Why This Matters
🌍 Real World
Setting a default command in a Dockerfile is common when packaging applications so that containers start the app automatically.
💼 Career
Understanding how to use the <code>CMD</code> instruction and run containers is essential for DevOps roles managing containerized applications.
Progress0 / 4 steps
1
Create the base image
Write a Dockerfile line to use the official python:3.12-slim image as the base image with the FROM instruction.
Docker
Hint
The FROM instruction sets the base image for your Dockerfile.
2
Add the CMD instruction
Add a CMD instruction to the Dockerfile that runs python3 -m http.server 8000 as the default command when the container starts.
Docker
Hint
Use the JSON array syntax for CMD to specify the command and its arguments.
3
Build the Docker image
Write the Docker CLI command to build an image named my-web-server from the current directory.
Docker
Hint
Use docker build -t image_name . to build the image from the current folder.
4
Run the Docker container
Write the Docker CLI command to run a container from the my-web-server image, mapping port 8000 on the host to port 8000 in the container.
Docker
Hint
Use docker run -p host_port:container_port image_name to map ports and start the container.
Practice
(1/5)
1. What is the main purpose of the CMD instruction in a Dockerfile?
easy
A. To expose network ports
B. To set the default command that runs when the container starts
C. To copy files into the container
D. To define environment variables
Solution
Step 1: Understand the role of CMD
The CMD instruction specifies the default command to run inside the container when it starts.
Step 2: Differentiate from other instructions
Other instructions like COPY, EXPOSE, and ENV serve different purposes unrelated to the default command.
Final Answer:
To set the default command that runs when the container starts -> Option B
Quick Check:
CMD sets default command = C [OK]
Hint: CMD sets container's default run command [OK]
Common Mistakes:
Confusing CMD with COPY or ENV
Thinking CMD exposes ports
Assuming CMD sets environment variables
2. Which of the following is the correct exec form syntax for the CMD instruction to run nginx as the default command?
easy
A. CMD ["nginx"]
B. CMD {"nginx"}
C. CMD ("nginx")
D. CMD nginx
Solution
Step 1: Recall CMD syntax forms
Docker supports two CMD forms: shell form (string) and exec form (JSON array). The exec form is recommended for clarity and safety.
Step 2: Identify correct JSON array syntax
The correct JSON array syntax uses square brackets and double quotes: CMD ["nginx"]. Other options use invalid syntax.
Final Answer:
CMD ["nginx"] -> Option A
Quick Check:
CMD JSON array uses brackets and quotes = D [OK]
Hint: Use JSON array with brackets and quotes for CMD [OK]
Common Mistakes:
Using parentheses or curly braces instead of brackets
Omitting quotes around command
Using shell form without quotes causing errors
3. Given this Dockerfile snippet:
FROM alpine
CMD ["echo", "Hello World"]
What will be the output when you run the container without overriding the command?
medium
A. No output, container exits silently
B. echo Hello World
C. Hello World
D. Error: command not found
Solution
Step 1: Understand CMD execution
The CMD instruction runs echo with argument Hello World as the default command inside the container.
Step 2: Predict container output
Running the container executes echo "Hello World", which prints Hello World to the console.
Final Answer:
Hello World -> Option C
Quick Check:
CMD runs echo with args = Hello World [OK]
Hint: CMD JSON array runs command with args exactly [OK]
Common Mistakes:
Thinking CMD prints the command itself
Assuming no output without explicit print
Confusing CMD with ENTRYPOINT behavior
4. Identify the best practice issue in this Dockerfile snippet:
FROM ubuntu
CMD echo Hello World
medium
A. Missing quotes around Hello World
B. FROM instruction is incorrect
C. CMD cannot run echo command
D. CMD should be in JSON array form for best practice
Solution
Step 1: Check CMD syntax
The CMD instruction here uses shell form without quotes, which is allowed but less safe and less clear.
Step 2: Recommend JSON array form
Best practice is to use JSON array form like CMD ["echo", "Hello World"] to avoid shell parsing issues.
Final Answer:
CMD should be in JSON array form for best practice -> Option D
Quick Check:
Use JSON array for CMD best practice = B [OK]
Hint: Prefer JSON array form for CMD to avoid shell issues [OK]
Common Mistakes:
Thinking shell form is invalid syntax
Assuming quotes are mandatory in shell form
Believing CMD cannot run simple commands
5. You have this Dockerfile:
FROM node:18
CMD ["node", "app.js"]
You want to override the default command to run node test.js when starting the container. Which command will do this correctly?
hard
A. docker run myimage node test.js
B. docker run myimage CMD ["node", "test.js"]
C. docker run myimage --cmd node test.js
D. docker run myimage run node test.js
Solution
Step 1: Understand CMD override
When running a container, any command after the image name overrides the Dockerfile CMD instruction.
Step 2: Identify correct docker run syntax
The correct syntax is docker run myimage node test.js. Options with CMD or --cmd flags are invalid.
Final Answer:
docker run myimage node test.js -> Option A
Quick Check:
Override CMD by adding command after image name = A [OK]
Hint: Override CMD by adding command after image name in docker run [OK]
Common Mistakes:
Using CMD or --cmd flags in docker run
Placing command before image name
Using invalid docker run syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.