41

I'm trying to set Magento2 on Docker with Nginx & PHP7.

I've added a custom php.ini file as recommended by the PHP7 Docker image. I can see from phpinfo.php that it's loading my php.ini file but none of my updates are there. enter image description here enter image description here enter image description here

It should be:

memory_limit = 2G
max_execution_time = 800

I've checked the PHP container and I can see the php.ini file is there with the correct settings, or so I think?

$ docker exec -it mymagento2docker_php_1 /bin/bash 
# cat /usr/local/etc/php/php.ini                                                                                                                  
; This file is created automatically by the docker build

memory_limit = 2G
max_execution_time = 800

What am I doing wrong? Below are some more details.


Docker Project

.
├── docker-compose.yml
├── index.php
├── magento2
│   ├── [DIRECTORIES & FILES OMMITED]
│                   
├── nginx
│   ├── Dockerfile
│   ├── default.conf
│   └── nginx.conf
├── output.txt
├── php
    ├── Dockerfile
    └── config
        └── php.ini

docker-compose.yml

nginx:
    build: ./nginx/
    ports:
        - 80:80
    links:
        - php
    volumes_from:
        - app

php:
    build: ./php/
    expose:
        - 9000
    links:
        - mysql
    volumes_from:
        - app

app:
    image: php:7.0-fpm
    volumes:
        - ./magento2:/var/www/html
    command: "true"

mysql:
    image: mysql:latest
    volumes_from:
        - data
    ports:
        - "3306:3306"        
    environment:
        MYSQL_ROOT_PASSWORD: mage2
        MYSQL_DATABASE: mage2
        MYSQL_USER: mage2
        MYSQL_PASSWORD: mage2 

data:
    image: mysql:latest
    volumes:
        - /var/lib/mysql
    command: "true"

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
        - 8080:80
    links:
        - mysql
    environment:
        PMA_HOST: mysql    

php/Dockerfile

FROM php:7.0-fpm

# Install dependencies
RUN apt-get update \
  && apt-get install -y \
    cron \
    libfreetype6-dev \
    libicu-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libpng12-dev \
    libxslt1-dev

# Configure the gd library
RUN docker-php-ext-configure \
  gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

# Install required PHP extensions
RUN docker-php-ext-install \
  gd \
  intl \
  mbstring \
  mcrypt \
  pdo_mysql \
  xsl \
  zip \
  soap

# Install the 2.4 version of xdebug that's compatible with php7
RUN pecl install -o -f xdebug-2.4.0

COPY config/php.ini /usr/local/etc/php/

## php/config/php.ini ##
; This file is created automatically by the docker build

memory_limit = 2G
max_execution_time = 800

UPDATE

I've tried restarting nginx with the below, but it did not work.

$ docker exec -it mymagento2docker_php_1 /bin/bash 
# /etc/init.d/nginx restart                                                                                                                       
bash: /etc/init.d/nginx: No such file or directory
# service nginx restart    
nginx: unrecognized service
# nginx -s reload          
bash: nginx: command not found
# exit
$ docker restart mymagento2docker_nginx_1
mymagento2docker_nginx_1

$ docker exec -it mymagento2docker_nginx_1 /bin/bash 
# /etc/init.d/nginx restart                                                                                                                                   
Restarting nginx: nginxross in ~/my-magento2-docker
$ docker-compose ps
            Name                          Command             State            Ports          
---------------------------------------------------------------------------------------------
mymagento2docker_app_1          true                          Exit 0                          
mymagento2docker_data_1         docker-entrypoint.sh true     Exit 0                          
mymagento2docker_mysql_1        docker-entrypoint.sh mysqld   Up       0.0.0.0:3306->3306/tcp 
mymagento2docker_nginx_1        nginx -g daemon off;          Exit 0                          
mymagento2docker_php_1          php-fpm                       Up       9000/tcp               
mymagento2docker_phpmyadmin_1   /run.sh phpmyadmin            Up       0.0.0.0:8080->80/tcp   
ross in ~/my-magento2-docker
$ docker-compose up -d
Starting mymagento2docker_app_1
Starting mymagento2docker_data_1
mymagento2docker_mysql_1 is up-to-date
mymagento2docker_phpmyadmin_1 is up-to-date
mymagento2docker_php_1 is up-to-date
Starting mymagento2docker_nginx_1
ross in ~/my-magento2-docker
$ docker exec -it mymagento2docker_nginx_1 /bin/bash 
# service nginx restart                                                                                                                                       
Restarting nginx: nginxross in ~/my-magento2-docker
$ docker-compose up -d
Starting mymagento2docker_app_1
Starting mymagento2docker_data_1
mymagento2docker_mysql_1 is up-to-date
mymagento2docker_phpmyadmin_1 is up-to-date
mymagento2docker_php_1 is up-to-date
Starting mymagento2docker_nginx_1
ross in ~/my-magento2-docker
$ docker exec -it mymagento2docker_nginx_1 /bin/bash 
# nginx -s reload                                                                                                                                             
2016/10/05 14:07:43 [notice] 12#12: signal process started
# 
starball
  • 20,030
  • 7
  • 43
  • 238
Holly
  • 7,462
  • 23
  • 86
  • 140

3 Answers3

78

Add a volumes: section to your php service in the docker-compose.yml file, map a local directory with a custom.ini file to /usr/local/etc/php/conf.d, and restart your container. Whatever valid settings in that file will override those from the main php.ini file. (Incidentally you can do the same with MySQL but not with Nginx).

This works in my own project:

php: volumes: - ./localpath/custom.ini:/usr/local/etc/php/conf.d/custom.ini

Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
otravers
  • 1,321
  • 8
  • 16
  • 21
    I want to elaborate on your answer since this helped in one of my other questions. I was using docker-compose kill to kill the containers and was wondering why the changes never took effect. Because docker-compose cache's the results from changes, make sure to use docker-compose down to forcefully take down the containers before starting them up after you do this and then it will load the php.ini file correctly. Hopefully this saves someone 8 hours of headache that this caused me. – Joseph Astrahan Jan 18 '17 at 04:25
  • Thank you! This was exactly what I needed. – JD- Jan 18 '21 at 19:50
  • 2
    So, does this only add the settings that you specify? Or will it only put in settings that you specify if you link a custom.ini? Also, can it be called custim.ini or should it be called php.ini? – techcase May 05 '21 at 17:03
1

i think you have to reload the nginx config. i dont know which OS your php container uses, but try inside of the container some of these:

# /etc/init.d/nginx restart

# service nginx restart

# nginx -s reload

my logical reason is, that you install php ( and start it at the same time ) and after all you copy the new config.

Gabbax0r
  • 1,696
  • 3
  • 18
  • 31
  • where would I put those lines of code? In the php/Dockerfile? I'm guessing without the # before them – Holly Oct 05 '16 at 13:58
  • ofcourse without the comment hashtag. inside of your nginx container. otherwise you can try to restart your php-fpm inside the php containerwith: /etc/init.d/php7-fpm restart or service php7-fpm restart maybe just php-fpm restart without the version number. here my research link http://www.cyberciti.biz/faq/unix-linux-restart-php-service-command/ – Gabbax0r Oct 05 '16 at 14:04
  • thanks I've tried that but without any success, see my update – Holly Oct 05 '16 at 14:10
  • Similar to this answer (enough to not make a separate answer), I had to restart apache with "apachectl restart" – Alkanshel Aug 16 '18 at 19:39
0

This is what happened : - If you map the php.ini from host to container, make sure that file (php.ini) must exist on host before you launch the container. Otherwise, the container will not start - Please check if docker was able to read your php.ini file (check permission, sometime docker is running as different user and can't access host file which belongs to root) - Every time you update php.ini, you must restart php-fpm (get inside the container and restart the php-fpm to test, do not rebuild container) , and clear cache (if you enable opcache)

Dylan B
  • 796
  • 8
  • 16