Docker Entrypoint script not taking any arguments in?

262 Views Asked by At

Whilst developing my Dockerfile, I ran into an issue with the Entrypoint instruction script; it's not taking my docker run arguments in.

Here is an example of what I tried, given:

COPY ./entrypoint.sh /entrypoint.sh  
RUN chmod +x /entrypoint.sh  
ENTRYPOINT /entrypoint.sh

With the following entrypoint.sh:

#!/bin/bash
echo "I received:" "$@"

When I run the command:

docker run given/image:latest "A B C"

I expected the output:

I received: A B C

But instead, it was:

I received:

Am I missing something obvious here?

1

There are 1 best solutions below

4
u-ways On BEST ANSWER

You're using the shell form of the ENTRYPOINT instruction. From the documentation:

The shell form:

ENTRYPOINT command param1 param2

The shell form prevents any CMD or run command line arguments from being used.

Whilst in practise it's possible to hack your way around this by amending your instruction to:

ENTRYPOINT /entrypoint.sh "$0" "$@"

However, I would recommend using the exec form as it passes Unix signals. (i.e. SIGTERM; docker stop <container)

Which is a simple syntax change:

ENTRYPOINT ["/entrypoint.sh"]

The only inconvenience you run into when using the exec form is the fact that it will not do variable substitution so something like:

ENTRYPOINT ["$HOME/entrypoint.sh"]

Might not work as you would expect. Instead, you could do something like:

ENTRYPOINT ["sh", "-c", "$HOME/entrypoint.sh"]

(Do note that you will still run into the same Unix signals problem if you wrapped your command with sh -c. Thanks @David Maze)

For further details, see: Dockerfile reference - exec form ENTRYPOINT example