Php: Add init scripts execution

Created on 30 Nov 2017  Â·  8Comments  Â·  Source: docker-library/php

I often need to run some scripts before daemon startup and much simpler than overriding entrypoint will be to just COPY my script to image and this script will be automatically executed.

Something like this in docker-php-entrypoint:

  #!/bin/sh
  set -e

+ # Execute all init scripts
+ if [ -d /init.d ]; then
+   for f in /init.d/*.sh; do
+       bash "$f"
+   done
+ fi
+ 
  # first arg is `-f` or `--some-option`
  if [ "${1#-}" != "$1" ]; then
    set -- apache2-foreground "$@"
  fi

  exec "$@"

Then custom Dockerfile can contain something like this:

COPY scripts/wait_for_db.sh /init.d/1_wait_for_db.sh

Is PR wellcome?

Most helpful comment

The initdb support in images like MariaDB and MySQL was added specifically because the functionality provided there can't be duplicated in a Dockerfile, docker run, or compose yaml easily. This addition is something that is possible in all three cases.

Compose examples:

version: "3"

services:
  web:
    image: php:7.2-apache-stretch
    volumes:
      - ./scripts/:/init-scripts/
    entrypoint:
      - bash
      - -c
      - |
        set -e
        echo 'running prestart script'
        /init-scripts/prestart.sh

        echo 'running init.php'
        php -f /init-script/some/init.php

        echo 'initialization done, starting apache'
        exec apache2-foreground

# alternatively
  web2:
    image: php:7.2-apache-stretch
    volumes:
      - ./scripts/:/init-scripts/
    command:
      - /init-scripts/custom-script.sh
# where the custom-script.sh can run other scripts in the folder
# and then end with "exec apache2-foreground"

A dockerfile:

FROM php:7.2-apache-stretch
COPY ./scripts/ /init-scripts/
CMD /init-scripts/custom-script.sh
# where the custom-script.sh can run other scripts in the folder
# and then end with "exec apache2-foreground"

On a docker run line:

$ docker run -d -v "$PWD"/scripts/:/init-scripts/ php:7.2-apache-stretch /init-scripts/custom-script.sh

Similar/related issues: https://github.com/docker-library/rabbitmq/issues/5, https://github.com/docker-library/wordpress/issues/147

All 8 comments

Do we have any other use cases then just waiting for a socket?

Yes, I use it for db initialization and schema upgrade also. Lately used something similar for conditional enabling xdebug extension.

The initdb support in images like MariaDB and MySQL was added specifically because the functionality provided there can't be duplicated in a Dockerfile, docker run, or compose yaml easily. This addition is something that is possible in all three cases.

Compose examples:

version: "3"

services:
  web:
    image: php:7.2-apache-stretch
    volumes:
      - ./scripts/:/init-scripts/
    entrypoint:
      - bash
      - -c
      - |
        set -e
        echo 'running prestart script'
        /init-scripts/prestart.sh

        echo 'running init.php'
        php -f /init-script/some/init.php

        echo 'initialization done, starting apache'
        exec apache2-foreground

# alternatively
  web2:
    image: php:7.2-apache-stretch
    volumes:
      - ./scripts/:/init-scripts/
    command:
      - /init-scripts/custom-script.sh
# where the custom-script.sh can run other scripts in the folder
# and then end with "exec apache2-foreground"

A dockerfile:

FROM php:7.2-apache-stretch
COPY ./scripts/ /init-scripts/
CMD /init-scripts/custom-script.sh
# where the custom-script.sh can run other scripts in the folder
# and then end with "exec apache2-foreground"

On a docker run line:

$ docker run -d -v "$PWD"/scripts/:/init-scripts/ php:7.2-apache-stretch /init-scripts/custom-script.sh

Similar/related issues: https://github.com/docker-library/rabbitmq/issues/5, https://github.com/docker-library/wordpress/issues/147

I do not see a big difference with MySQL - also there you were able to override command and run something before execution (Inc temporary deamon what listens only to local socket).

In both cases it will make this things easier and cleaner.


    1. 2017 v 2:58, yosifkit notifications@github.com:

The initdb support in images like MariaDB and MySQL was added specifically because the functionality provided there can't be duplicated in a Dockerfile, docker run, or compose yaml easily. This addition is something that is possible in all three cases.

Compose examples:

version: "3"

services:
web:
image: php:7.2-apache-stretch
volumes:
- ./scripts/:/init-scripts/
entrypoint:
- bash
- -c
- |
set -e
echo 'running prestart script'
/init-scripts/prestart.sh

    echo 'running init.php'
    php -f /init-script/some/init.php

    echo 'initialization done, starting apache'
    exec apache2-foreground

alternatively

web2:
image: php:7.2-apache-stretch
volumes:
- ./scripts/:/init-scripts/
command:
- /init-scripts/custom-script.sh

where the custom-script.sh can run other scripts in the folder

and then end with "exec apache2-foreground"

A dockerfile:

FROM php:7.2-apache-stretch
COPY ./scripts/ /init-scripts/
CMD /init-scripts/custom-script.sh

where the custom-script.sh can run other scripts in the folder

and then end with "exec apache2-foreground"

On a docker run line:

$ docker run -d -v "$PWD"/scripts/:/init-scripts/ php:7.2-apache-stretch /init-scripts/custom-script.sh
Similar/related issues: docker-library/rabbitmq#5, docker-library/wordpress#147

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@iBobik well actually I personally think this logic goes in the daemon it self. Mainly because you can have a thread dedicated to exposing health checks and metrics even whilst your daemon is in "starting" state.

Is there a good reason why this 5 lines can not be added into docker-php-entrypoint?

For most cases it means overhead just 100 bytes bigger image and one condition execution. Compare it with current list of pre installed PHP extensions what almost nobody use all of them.

@iBobik it is something that would need mantaining. This is just a plain PHP image.

@iBobik we install an absolute minimal set of extensions by default :wink: (hence why docker-php-ext-* exist to facilitate users installing their own additional extensions, including those extensions various dependencies -- see https://github.com/docker-library/php/issues/75 for a fairly long discussion on that topic)

I'm definitely also of the opinion that this feature is overkill for this particular image -- as noted by @yosifkit above, images like PostgreSQL and MySQL support this precisely because it's difficult to accomplish by default.

Here, it's as easy as adding a couple lines to the image, or even simply using bash -c 'php /..../script.php && php /..../script2.php && exec apache2-foreground', etc as a command at runtime, so it's trivial for users to supply this functionality themselves. :heart:

(and this thread will serve as a searchable reference for several approaches to do exactly that :+1:)

Was this page helpful?
0 / 5 - 0 ratings