61

I'm trying to understand what this Docker entrypoint does.

It seems to me that's a very common pattern when writing Dockerfiles, but my bash skills are limited and I have no idea of all the special bash symbols kung fu.

Also, it's hard to google for "--", "$!" etc. What are these called in bash world?

To summarize, what is the line bellow trying to do?

if [ "${1#-}" != "$1" ]; then
  set -- haproxy "$@"
fi
  • Also, to provide the simple answer to "what is the line below trying to do"... this allows you to pass arguments to the main process the container is going to run. It checks "does 'haproxy' not equal 'haproxy --help' for exmaple and if so, grabs those arguments and passes them along. For instance, docker run haproxy --help will pass my "--help" to haproxy inside the container. – Evan Morrison Oct 7 at 20:35
81

The set command (when not setting options) sets the positional parameters eg

$ set a b c
$ echo $1
a
$ echo $2
b
$ echo $3
c

The -- is the standard "don't treat anything following this as an option"

The "$@" are all the existing position paramters.

So the sequence

set -- haproxy "$@"

Will put the word haproxy in front of $1 $2 etc.

eg

$ echo $1,$2,$3
a,b,c
$ set -- haproxy "$@"
$ echo $1,$2,$3,$4   
haproxy,a,b,c
| improve this answer | |
  • i don't see any difference using "--" or not though. Tried here and got the same results. I'd love if you could improve the answer to highlight the difference. – Lucas Pottersky Sep 9 '16 at 15:22
  • 14
    Compare set -- -z 2 3 4 and set -z 2 3 4. The -- stops the -a being interpreted as an option. In this case it's not necessary, but it's "good practice" to get into the habit of using -- when you're note sure what is following :-) – Stephen Harris Sep 9 '16 at 16:09
  • 2
    The other main difference is that if you are sourcing a script inside of another script, using set -- $arg1 $arg2 will allow your sourced script to read the arguments, otherwise only Bash supports passing arguments directly to a sourced script (and that can be tricky to notice). – dragon788 Aug 14 '18 at 18:34
  • @StephenHarris your comment made me understand complete.! kudos – chaosguru Apr 2 at 8:36
  • 3
    @StephenHarris, I think it should be "The -- stops the -z being interpreted as an options", right? – LRDPRDX Aug 15 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.