I'm creating a Symfony environment (PHP-FPM, Nginx, & more) with Docker & Docker-compose.
But, PHP does not use my php.ini and ignores the config (date.timezone parameter is not found in my Symfony application).
Of course, when I go on my container, the date.timezone is correctly set in the 2 php.ini (cli & FPM).
I don't understand why, but it works if I put my php.ini in /usr/local/etc/php/ folder (wtf)
Did I miss something?
docker-compose.yml :
nginx:
image: nginx
volumes:
- "./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro"
links:
- "php:php"
ports:
- "80:80"
- "443:443"
working_dir: "/etc/nginx"
php:
build: docker/php
volumes:
- ".:/var/www:rw"
working_dir: "/var/www"
Dockerfile :
FROM php:5-fpm
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
apt-get install -y php5-common php5-fpm php5-cli php5-mysql php5-apcu php5-intl php5-imagick && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN sed -i 's/;date.timezone =/date.timezone = "Europe\/Paris"/g' /etc/php5/fpm/php.ini
RUN sed -i 's/;date.timezone =/date.timezone = "Europe\/Paris"/g' /etc/php5/cli/php.ini
RUN usermod -u 1000 www-data
Official PHP Docker Image use /usr/local/etc/php as base folder: see Dockerfile.
I've been facing the same problem for a while, the host machine had a correct date/time but every php container was in GMT instead of Europe/Paris or host date time.
For those of you who will search for a valid answer and need more precisions than #moliver answer, using sed in the dockerfile maybe not the best answer. Moreover, php.ini might not be created at building time. Here is the ls of /usr/local/etc/php in one of my php container.
root#6fd7bc929a7e:/usr/local/etc/php/conf.d# ls
docker-php-ext-bcmath.ini docker-php-ext-exif.ini docker-php-ext-mysqli.ini docker-php-ext-soap.ini docker-php-ext-xsl.ini
docker-php-ext-bz2.ini docker-php-ext-gd.ini docker-php-ext-pcntl.ini docker-php-ext-tidy.ini docker-php-ext-zip.ini
docker-php-ext-calendar.ini docker-php-ext-mcrypt.ini docker-php-ext-pdo_mysql.ini docker-php-ext-xmlrpc.ini ext-imagick.ini
(yes i added a lot of extensions, but no php.ini by default).
docker-compose is very flexible, you can add a volume containing php.ini info :
version: '2'
services:
php:
build: ./DOCKERFILES/phpdemo
container_name: applicationDEMO
volumes:
- "./conf/whatever.ini:/usr/local/etc/php/conf.d/whatever.ini"
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
And in your ./conf/whatever.ini
date.timezone = "Europe/Paris"
Hope this helps
I had this issue in my php:5.6-apache docker image.
I simply added these two lines in my dockerfile:
# use development php ini
RUN mv /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini
# set default timezone
RUN sed -ri -e 's!;date.timezone =!date.timezone = "Europe/Paris"!g' /usr/local/etc/php/php.ini
It worked well. :-)
Related
I am trying to Dockerize my project (PHP MYSQL and PDO). Even though I added the scripts to install the extensions to my Dockerfile and every time I build it is installing them I am still getting: "Could not find driver". I checked in the phpinfo() and the driver is not there. I deleted all images and containers, built from scratch. Same result. Any ideas? In my docker file I have the following:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 80
and my docker-compose.yaml file:
version: '3.3'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php74
depends_on:
- db
links:
- db
volumes:
- ./php:/var/www/html/
ports:
- 8008:80
db:
container_name: mysql8
command: --default-authentication-plugin=mysql_native_password
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: realDE
MYSQL_USER: khaldoun
MYSQL_PASSWORD: password
ports:
- 6033:3306
For the test I performed, I did the following:
Dockerfile -
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
EXPOSE 80
# start Apache2 on image start
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
index.php
<?php
phpinfo();
?>
run command (I named the image pdo-test):
docker run --name=pdo-test -p 8080:80 -d pdo-test
Once the container was started I navigated to HTTP://localhost:8080/index.php and saw the PDO driver is loaded:
Please note the only difference between my Dockerfile and yours is that I copied a PHP page into /var/www/html and added a command that would start Apache when the container is run.
Things you should check:
is the volume you're mounting correct ./php:/var/www/html
since you have no command to execute Apache, confirm it is starting properly in the container. I tested both ways and it started each time, but you should bash into the container and make sure Apache is running as you expect.
EDIT I copied one of the php.ini files from the container
docker cp pdo-test:usr/local/etc/php/php.ini-production php.ini
and uncommented the PDO drivers:
;extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
Then I rebuilt the container, copying in the updated php.ini file:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
COPY $PWD/php.ini /usr/local/etc/php
EXPOSE 80
# start Apache2 on image start
# CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
I can now see the php.ini file in phpinfo()
docker-compose.yml
version: '3.7'
services:
php:
build:
context: .
dockerfile: Dockerfile
image: php:7.3-rc-fpm
container_name: php_7.3-rc-fpm
volumes:
- .:/var/www/app
restart: unless-stopped
working_dir: /var/www
stdin_open: true
tty: true
Dockerfile
FROM php:7.3-rc-fpm
RUN apt-get update && apt-get install -y \
build-essential \
mysql-client \
locales \
zip \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip pcntl
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
Started containers with
docker-compose up -d
and when I execute
docker-compose exec php bash
followed by
mysql --version
result in
bash: mysql: command not found
the mysql-client is missing and the others RUNs installation as well...
Any idea what is going on?
....and stackoverflow need more details to approve my edit when I don't have any ............
image: php:7.3-rc-fpm should be dropped.
It tells docker-compose to build from the "php_7.3-rc-fpm" image and not from the image build with your Dockerfile (it's a question of precedence). So it's normal that nothing you ask to install in the Dockerfile is available...
I tested to be sure and indeed, if you drop this line, the commands docker-compose exec php bash followed by mysql --version gives you what you expected.
You are misusing container image name. In your docker-compose.yml you tell:
services:
php:
build:
context: .
dockerfile: Dockerfile
image: php:7.3-rc-fpm
That you want to build your own image and name it php:7.3-rc-fpm! But this is not your image' name - it is the name of a well-known php docker container! And in your Dockerfile you inherits from it:
FROM php:7.3-rc-fpm
So, you are overwriting public image but your own. And I can only guess, what will be the new image like..
Solution - remove image from your docker-compose file. It is not the image to be used, it is the name you want to give your image after being built, upon used in conjunction with build properties.
I'm running few Docker Containers view at the Code below. I would like to integrate the TYPO3 via w get but get an ERROR: Service 'app' failed to build: ADD failed: stat /mnt/sda1/var/lib/docker/tmp/docker-builder400003814/|: no such file or directory how can i solve it? and move the necessary folders into my www/ path thanks :)
docker-compose up -d
`docker-compose.yml`
version: '2'
services:
version: '2'
services:
#######################################
# PHP application Docker container
#######################################
app:
build:
context: .
dockerfile: Dockerfile
links:
- mysql
ports:
- "8000:80"
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MySQL-5.7.Dockerfile
restart: always
env_file:
- etc/environment.yml
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
myphpadmin:
build:
context: docker/myphpadmin
dockerfile: Dockerfile
restart: always
links:
- mysql
ports:
- 8080:80
environment:
- PMA_HOST=mysql
- VIRTUAL_PORT=80
networks:
- php-network
networks:
php-network:
driver: bridge
`Dockerfile` for PHP & APACHE
FROM webdevops/php-apache-dev:ubuntu-16.04
ENV PROVISION_CONTEXT "development"
# Configure volume/workdir
WORKDIR /app/
Typo3 `Dockerfile`
FROM ubuntu:latest
ENV TYPO3_VERSION 7.6.16
# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
apache2 php7.0 php7.0-mysql libapache2-mod-php7.0 curl lynx-cur php7.0-curl php7.0-gd php-imagick php7.0-soap php7.0-xml php7.0-zip
# Enable apache mods.
RUN a2enmod php7.0
RUN a2enmod rewrite
# Update the PHP.ini file, enable <? ?> tags and quieten logging.
RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php/7.0/apache2/php.ini
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/7.0/apache2/php.ini
ADD https://get.typo3.org/7.6 | tar -xzf - && \
RUN cd /var/www/html && \
ln -s typo3_src-* typo3_src && \
ln -s typo3_src/index.php && \
ln -s typo3_src/typo3 && \
ln -s typo3_src/_.htaccess .htaccess && \
mkdir typo3temp && \
mkdir typo3conf && \
mkdir fileadmin && \
mkdir uploads && \
touch FIRST_INSTALL && \
chown -R www-data. .
# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
# Expose apache.
EXPOSE 80
# Copy this repo into place.
ADD www /var/www/site
# Update the default apache site with the config we created.
ADD etc/apache-config.conf /etc/apache2/sites-enabled/000-default.conf
# By default start up apache in the foreground, override with /bin/bash for interative.
CMD /usr/sbin/apache2ctl -D FOREGROUND
That is however not really TYPO3 related.
I would suggest that you take a look at https://github.com/webdevops/TYPO3-docker-boilerplate which is well tested and just works out of the box.
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.
My Dockerfile is below:
FROM ubuntu:14.04
MAINTAINER Joe Astrahan <jastrahan#poolservice.software>
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?
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
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.
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.
I build 2 dockers, one docker with apache, one docker with php5, and I use docker-compose to start.
apache2 Dockerfile in directoy apache2:
FROM debian:latest
RUN apt-get update && apt-get install -y apache2
ADD test.php /var/www/html
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
and test.php:
<?php
phpinfo();
?>
php5 Dorckerfile in directory php:
FROM debian:latest
RUN apt-get update && apt-get install -y php5
docker-compose.yml:
apache:
build: ./apache2
container_name: apache
ports:
- "80:80"
links:
- "php5"
php5:
build: ./php
container_name: php
then I run:
docker-compose up
apache2 server start successfully. Then I access this server by http://server_ip, then I get index of debian.But when I access http://server_ip/test.php, just occur this:
<?php
phpinfo();
?>
php just doesn't work.And I don't why.
You can separate Apache and PHP with PHP-FPM. It is however that the DocumentRoot must be mounted on both containers.
Apache must be able to access the files locally (inside its container) as well as the PHP-FPM server.
I am currently working on the same, have a look at my docker-compose.yml here
https://github.com/cytopia/devilbox/blob/master/docker-compose.yml
Both volumes (in PHP and apache) are mounted to /shared/httpd
I would say its not possible to run seperate containers for php as apache module. I guess this is what Wolfgang meant.
If you want to seperate apache and php in two different containers you need to run php as fpm.
Have a look here for inspiration: How to correctly link php-fpm and Nginx Docker containers together?
If you need to run apache and php as apache_mod use a combined container like this: https://github.com/docker-library/php/blob/fec7f537f049aafd2102202519c3ca9cb9576707/5.5/apache/Dockerfile
from: https://hub.docker.com/_/php/
If you don't specifically need to separate Apache from PHP, then you might be good to go with an official php:5.6-apache image which comes with Apache out of the box.
For example, your docker-compose.yml might look something like this:
version: '3'
services:
web:
image: php:5.6-apache
ports:
- "8080:80" # Map container port 80 to host machine port 8080
volumes:
- ".:/var/www/html" # Mount current folder as volume to container at /var/www/html
Or, for a more real-life example, if you also need at least one of the following:
A custom web root (for Laravel, Symfony, etc)
Other Apache modules installed
Other PHP extensions installed
You might do something more like this:
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80" # Map container port 80 to host machine port 8080
environment:
APACHE_DOCUMENT_ROOT: "/var/www/yourapp.com/public"
volumes:
- ".:/var/www/yourapp.com" # Mount current folder as volume to container at /var/www/yourapp.com
And then your Dockerfile (which we reference from the docker-compose.yml above):
FROM php:5.6-apache
# Declare an environment variable with a default value for changing Apache's document root
# We will override this in docker-compose.yml
ENV APACHE_DOCUMENT_ROOT /var/www/html
# Configure web root
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Install additional Apache modules
# This example: mod_rewrite & mod_headers
RUN a2enmod rewrite headers
# Install additional PHP extensions
# This example: memcached & mysqli
# For other extensions see official docs:
# https://hub.docker.com/_/php (section: How to install more PHP extensions)
RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \
&& pecl install memcached-2.2.0 \
&& docker-php-ext-enable memcached \
&& docker-php-ext-install -j$(nproc) mysqli