45

I am creating an image from another image that set a specific entrypoint. However I want my image to have default one. How do I reset the ENTRYPOINT?

I tried the following Dockerfile:

FROM some-image
ENTRYPOINT ["/bin/sh", "-c"]

Unfortunately it doesn't work like the default entrypoint as it need the command to be quoted.

docker run myimage ls -l /    # "-l /" arguments are ignored
file1 file2 file3             # files in current working directory

docker run myimage "ls -l /"  # works correctly

How do I use commands without quoting?

Eugene Dounar
  • 1,134
  • 1
  • 10
  • 10

3 Answers3

63

To disable an existing ENTRYPOINT, set an empty array in your docker file

ENTRYPOINT []

Then your arguments to docker run will exec as a shell form CMD would normally.

The reason your ENTRYPOINT ["/bin/sh", "-c"] requires quoted strings is that without the quotes, the arguments to ls are being passed to sh instead.

Unquoted results in lots of arguments being sent to sh

"/bin/sh", "-c", "ls", "-l", "/"

Quoting allows the complete command (sh -c) to be passed on to sh as one argument.

"/bin/sh", "-c", "ls -l /"
Matt
  • 68,711
  • 7
  • 155
  • 158
  • Are you sure it's not because `-c` causes `sh` to only pick up one argument? I'm using `bash`, not `sh`, and removing `-c` from the ENTRYPOINT lets it pick up later arguments. – Mohan Aug 09 '18 at 17:35
  • @Mohan I'm not sure what you mean? The `-c` argument is what `sh` and `bash` will run, by design that's a single argument. The quotes make it a single argument, otherwise anything afterwards is ignored – Matt Aug 31 '18 at 21:28
  • You helped OP solve his problem but didn't really explained why `ENTRYPOINT ["/bin/sh", "-c"]` is not the same as `ENTRYPOINT []` whereas they are both supposed to be the same as `/bin/sh -c` is the default. – DevAb Nov 07 '21 at 22:47
  • @DevAb The image default is no entry point, which is what `[]` equates to. That is not the same as the entry point of `/bin/sh -c` which was set somewhere in an image build by OP (and why I use the term "To disable") – Matt Nov 07 '21 at 23:29
  • @Matt I've just read [this answer](https://stackoverflow.com/a/21564990/10634206) stating that `/bin/sh -c` is the default entrypoint... Maybe the default entrypoint acts just like if you would open a terminal under sh as the default shell that is ready to receive a command which is a bit different than running a command with `/bin/sh -c` ? – DevAb Nov 09 '21 at 23:55
  • 1
    @DevAb That's the default way a `CMD` is launched and slightly different to a specified `ENTRYPOINT`. If you use the shell form of `CMD words` the `words` will be passed to `/bin/sh -c "words"`. The exec form `CMD [ "exec" ]` will not use `sh`. – Matt Nov 10 '21 at 00:17
  • Setting `ENTRYPOINT []` lets you go back to relying on that `CMD` logic. – Matt Nov 10 '21 at 00:17
  • [Here's where the builder decides to prepend a shell to the command or not](https://github.com/moby/moby/blob/33a3680e08d1007e72c3b3f1454f823d8e9948ee/builder/dockerfile/dispatchers.go#L442) – Matt Nov 10 '21 at 00:59
5

This isn't really related to docker. Try running the following:

/bin/sh -c echo foo

/bin/sh -c "echo foo"

The -c means that /bin/sh only picks up one argument. So removing the -c from the entrypoint you define should fix it. This is more flexible than resetting the entry point; e.g. you can do this to use Software Collections:

ENTRYPOINT ["scl", "enable", "devtoolset-4", "--", "bash"]

Mohan
  • 7,302
  • 5
  • 32
  • 55
1

Note: beware of ENTRYPOINT [].

As mentioned in moby/moby issue 3465 ("Reset properties inherited from parent image"), Brendon C. notes:

Looks like ENTRYPOINT [] and ENTRYPOINT [""] both invalidate the cache on each build when not using BuildKit.

Simple Dockerfile to demonstrate:

FROM jrottenberg/ffmpeg:4.3-alpine311 as base

ENTRYPOINT []

RUN echo "HERE!"

Steps 2 and 3 will never use cache. This is my workaround:

FROM jrottenberg/ffmpeg:4.3-alpine311 as base

ENTRYPOINT ["/usr/bin/env"]

RUN echo "HERE!"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250