I have known that in the dockerfile, when ENTRYPOINT and CMD are both provided, the values in CMD will be as default parameter of ENTRYPOINT, then when running a container with additional parameter, the additional parameter will replace the values of CMD in dockerfile.
but when I see the dockerfile of mongodb, I'm confused, the entrypoint and cmd part of its dockerfile:
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 27017
CMD ["mongod"]
I know when I run docker run mongo:latest
this will be docker-entrypoint.sh mongod
, but when I run docker run mongo:latest --auth
, why could it run correctly? Won't it be docker-entrypoint.sh --auth
? since the --auth
will replace mongod
, so why I run docker run mongo --auth
but it works fine?
The mongod Dockerfile references docker-entrypoint.sh
.
That script docker-entrypoint.sh
starts by testing if the first parameter begins with '-
' (as in '--auth
'):
if [ "${1:0:1}" = '-' ]; then
set -- mongod "$@"
fi
So the arguments become mongod --auth
, not just --auth
.
Meaning:
mongod
, you don't have to type mongod
first when using docker run
: it will be added for you in docker-entrypoint.sh
mongod
, you don't have to type mongod
either: CMD
will provide it for you to the ENTRYPOINT
docker-entrypoint.sh
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With