Docker-Compose won't volume my php.ini file
Asked Answered
S

3

9

I'm trying to use docker-compose to volume my php.ini file so I can make changes on the fly on my local machine to see how it affects the host machine. Unfortunately the only way I've been able to get the php.ini file into the container is directly during creation in the Dockerfile so far.

Attached is an image of the container running fine with the current settings below.

enter image description here

My Dockerfile is below:

FROM ubuntu:14.04
MAINTAINER Joe Astrahan <[email protected]>

VOLUME ["/var/www"]


RUN apt-get update && \
    apt-get install -y software-properties-common && \
    apt-get update && \
    apt-get install -y \
      apache2 \
      curl \
      libcurl3 \
      libcurl3-dev \
      php5 \
      php5-cli \
      libapache2-mod-php5 \
      php5-gd \
      php5-json \
      php5-ldap \
      php5-mysqlnd \
      php5-pgsql \
      php5-curl \
      mysql-client

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

# install php-5.5.30
COPY config/install_php-5.5.30.sh /tmp/install_php-5.5.30.sh
RUN /bin/bash /tmp/install_php-5.5.30.sh


COPY config/apache_default.conf /etc/apache2/sites-available/000-default.conf
COPY config/run /usr/local/bin/run

RUN chmod +x /usr/local/bin/run
RUN a2enmod rewrite

#This will allow us to modify files in the container for testing if we need to
RUN apt-get update && \
    apt-get install -y vim

EXPOSE 80
CMD ["/usr/local/bin/run"]

My docker-compose.yml file is below:

version: '2'
services:
    dblive:
        image: mysql:5.5.52
        volumes:
            - ./db_data_live:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: ****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    dbdev:
        image: mysql:5.5.52
        volumes:
            - ./db_data_dev:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD:****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    phpmyadmin:
        depends_on:
            - dblive
            - dbdev
        image: phpmyadmin/phpmyadmin
        environment:
            PMA_ARBITRARY : 1
        restart: always
        ports:
            - "8081:80"

    web:
        build: ./
        depends_on:
            - dblive
            - dbdev
        volumes:
            - ./web:/var/www
            - ./config/php.ini:/etc/php5/apache2/conf.d/custom.ini
            - ./logs/apache_error.log:/var/log/apache2/error.log
            - ./logs/apache_access.log:/var/log/apache2/access.log
            - ./config/apache_default.conf:/etc/apache2/sites-enabled/000-default.conf
        restart: always
        ports: 
            - "80:80"
            - "443:443"

I tried following the advice here, can't upate php.ini file in Docker container, by creating a custom.ini file and mounting it in that location. I actually did it correctly I think because if you look at my image I attached for phpinfo(), you can see that under additional .ini files parsed my custom.ini is there at the end. I did a test though by setting asp_tags = On instead of Off and I can't. phpinfo() will always show it as off. Refer to my attached image of it showing it off despite loading my config file.

I'm not even sure if its really honoring any of the commands in there at all?

enter image description here

enter image description here

Extra Files Used

Run

#!/bin/bash
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

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND

install_php-5.5.30.sh

#!/bin/bash

# install dependencies
apt-get -y update && \
apt-get install -y \
  build-essential \
  apache2-dev \
  libxml2-dev

# download PHP 5.5.30 source code
cd /tmp
curl -fsSL http://php.net/get/php-5.5.30.tar.bz2/from/this/mirror | tar xjf -
cd php-5.5.30

# configure build options
./configure --prefix=/usr \
            --with-config-file-path=/etc/php5/apache2 \
            --with-config-file-scan-dir=/etc/php5/apache2/conf.d \
            --disable-pdo \
            --disable-json \
            --enable-mbstring \
            --with-apxs2

# compile and install
NUM_CORES=`cat /proc/cpuinfo | grep processor | wc -l`
make -j $NUM_CORES
make install

# configure extension directory
echo 'extension_dir="/usr/lib/php5/20121212"' >> /etc/php5/apache2/php.ini

# cleanup
rm -rf /tmp/php-5.5.30 /tmp/install_php-5.5.30.sh

My file structure

enter image description here

Sporades answered 17/1, 2017 at 7:47 Comment(1)
I want to note that if I attempt to volume directly to /etc/php/apache2/php.ini when starting the container it gets stuck in some kind of infinite loop? (Basically it never starts up and the container keeps restarting over and over)Sporades
S
16

I found the answer, put a file called custom.php.ini in the config directory (if you are following my directory structure).

Set the volumes like this in my example...

volumes:
            - ./web:/var/www
            - ./config/custom.php.ini:/etc/php5/apache2/conf.d/custom.php.ini

By default the scan directory for extra php files will look in the conf.d directory. These files will overwrite the settings of the main php.ini. I tested this with the asp_tag option turning it Off and On. It works fine as long as you do the following below.

The trick to making this work is to use docker-compose down instead of docker-compose kill

This removes the containers and their cache files. PHP only loads the configuration file once at bootup and the other files so changing the php.ini or the custom file after requires this docker-compose down to force the change.

Sporades answered 18/1, 2017 at 4:20 Comment(1)
With php8 its: "- ./config/custom.php.ini:/usr/local/etc/php/conf.d/custom.php.ini" You can get the path from the phpinfo(); page at point "Scan this dir for additional .ini files"Selfpity
E
1

If you are using something like wodby (docker4php or docker4drupal) or lando or trying to find an answer "why php.ini doesn't work" (like me), these tools are using their own way to pass configuration into php

https://github.com/wodby/php#php-and-php-fpm-configuration

I am posting this answer here because I wasted 2 hours to find an answer and I came into this question from google. I want to save some time to others.

Mounting of custom.php.ini will not help.

Essential answered 2/1, 2022 at 13:36 Comment(0)
S
1

Given that you are using an apache-php image in a compose.yml file, the only thing that worked for me under php8 is the following:

 volumes: 
      - ./config/php/php.ini:/usr/local/etc/php/php.ini

Make sure that the file /config/php/php.ini exists and has the required directives. You can use as a sample the php.ini file provided by the php repo. https://github.com/php/php-src/blob/master/php.ini-development

Note: Do not expect to see your local file in your phpinfo as a Configuration File (php.ini), but it will be mounted. If you do any change in your local file, this will be copied to the /usr/local/etc/php/php.ini and will be used by your php.

Sculley answered 22/1 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.