4

I have the following at the end of my Dockerfile:

ENTRYPOINT bash -C '/usr/local/bin/setup_php_settings';'bash'

If I am not wrong that /usr/local/bin/setup_php_settings will be executed each time the container is started. If so then I have a few installation stuff (like this ZRay for example) inside that script that I would like to move to another script that would be executed just once on image build process let's say.

The content of the setup_php_settings (whithout the ZRay part) is the following:

#!/bin/bash -x
set -e

PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL & ~E_DEPRECATED & ~E_NOTICE"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/cli/php.ini

mkdir -p /data/tmp/php/uploads
mkdir -p /data/tmp/php/sessions
mkdir -p /data/tmp/php/xdebug
chown -R www-data:www-data /data/tmp/php*

ln -sf /etc/php5/mods-available/zz-php.ini /etc/php5/apache2/conf.d/zz-php.ini
ln -sf /etc/php5/mods-available/zz-php-directories.ini /etc/php5/apache2/conf.d/zz-php-directories.ini
ln -sf /usr/share/php/libzend-framework-php/Zend/ /usr/share/php/Zend
a2enmod rewrite
php5enmod mcrypt

# Apache gets grumpy about PID pre-existing files
: "${APACHE_PID_FILE:=${APACHE_RUN_DIR:=/var/run/apache2}/apache2.pid}"
rm -f "$APACHE_PID_FILE"

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND "$@"

The question is, how do I execute such new script during image build? Using CMD? Any other workaround?

1 Answer 1

5

The correct directive for this is RUN. This would run the script on image build. in your case -

RUN /usr/local/bin/setup_php_settings
CMD bash
Sign up to request clarification or add additional context in comments.

4 Comments

Not so fast, I have added the content of the setup_php_settings file and maybe you want to re-check your answer :) I would say based on your answer that will be better to have a separated RUN command for the installation stuff, what do you think?
All of the steps in your script, except for maybe the last line should be run during image build (i.e a separate RUN command). The last one looks more like something I would put inside a CMD command in my dockerfile, Although I would substitute the source part with ENV commands in the dockerfile (assuming this file only sets env vars)
Ok, can you add an example of your possible setup? Sorry I am still learning Docker :)
This is kinda of a different question than "how to run a script on image creation". The short answer would be - run 2 containers, one for apache (you can use their official image - hub.docker.com/_/httpd) and one for your php app, and link them together using the --link flag. it's a bit advanced but if you want to dive into docker I recommend starting building your apps ready for container work right from the start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.