Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused with CMD and ENTRYPOINT in dockerfile

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?

like image 826
laoqiren Avatar asked Apr 01 '17 03:04

laoqiren


Video Answer


1 Answers

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:

  • if you have to pass any argument after mongod, you don't have to type mongod first when using docker run: it will be added for you in docker-entrypoint.sh
  • if you don't have any argument to pass after mongod, you don't have to type mongod either: CMD will provide it for you to the ENTRYPOINT docker-entrypoint.sh.
like image 108
VonC Avatar answered Oct 12 '22 23:10

VonC