| title | Learn Dockerfile CMD Instructions Practically |
|---|---|
| description | Understand how to use the CMD instruction in Dockerfiles, and how to override CMD during the 'docker run' command. |
In this guide, you will:
- Create an Nginx Dockerfile with the
CMDinstruction. - Understand how to override the
CMDinstruction during thedocker runcommand. - Build the Docker image and verify its functionality.
- Directory:
DockerFiles
Create a Dockerfile with the following content:
# Use nginx:alpine-slim as base Docker Image
FROM nginx:alpine-slim
# OCI Labels
LABEL org.opencontainers.image.authors="Kalyan Reddy Daida"
LABEL org.opencontainers.image.title="Demo: CMD Instruction in Docker"
LABEL org.opencontainers.image.description="A Dockerfile demo illustrating the use of the CMD instruction"
LABEL org.opencontainers.image.version="1.0"
# Copy a custom index.html to the Nginx HTML directory
COPY index.html /usr/share/nginx/html
# Default CMD to start Nginx in the foreground
CMD ["nginx", "-g", "daemon off;"]Create a simple index.html:
<!DOCTYPE html>
<html>
<body style='background-color:rgb(227, 213, 180);'>
<h1>Welcome to StackSimplify - CMD Dockerfile Instruction</h1>
<p>Learn technology through practical, real-world demos.</p>
<p>Application Version: V1</p>
<p>CMD: Specify default commands.</p>
</body>
</html># Change to the directory containing your Dockerfile
cd DockerFiles
# Build the Docker Image
docker build -t [IMAGE_NAME]:[TAG] .
# Example:
docker build -t demo10-dockerfile-cmd:v1 .
# Run the Docker Container
docker run --name my-cmd-demo1 -p 8080:80 -d demo10-dockerfile-cmd:v1
# Verify Nginx is running inside the container
docker exec -it my-cmd-demo1 ps aux
# Expected Output:
# You should see the Nginx process running with 'nginx: master process nginx -g daemon off;'
# Access the application in your browser
http://localhost:8080Observations:
- The Nginx process should be running inside the container.
- The
CMD ["nginx", "-g", "daemon off;"]defined in the Dockerfile is executed as-is.
# Run Docker Container by overriding the CMD instruction
docker run --name my-cmd-demo2 -it demo10-dockerfile-cmd:v1 /bin/sh
# Run inside container ps aux
ps aux
# Expected Output:
# Nginx is not running because the CMD has been overridden with '/bin/sh'
# You can start Nginx manually if desired:
nginx -g 'daemon off;'
# To exit the container shell:
exitObservations:
- After connecting to the Docker container, Nginx is not running.
- The
CMDinstruction has been overridden with/bin/shduringdocker run.
# Stop and remove the containers
docker rm -f my-cmd-demo1
docker rm -f my-cmd-demo2
# Remove the Docker images from local machine
docker rmi [DOCKER_USERNAME]/[IMAGE_NAME]:[TAG]
docker rmi [IMAGE_NAME]:[TAG]
# Example:
docker rmi demo10-dockerfile-cmd:v1
# List Docker Images to confirm removal
docker imagesYou have successfully:
- Created an Nginx Dockerfile using the
CMDinstruction. - Built the Docker image and ran it, observing the default
CMDexecution. - Overridden the
CMDinstruction duringdocker runand verified its effect. - Tagged and pushed the Docker image to Docker Hub.
-
CMD Instruction:
- The
CMDinstruction specifies the default command to run when starting a container from the image. - It can be overridden by specifying a different command during
docker run. - Only the last
CMDinstruction in the Dockerfile takes effect.
- The
-
Overriding CMD:
- When you specify a command at the end of the
docker runcommand, it overrides theCMDspecified in the Dockerfile. - This is useful when you want to run different commands using the same image.
- When you specify a command at the end of the
-
Best Practices:
- Use
CMDto specify the default command for the container. - Use
ENTRYPOINTwhen you want to define a fixed command and allow additional parameters. - Avoid using both
ENTRYPOINTandCMDunless necessary.
- Use
- Docker Documentation
- Dockerfile Reference - CMD Instruction
- Best Practices for Writing Dockerfiles
- Understanding CMD and ENTRYPOINT in Docker
Happy Dockerizing!