Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker CMD weirdness when ENTRYPOINT is a shell script

Here's a simple Dockerfile

FROM centos:6.6
ENTRYPOINT ["/bin/bash", "-l", "-c"]
CMD ["echo", "foo"]

Unfortunately it doesn't work. Nothing is echo'd when you run the resulting container that's built.
If you comment out the ENTRYPOINT then it works. However, if you set the ENTRYPOINT to /bin/sh -c, then it fails again

FROM centos:6.6
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["echo", "foo"]

I thought that was the default ENTRYPOINT for an container that didn't have one defined, why didn't that work?

Finally, this also works

FROM centos:6.6
ENTRYPOINT ["/bin/bash", "-l", "-c"]
CMD ["echo foo"]

Before I submit an issue, I wanted to see if I'm doing something obviously wrong?
I'm using rvm inside my container which sort of needs a login shell to work right.

like image 606
SeafoodBuffet Avatar asked Oct 19 '22 00:10

SeafoodBuffet


1 Answers

Note that the default entry point/cmd for an official centos 6 image is:

  • no entrypoint
  • only CMD ["/bin/bash"]

If you are using the -c command, you need to pass one argument (which is the full command): "echo foo".
Not a series of arguments (CMD ["echo", "foo"]).

As stated in dockerfile CMD section:

If you use the shell form of the CMD, then the <command> will execute in /bin/sh -c:

FROM ubuntu
CMD echo "This is a test." | wc -

If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable

Since echo is a built-in command in the bash and C shells, the shell form here is preferable.

like image 82
VonC Avatar answered Oct 21 '22 22:10

VonC