0
0
Dockerdevops~15 mins

CMD instruction for default command in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use docker run -p host_port:container_port image_name to map ports and start the container.