I was looking through an entrypoint script for a docker image and they had the following lines (53-54)
: ${POSTGRES_USER:=postgres}
: ${POSTGRES_DB:=$POSTGRES_USER}
I saw this thread What is the purpose of the : (colon) GNU Bash builtin?
and was able to figure out :
meant true and that the :=
was used to assign default values, but it doesn't really touch upon what does :
at the beginning of the line at least for this specific case.
It's nothing, these colons are part of the command names apparently. You can verify yourself by creating and running a command with : in the name. The shell by default will autoescape them and its all perfectly legal. Follow this answer to receive notifications.
Bash and sh both use colons (":") in more than one context. You'll see it used as a separator ($PATH, for example), as a modifier (${n:="foo"}) and as a null operator ("while :").
Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.
Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a “hash-bang”, “she-bang” or “sha-bang”.
These arguments decide how the script runs inside the container. We will look into running custom shell scripts inside a Docker container with command line arguments in this guide. ENTRYPOINT: Here you will specify the command that has to be executed when the container starts. The default ENTRYPOINT command is /bin/sh -c
In particular, the script you show has a very typical pattern for an entrypoint script: If your Dockerfile names this script as its ENTRYPOINT then you want to pass the command you want to run as the “command” part. If you run your shell as just then sh will be passed to the entrypoint script, which will do the setup and then eventually run it.
Or, you can pass the ab command with the http endpoint at the end of the docker run command. Using CMD: Just add the full ab command at the end of the docker run command. It will override the whole CMD specified in the Dockerfile. Using ENTRYPOINT: You cannot override the whole ENTRYPOINT.
So, for example, if you add an option to the docker run command, it runs in the background after the executable set in the ENTRYPOINT. In addition, Docker allows you to override the ENTRYPOINT value by using the –entrypoint option during container creation. Let us illustrate how the exec form works.
In the Bourne shell and derivatives like Bash, :
is a no-op command: that is, it doesn't do anything, but arguments are evaluated normally. Contrast this with a comment (#
), which does nothing at all (everthing following the #
is simply ignored).
This syntax:
: ${POSTGRES_USER:=postgres}
Is commonly used to assign default values to variables. The syntax ${POSTGRES_USER:=postgres}
(a) will set the variable POSTGRES_USER
to postgres
only if it does not already have a value, and will then (b) evaluate to the value of $POSTGRES_USER
. If we used a comment:
# ${POSTGRES_USER:=postgres}
...nothing at all would happen, because the shell simply ignores the entire line. Using :
means that the ${...}
expression is still evaluated, so POSTGRES_USER
is assigned a value if necessary.
Update
If there was no :
at the beginning of the line, then this:
${POSTGRES_USER:=postgres}
would be a shell command! The shell would first evaluate the variable expansion and come up with something like postgres
, so you would have a shell script that effectively looked like this:
postgres
Which would probably give you the error:
bash: postgres: command not found...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With