Dockerfile: CMD vs ENTRYPOINT

Published: (December 13, 2025 at 11:31 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Default CMD in the Ubuntu Image

The official Ubuntu image defines a default command:

CMD ["/bin/bash"]

When you run a container and provide arguments, those arguments replace the default CMD.

docker run --rm --name cmd ubuntu:latest ls /home
docker run --rm --name cmd ubuntu:latest date

Example outputs

# List contents of /home/ubuntu
docker run --rm --name cmd ubuntu:latest ls -l /home/ubuntu
total 0

# Show the current date
docker run --rm --name cmd ubuntu:latest date
Sat Dec 13 16:22:53 UTC 2025

Creating an Image with ENTRYPOINT

Below is a simple Dockerfile that sets an ENTRYPOINT script and provides a default argument via CMD.

FROM ubuntu:latest
WORKDIR /app
COPY script.sh .
RUN ["chmod", "+x", "script.sh"]
ENTRYPOINT ["/app/script.sh"]
CMD ["world"]

script.sh

#!/bin/bash
echo "Hello $1"

The script expects a single argument. If no argument is supplied at runtime, the value from CMD (world) is used.

Running the Image

Using the default argument

docker run --rm --name entrypoint entrypoint:1.0.0
# Output:
# Hello world

Overriding the argument

docker run --rm --name entrypoint entrypoint:1.0.0 december
# Output:
# Hello december

Key Takeaways

  • The CMD instruction can always be overridden by providing arguments when running a container.
  • The ENTRYPOINT itself cannot be overridden at runtime, but you can change the arguments passed to it by specifying a different CMD (or by providing arguments directly in docker run).
  • Choose between ENTRYPOINT and CMD based on the intended use case: use ENTRYPOINT for a fixed executable and CMD for default arguments or an alternative command.
Back to Blog

Related posts

Read more »