Best practice: When your Docker container runs as a daemon, write entrypoint.sh
and run it when container starts by ENTRYPOINT
.
CMD
can be overwritten.
Suppose you want to run a command when the Docker container starts.
In that case, you can use CMD
.
The following code runs the command sleep 5
when the container starts.
FROM Ubuntu
CMD sleep 5
# or
# CMD ["sleep", "5"]
The arguments of CMD
can be replaced when its run time command by:
docker run ubuntu-sleeper sleep 10
ENTRYPOINT
can run a command when the container starts as well as CMD
:
FROM Ubuntu
ENTRYPOINT ["sleep"]
Example usage:
docker run ubuntu-sleeper 5 # sleep + 5
If no argument is passed as an argument of the run
command, it returns error because sleep has no operand.
Let’s make this sleep time 5 as variable, and the default command as sleep
:
FROM Ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]
Now the default value is 5, but you can overwrite the ENTRYPOINT and CMD as follows:
docker run ubuntu-sleeper # sleep + 5
docker run ubuntu-sleeper 10 # sleep + 10
docker run --entrypoint sleep2.0 ubuntu-sleeper 10 #override entrypoint command