0

I am working in the following Dockerfile:

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive \
    apt-get install -y \
      curl \
      apache2 \
      php5 \
      php5-cli \
      libapache2-mod-php5 \
      php5-gd \
      php5-json \
      php5-mcrypt \
      php5-mysql \
      php5-curl \
      php5-memcached \
      php5-mongo \
      zend-framework

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
    chown www-data /usr/local/bin/composer && composer --version

# Install usefull PHP tools
RUN composer global require sebastian/phpcpd && \
    composer global require phpmd/phpmd && \
    composer global require squizlabs/php_codesniffer

# Install xdebug after we install composer since it cause issues
# see https://getcomposer.org/doc/articles/troubleshooting.md#xdebug-impact-on-composer
RUN apt-get install -y php5-xdebug

As you may notice this install PHP 5.5.x and it comes with the default configuration which I would like to override with my own values.

I have the following directory structure:

docker-php55/
├── container-files
│   ├── config
│   │   └── init
│   │       └── vhost_default
│   └── etc
│       └── php.d
│           ├── zz-php-directories.ini
│           └── zz-php.ini
├── Dockerfile
├── LICENSE
├── README.md
└── run

The files zz-php-directories.ini and zz-php.ini are my configurations that I should write to /etc/php5/apache2/php.ini upon image creation. The content of the files is the following:

zz-php.ini
; Basic configuration override
expose_php = Off
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = UTC
max_execution_time = 120

; Error reporting
display_errors = stderr
display_startup_errors = Off
error_reporting = E_ALL

; A bit of performance tuning
realpath_cache_size = 128k

; OpCache tuning
opcache.max_accelerated_files = 32000
; Temporarily disable using HUGE PAGES by OpCache.
; This should improve performance, but requires appropriate OS configuration
; and for now it often results with some weird PHP warning:
; PHP Warning:  Zend OPcache huge_code_pages: madvise(HUGEPAGE) failed: Invalid argument (22) in Unknown on line 0
opcache.huge_code_pages=0

; Xdebug
[Xdebug]
xdebug.remote_enable = true
xdebug.remote_host   = "192.168.3.1" // this IP should be the host IP
xdebug.remote_port   = "9001"
xdebug.idekey        = "XDEBUG_PHPSTORM"

zz-php-directories.ini
; Configure temp path locations
sys_temp_dir = /data/tmp/php
upload_tmp_dir = /data/tmp/php/uploads
session.save_path = /data/tmp/php/sessions

uploadprogress.file.contents_template = "/data/tmp/php/upload_contents_%s"
uploadprogress.file.filename_template = "/data/tmp/php/upt_%s.txt"

How do I override the default php.ini parameters on the image with the ones on those files upon image creation?

EDIT: Improve the question

To leave an example, zz-php.ini is a local file placed in my laptop|PC. As soon as I install PHP in the image it comes with a default configuration file, this mean I should have a file under /etc/php5/apache2/php.ini.

This default configuration file already has default values like for example: expose_php = On (again this is the default, others comes as ;realpath_cache_size =) so what I want to do is to change the value for the default file with the value from my file, in other words:

default (as in /etc/php5/apache2/php.ini) expose_php = On
override (as in zz-php.ini) expose_php = Off

At the end I should have the values from zz-php.ini overwrited in /etc/php5/apache2/php.ini

As for the host IP address I think I could use a ENV var and pass to the build as an argument, I am right? If no, then how would you get the host IP address needed for that setup?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363

1 Answers1

1

That's two questions.

1) Just use the COPY instruction to copy your local php.ini into the image location. Eg:

COPY php.ini /etc/php5/apache2/php.ini

2) You don't want to hardcode any ip into your image. That needs to be done when the container is started. The standard way of doing this with docker is to specify an environment variable like HOST_IP and you use a shell script to make the modifications on the container at start time. For instance:

Your inject.sh script:

#!/usr/bin/bash
sed -i -E "s/xdebug.remote_host.*/xdebug.remote_host=$HOST_IP/" /etc/php5/apache2/php.ini

You need to add the inject.sh file to your image when you build it.

COPY inject.sh /usr/local/bin/

Then you can initialize and start your container as follow:

docker run -e HOST_IP=53.62.10.12 mycontainer bash -c "inject.sh && exec myphpapp"

The exec is needed to make sure the myphpapp becomes the main process of the container (ie: it has PID 1) otherwise it won't receive kill commands (like Ctrl-C).

Bernard
  • 16,149
  • 12
  • 63
  • 66
  • Well, I don't want to `copy` the `zz-php.ini` on `/etc/php5/apache2/php.ini` I want to change default values for `/etc/php5/apache2/php.ini` with the ones provide in `zz-php.ini`, is that what `COPY` does? – ReynierPM Sep 23 '16 at 13:14
  • Your `zz-php.ini` file is a file that you have locally right? (on your laptop let's say). The COPY will copy it to the image from your laptop. If you want to move/rename a file that is already in the image, then you need to perform the operation in a RUN statement. See https://docs.docker.com/engine/reference/builder/#/copy – Bernard Sep 23 '16 at 13:21
  • I think I am not following you or you are not understand me, see my edit on OP please and thank you – ReynierPM Sep 23 '16 at 13:32
  • I understand that you want to override the default php.ini with the zz-php.ini on your local machine. As I wrote in my answer you do that using the COPY statement in your Dockerfile, just as I wrote above. As for the Host IP, you should specify it at run time, that is when your container is created, not when you build your image. I gave you a sample code to do that above. – Bernard Sep 23 '16 at 13:40
  • No, and no, I don't want to override the whole default `php.ini` with `zz-php.ini` I want to override some values from default `php.ini` with values from `zz-php.ini` the difference is file vs values, understand now? – ReynierPM Sep 23 '16 at 13:43
  • ok, that certainly wasn't clear to me. You want to replace **some lines** of the default php.ini with those in your zz-php.ini. You can do it at run time (container startup) using the same technique I listed for the inject.sh. Or you can do it at build time (in your Dockerfile) by COPY'ing your zz-php.ini file in your image and using a RUN statement to perform the merging. The merging would have to be done by a custom bash script. – Bernard Sep 23 '16 at 13:50
  • Exactly, "some lines" this is what I want and the technique should works although can you add a simple example for the lines replacing to your answer? Thank you again – ReynierPM Sep 23 '16 at 13:51
  • That's a different question although a quick search tells me that the default.ini file settings are overwritten by any `.ini` local files. So you should only have to copy your `.ini` file at the correct dir location. See this for instance. http://stackoverflow.com/questions/2899326/overwrite-php-ini-settings – Bernard Sep 23 '16 at 13:57
  • Okey, thanks that might works I will accept your answer – ReynierPM Sep 23 '16 at 13:59