Docker-Compose, Dockerfile and Build Image Problems (php-fpm + pecl + xdebug) - php

I'm new to Docker and want to build the image myself (nginx + php-fpm + mariadb + phpadmin).
I want to have Xdebug so I've a Dockerfile to customize the php-fpm image.
And then I run into a problem (same as here) when I execute
docker-compose --build
or
docker-compose up --build
The image was built ok first time, but for next times it fails because the php-fpm somehow ended up with already installed xdebug:
+ pecl install xdebug
pecl/xdebug is already installed and is the same as the released version 3.1.5
install failed
It is like the image comes from cache but Dockerfile is still applied.
I've used the solution from that post but I am clearly missing something, it should not be like that. What am I doing wrong?
My docker-compose:
version: "3.7"
services:
web:
image: nginx:1.17
ports:
- 8080:80
volumes:
- ../logs:/var/log/nginx/
- ../wordpress:/var/www/myapp
- type: bind
source: ./site.conf
target: /etc/nginx/conf.d/default.conf
depends_on:
- php
- mariadb
php:
image: php:7.4-fpm
build:
context: ./php
volumes:
- ../wordpress:/var/www/myapp
- type: bind
source: ./php/conf.d/xdebug.ini
target: /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- type: bind
source: ./php/conf.d/error_reporting.ini
target: /usr/local/etc/php/conf.d/error_reporting.ini
depends_on:
- mariadb
mariadb:
image: mariadb:10.4
restart: always
ports:
- 127.0.0.1:3308:3306
environment:
- MYSQL_ROOT_PASSWORD=4321
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=1234
volumes:
- mariadb-data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 8900:80
environment:
- PMA_ARBITRARY=1
- PMA_HOST=mariadb
depends_on:
- mariadb
volumes:
mariadb-data:
and my php-fpm dockerfile:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libicu-dev\
openssl \
git \
unzip\
nano\
&& docker-php-ext-install \
intl pdo pdo_mysql mysqli\
&& docker-php-ext-enable \
intl pdo pdo_mysql
RUN bash -c '[[ -n "$(pecl list | grep xdebug)" ]]\
|| (pecl install xdebug && docker-php-ext-enable xdebug)'

Discussion/Explanation
What am i doing wrong?
Perhaps it is a misunderstanding of the docker-compose.yml template, specifically in regards of the container for the php service (based on php:7.4-fpm) which might have been introduced in error.
When you specify both the image and the build attribute of a service, the image as you build it will be stored under the image name (see also later References section).
As you name the image as php:7.4-fpm and those build images are stored locally (there where you run docker-compose(1)) and you have in your Dockerfile within that build context that image same name php:7.4-fpm:
FROM php:7.4-fpm
# ...
Then when you build for the first time, the image php:7.4-fpm is not available locally. Then it is build from Docker Hub fetched php:7.4-fpm and the build result is then overwriting the image php:7.4-fpm locally as you are using the same name (!).
This is most likely not intended.
This can also explain why --build --force-recreate does not work either: The Dockerfile build instructions consider php:7.4-fpm to be the image to be built from but it is already the result (of the first local build).
Solution
Remove the image service keyword. You don't need it as you have the build keyword. Docker Compose will take care and name the image within your project automatically (based on service- and project name).
services:
php:
build:
context: ./php
volumes:
- ../.:/var/www/myapp:ro
Then remove the dangling/tainted php:7.4-fpm image:
$ docker image rm php:7.4-fpm
These two steps should already solve your issue and get your docker composition up without errors.
Additionally you can remove the workaround and do a plain pecl install xdebug && docker-php-ext-enable xdebug, here with better debugging abilities for the build (set -ex):
FROM php:7.4-fpm
RUN set -ex ;\
apt-get update ;\
apt-get install -y \
libicu-dev\
openssl \
git \
unzip \
nano \
;\
docker-php-ext-install intl pdo pdo_mysql mysqli ;\
docker-php-ext-enable intl pdo pdo_mysql ;\
:
RUN set -ex ;\
pecl install xdebug ;\
docker-php-ext-enable xdebug ;\
:
You could further tweak this by making the FROM image a build argument, bind it in the docker-composer.yml template (and use variables and defaults there-in, too). I'll leave that to your liking.
References
If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:
build: ./dir
image: webapp:tag
This results in an image named webapp and tagged tag, built from ./dir.
From: https://docs.docker.com/compose/compose-file/compose-file-v3/#build

When starting the containers, you will need to instruct docker-compose to rebuilt the dockerfile.
Try this:
docker-compose up --build --force-recreate

Related

Docker phpdockerio/php:8.1-fpm builds with PHP version 8.2: How can I revert to 8.1?

I'm running my project on a PHP-FPM docker container (details of config files below). When I build my containers and attempt to run composer I'm getting errors reporting missing PHP extensions. However, I thought my build files where covering these extensions (see docker/php-fpm/Dockerfile below).
It turns out that the container is being built with php8.2 as the default version. I have been able to change the symlinks to set the default version back to php8.1 but this doesn't seem like the right solution. Can anyone suggest a better fix?
How I know the container is running 8.2:
Inside the container I ran php --version and got:
root#fee8cc9ff790:/application# php --version
PHP 8.2.0 (cli) (built: Dec 8 2022 13:56:08) (NTS)
Then which php gave me:
root#fee8cc9ff790:/application# which php
/usr/bin/php
I followed the symlinks to find that linked PHP binaries in /etc/alternatives are:
phar -> /usr/bin/phar8.2
phar.phar -> /usr/bin/phar.phar8.2
php -> /usr/bin/php8.2
phpdbg -> /usr/bin/phpdbg8.2
This is the bit that doesn't seem right to me. I was able to relink these to their 8.1 versions and things seem to be running fine now but what happens when I rebuild the container?
Details of my files:
docker-compose.yml
###############################################################################
# Generated on docker.io #
###############################################################################
version: '3.9'
services:
mailhog:
image: 'mailhog/mailhog:latest'
redis:
image: 'redis:alpine'
mysql:
image: 'mysql:8.0.27'
working_dir: /application
platform: linux/amd64
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=
- MYSQL_USER=
- MYSQL_PASSWORD=
webserver:
image: 'nginx:alpine'
working_dir: /application
volumes:
- '.:/application'
- './docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf'
php-fpm:
build: docker/php-fpm
working_dir: /application/
volumes:
- '.:/application'
- './docker/php-fpm/php-ini-overrides.ini:/etc/php/8.1/fpm/conf.d/99-overrides.ini'
- './docker/php-fpm/php-ini-overrides.ini:/etc/php/8.1/cli/conf.d/99-overrides.ini'
docker-compose.override.yml
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: '3.9'
services:
mailhog:
ports:
- '8026:8025'
mysql:
ports:
- '33061:3306'
webserver:
ports:
- '801:80'
docker/php-fpm/Dockerfile
FROM phpdockerio/php:8.1-fpm
WORKDIR "/application"
RUN apt-get update; \
apt-get -y --no-install-recommends install \
git \
php-xdebug \
php8.1-mysql \
php8.1-sqlite \
mysql-client \
php8.1-redis; \
apt-get clean; \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
This turned out to be caused by the extensions I was loading. In docker/php-fpm/Dockerfile I installed php-xdebug which was causing the container to load up with PHP8.2
Specifying the 8.1 version php8.1-xdebug solved the problem.
Credit to luispabon for his response to the issue I raised on GitHub after thinking this must be a problem with the image.
https://github.com/phpdocker-io/base-images/issues/62

A Dockerfile doesn't install RUN dependencies

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.

How to enable LDAP on a dockerized Wordpress?

I have this container that I ran with a docker-compose.yml file.
version: '3'
services:
db:
<...>
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
I need to enable LDAP on PHP but I really can't find out how to complete the steps explained in the tutorial.
You will need to use the --with-ldap[=DIR] configuration option when
compiling PHP to enable LDAP support.
How am I supposed to do this on a running container ? Should this be done before running docker-compose up if so, which environment configuration am I supposed to use ?
You just have to use a different image as it's not easily configurable using the original image.
Have a look at dalareo/docker-wordpress-ldap-support on GitHub. You might use this Dockerfile by downloading it to the directory your project would be stored in and make a small change to your docker-compose.yml like here:
version: '3'
services:
db:
<...>
wordpress:
depends_on:
- db
# remove: image: wordpress:latest and put this instead:
build: .
# and place the rest of the definitions you normally have there
The Dockerfile copied from the repo:
FROM wordpress
RUN set -x \
&& apt-get update \
&& apt-get install -y libldap2-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
&& docker-php-ext-install ldap \
&& apt-get purge -y --auto-remove libldap2-dev
edit
I've found a public registry image build from this Dockerfile
Now you don't actually have to make any changes to your original docker-compose.yml file except for changing the image your wordpress is expected to run from. From wordpress:latest to dalareo/wordpress-ldap

install packages from docker-compose.yml into docker container

I am a beginner with docker and docker-compose and i need your help.
I'm making PHP-NGINX-PostgresSQL symfony developement environment using docker-compose.
Here it is :
web:
image: nginx:1.13.5
ports:
- "80:80"
volumes:
- ./html:/html
- ./site.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./html:/html
links:
- postgres
postgres:
image: postgres:9.6.5
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: postgres
Now, i would like to install php7.2-intl into my php container. So i would like to execute something like :
$ sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.2-intl
Could you help me? I'm really stuck and also I dont have a Dockerfile file, just a docker-compose.yml file.
To get a PHP docker container with the intl extension, you need to extend the official PHP image.
To do so, declare the use of your own Dockerfile for your PHP image in docker-compose.yml:
services:
php:
# Remove this line
# image: php:7-fpm
# Add this one instead
build: './docker/php'
# ...
Then, add the following Dockerfile file to the docker/php folder:
FROM php:7.1-fpm
RUN apt-get update && apt-get install -y \
libicu-dev \
&& docker-php-ext-install \
intl \
&& docker-php-ext-enable \
intl
You can now run docker-compose build to get your PHP container built with the Intl extension.
A few notes:
I prefer to explicitly tell which PHP version I use (here "7.1.x") rather than the more generic "7.x" you defined with php:7-fpm.
I preferred to use the docker-php-ext-install and docker-php-ext-enable command utilities provided by the PHP official image (see "How to install more PHP extensions" section in the PHP image documentation).

Adding mbstring to docker image for laravel 5 app

I am new to docker and I'm trying to set it up in order to run with Laravel 5.1. I am currently getting the following error
Call to undefined function Illuminate\Foundation\Bootstrap\mb_internal_encoding() in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php:43
I believe this is because the mbstring php extention is not installed. I have tried to add php-mbstring to the Docker file but it doesn't seem to be working.
Here is my full Docker file
FROM php:5.6.30-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-install mcrypt pdo_mysql \
&& docker-php-ext-install php-mbstring
I am then running sudo docker compose up from the application folder. This does not seem to be resolving the error though. How do I know if the extensions are being installed properly?
EDIT: I have included the docker-compose.yml file below
version: '2'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
# The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
volumes:
dbdata:
Remove the php- prefix and it should work fine. You can also run it on the previous docker-php-ext-install command:
docker-php-ext-install mcrypt pdo_mysql mbstring
on ubuntu php-mbstring has dependency with php-common and version specific mbstring like php7.1-mbstring that may cause the problem. You can check dependencies with this command below;
apt-cache depends php-mbstring

Categories