I have a simple nginx container trying to run from docker-compose..
version: "3.3"
services:
nginx:
image: nginx
privileged: true
entrypoint: ["/bin/sh -c"]
command: ["ls -lha ~"]
but it fails with:
docker-compose up -d
ERROR: for junk_nginx_1 Cannot start service nginx: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh -c": stat /bin/sh -c: no such file or directory: unknown
I thought it was because /bin/sh doesn’t exists in the image, but it certainly does. removing the -c
gives me the following error:
# this time the container runs, this is in the container logs.
/bin/sh: 0: Can't open ls -lha ~
so /bin/sh
does exists within the image. what am I doing wrong?
2
Answers
See entrypoint usage:
Also see command usage:
So, your error means your syntax not ok, the correct one for you is:
The execution:
When you use the array form of Compose
command:
andentrypoint:
(and, similarly, the JSON-array form of DockerfileCMD
,ENTRYPOINT
, andRUN
), you are responsible for breaking up the input into words. Each item in the array is a word, just as though it was quoted in the shell, and includes any spaces, punctuation, and other characters.So when you say
That is one word, not a command and its argument, and you are telling the shell to look for an executable program named
sh -c
(including space hyphen c as part of the filename) in the/bin
directory. Since that’s not there, you get an error.You shouldn’t usually need to override
entrypoint:
in a Compose setup. In your case, the only shell expansion you need is the home directory~
but that’s not well-defined in Docker. You should be able to just writeor in array form
or if you really need the
sh -c
wrapperYou’re using the stock Docker Hub
nginx
image; also consider whetherdocker-compose run
might be an easier way to run a one-off commandIf it’s your own image, try hard to avoid needing to override
ENTRYPOINT
. MakeCMD
be a complete command; if you need anENTRYPOINT
, a shell script that ends withexec "$@"
so that it runs theCMD
is a typical pattern.