bashdockerdockerfiledocker-entrypoint

Docker Entrypoint script not taking any arguments in?


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?


Solution

  • 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 practice 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"]
    

    Will not work as you would expect.


    You might be tempted to do something like so:

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

    However, 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