0
0
Dockerdevops~20 mins

ENTRYPOINT vs CMD difference in Docker - Hands-On Comparison

Choose your learning style9 modes available
Understanding ENTRYPOINT vs CMD in Docker
📖 Scenario: You are creating a Docker container for a simple application. You want to learn how ENTRYPOINT and CMD work differently when running containers.
🎯 Goal: Build a Dockerfile step-by-step to see how ENTRYPOINT and CMD behave differently when you run the container with or without extra commands.
📋 What You'll Learn
Create a Dockerfile with a base image
Add an ENTRYPOINT instruction
Add a CMD instruction
Run the container to see the output difference
💡 Why This Matters
🌍 Real World
Dockerfiles are used to create container images that package applications and their environments. Understanding ENTRYPOINT and CMD helps control how containers start.
💼 Career
DevOps engineers and developers use Dockerfiles daily. Knowing ENTRYPOINT vs CMD is essential for building flexible and predictable container images.
Progress0 / 4 steps
1
Create a Dockerfile with base image
Create a file named Dockerfile with the first line setting the base image to alpine.
Docker
Need a hint?

The base image line starts with FROM followed by the image name.

2
Add an ENTRYPOINT instruction
Add an ENTRYPOINT instruction to the Dockerfile that runs echo.
Docker
Need a hint?

Use JSON array syntax for ENTRYPOINT: ENTRYPOINT ["echo"].

3
Add a CMD instruction
Add a CMD instruction to the Dockerfile that provides the default text Hello from CMD.
Docker
Need a hint?

Use JSON array syntax for CMD: CMD ["Hello from CMD"].

4
Run the container and observe output
Run the Docker container built from this Dockerfile without extra commands using docker run --rm your_image_name. Then run it with extra command docker run --rm your_image_name Goodbye. Write print statements showing the expected outputs for both runs.
Docker
Need a hint?

When running without extra command, output is Hello from CMD. When running with Goodbye, output is Goodbye.