How to execute scripts after docker-compose up?

You can use ENTRYPOINT or CMD in your Dockerfile to execute a command when the container starts. The difference between them is that ENTRYPOINT is executed any time the container is started, while CMD might be replaced with an command line option. Assuming the command you want to execute is X

docker run my-image Y

will execute X if ENTRYPOINT X was in the Dockerfile and Y if CMD X was in the Dockerfile.

However, there are two caveats:

  1. The command will be executed every time the container is started.
  2. After the command terminates the container is shut down.

Therefore, a typical solution is to have a docker-entrypoint script. It checks whether it is run in a fresh container initiating its environment and afterwards executes the container’s actual program.
Have a look at the official mysql Dockerfile and entrypoint to get an idea.

An example entrypoint script could look like this:

$ cat docker_entrypoint.sh                                                                                                                                          
if [ ! -f .initialized ]; then                                                                                                                                                                                    
    echo "Initializing container"                                                                                                                                                                                 
    # run initializing commands                                                                                                                                                                                   
    touch .initialized                                                                                                                                                                                            
fi                                                                                                                                                                                                                

exec "$@"

First, it checks whether there is a file called .initialized. If there is none, some commands are run to initialize the container environment. After which touch .initialized creates .initialized as an empty file. Therefore, subsequent container starts won’t execute the initialization command again.
Secondly, it starts the actual service. Doing this with exec will replace the shell process with the service’s process. Hence, docker will keep the container running until the service terminates. "$@" will contain the “container/image command”. This is set with CMD X in the Dockerfile and is overriden on the command, as I already pointed out above. By using exec "$@" you will be able to start different programs in the container for inspection, e.g. bash, and start the service by default, as specified in the Dockerfile’s CMD statement.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)