pkaramol
pkaramol

Reputation: 19422

docker-entrypoint best practice for script execution

I want to run a couple of scripts on my docker-entrypoint.sh;

My question if whether it makes any difference and if it does, what is the recommended way of going about this, regarding the following options:

A.

${HOMEDIR}/myscript --param1 --param2

B.

bash -c "${HOMEDIR}/myscript --param1 --param2"

C.

source ${HOMEDIR}/myscript --param1 --param2

Upvotes: 3

Views: 1585

Answers (1)

teoman
teoman

Reputation: 969

It actually depends on what you are doing and what are you trying to do.

${HOMEDIR}/myscript --param1 --param2

This one will execute the script. When the script is done, any changes that it made to the environment are discarded.

bash -c "${HOMEDIR}/myscript --param1 --param2"

Running bash -c "my command here" vs. just running my command here is primarily different in that the former starts a subshell and the latter runs the commands in the current shell.

There are a number of differences in the effects, however:

  • Changes to the environment made in the subshell cannot affect the parent shell (current directory, values of environment variables, function definitions, etc.)

  • Variables set in the parent shell that has not been exported will be unavailable in the subshell.

Here is my reference since I did not know much about bash -c

source ${HOMEDIR}/myscript --param1 --param2

When you call source (or its alias .), you insert the script in the current bash process. So you could read variables set by the script.

When you call sh, you initiate a fork (sub-process) that runs a new session of /bin/sh, which is usually a symbolic link to bash. In this case, environment variables set by the sub-script would be dropped when the sub-script finishes.

Also here my reference.

TL;DR: If you do not want the bash to keep the changes you want with that scripts you will be running on, I recommend you to use (A). If you want the bash to keep the variables and changes, use (C). If you want to keep the changes and make the bash run the script on another bash, use (B) as I listed the differences between them.

Upvotes: 4

Related Questions