简体   繁体   中英

Docker run command ignoring part of Dockerfile CMD when ENTRYPOINT present

When I run my docker container, it appears to only be honouring the first element of the CMD array (python executable) and ignoring the trailing parameters.

Dockerfile:

FROM ubuntu:14.04

ENTRYPOINT ["/bin/bash", "-c"]
CMD ["/virtualenv/bin/python", "/mycode/myscript.py", "--param1"]

Run command:

$ docker run --rm -it --volume $(pwd)/..:/mycode --volume mycontainer-virtualenv:/virtualenv mycontainer

Output:

Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Same happens if I run --detach instead of -it .

Same also happens if I run with the CMD as an overriding docker run parameter:

$ docker run --rm -it --volume $(pwd)/..:/mycode --volume mycontainer-virtualenv:/virtualenv mycontainer /virtualenv/bin/python /mycode/myscript.py --param1
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

If I run the container with bash and run the CMD from the bash prompt, it works fine:

$ docker run --rm -it --volume $(pwd)/..:/mycode --volume mycontainer-virtualenv:/virtualenv mycontainer bash
root@d6a990e81c22:/# /virtualenv/bin/python /mycode/myscript.py --param1
Hello world!

You probably want

CMD ["/virtualenv/bin/python /mycode/myscript.py --param1"]

instead of

CMD ["/virtualenv/bin/python", "/mycode/myscript.py", "--param1"]

When CMD and ENTRYPOINT are both present in Dockerfile, CMD works as default parameters to ENTRYPOINT. So you basically doing

bash -c "/virtualenv/bin/python" "/mycode/myscript.py" "--param1"

when you want

bash -c "/virtualenv/bin/python /mycode/myscript.py --param1"

https://docs.docker.com/engine/reference/builder/#cmd https://docs.docker.com/engine/reference/builder/#entrypoint https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM