Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colon at the beginning of line in docker entrypoint bash script [duplicate]

Tags:

bash

docker

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.

like image 709
m0meni Avatar asked Sep 02 '15 00:09

m0meni


People also ask

What is double colon in Bash?

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.

What does Colon do in bash?

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 :").

Does bash script execute sequentially?

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.

What do you call the first line in a bash script?

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”.

How to run scripts inside a docker container with command line arguments?

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

How do I run a dockerfile entrypoint script?

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.

How do I run AB in dockerfile?

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.

How to override the entrypoint value in Docker?

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.


1 Answers

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... 
like image 143
larsks Avatar answered Sep 17 '22 12:09

larsks