Close

A way to enable startup scripts for a Docker Container

While I was setting up a new Image for running a app in a Rancher (v1.6) Cluster I had a real annoying problem.

I’m using a php:apache to run a default Laravel app.  The CI/CD Pipeline will deploy all stages like dev, master and feature branches on separate instances. When a Container will be deployed Laravel should also migrate its Database automatically. For this I wanted to run  php artisan migrate after it’s start.

The simplest solution was to add a command: php artisan migrate to my docker-compose.yml based on the image.

After my first try to start the container with docker-compose up  docker-compose up  I was a little frustrated. It is not possible to either extend the directives command nor entrypoint.

Finally I came up with this solution:

I added this as file to /usr/local/bin/entrypoint-wrapper.

Then I created a custom Image like the following:

This makes it possible to add the  command: php artisan migrate directive to the docker-compose.yml.

The wrapper will run the given command then run the default entrypoint with the default argument as define in the base Image https://github.com/docker-library/php/blob/master/7.2/stretch/apache/Dockerfile#L252.

Notice: 

  • At the time the command is executed, Apache has not yet started.
  • This will be executed for each container start, so don’t run commands that can’t be multiple times.

(Internally it will run https://github.com/docker-library/php/blob/master/7.2/stretch/apache/docker-php-entrypoint runs with parameter https://github.com/docker-library/php/blob/master/7.2/stretch/apache/apache2-foreground.)

UPDATE: I changed the script to never start apache in foreground if its executed in a CI/CD Pipeline. Otherwise the Job will run till the run timeout is reached.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

copyright by okaufmann.ch