How to execute scripts after docker-compose up?

If you just want to initialize your db in the very first time, you can use this easy way.

    volumes:
      - [the scripts dir]:/docker-entrypoint-initdb.d

Initializing a fresh instance When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

Doc is here: https://docs.docker.com/samples/library/mysql/


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.