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 I was a little frustrated. It is not possible to either extend the directives command nor entrypoint.

Finally I came up with the following solution. I added this as file to /usr/local/bin/entrypoint-wrapper.

set -e

if [ -z "$CI" ]; then
    # if not in CI pipeline

    if [ $# -gt 0 ]

    exec docker-php-entrypoint apache2-foreground
    exec docker-php-entrypoint "$@"

Then I created a custom Image like the following:

FROM php:apache 
COPY entrypoint-wrapper /usr/local/bin/ 
RUN chmod 775 /usr/local/bin/entrypoint-wrapper 
ENTRYPOINT [ "entrypoint-wrapper" ]

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.


  • 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 *

Scroll to Top