CMD and ENTRYPOINT commands in Dockerfile can get confusing if not tried by actually executing it.
Show, don't tell
1. CMD
By default docker images like ubuntu have CMD as last command in its dockerfile as below:
CMD ["/bin/bash"]When we run docker container with below commands
docker run --rm --name cmd ubuntu:latest ls /home
or
docker run --rm --name cmd ubuntu:latest date
Default command gets overridden by what we pass as arguments, in this cases as "ls /home" and "date"
- Above docker runs produces below outputs respectively:
~ % docker run --rm --name cmd ubuntu:latest ls -l /home/ubuntu
total 0
~ % docker run --rm --name cmd ubuntu:latest date
Sat Dec 13 16:22:53 UTC 2025
2. ENTRYPOINT
- Let's create a docker image (entrypoint:1.0.0) with ENTRYPOINT command
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"
- Above script expects a single argument
- If we don't pass any argument, the default argument will be the one provided in CMD
% docker run --rm --name entrypoint entrypoint:1.0.0
Hello world
- If we pass an argument during a docker run then
% docker run --rm --name entrypoint entrypoint:1.0.0 december
Hello december
Learnings:
- We can always override the CMD
- We cannot override ENTRYPOINT, but we can override the arguments passed to the ENTRYPOINT using CMD as last command during image creation
- ENTRYPOINT or CMD depends on usecase
Top comments (0)