Docker and symfony - php

I'm struggling with Docker.
I'm tring to create an image to work on symfony project and to learn Docker in the same time.
Here is my Dockerfile:
FROM php:7-apache
LABEL Description = "This image is used to start Symfony3 project"
ENV DIRPATH /var/www/html
# apt-get command
RUN apt-get update && apt-get install -y \
vim \
git \
&& apt-get clean
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# Install the Symfony Installer
RUN curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
I build the image with the command:
docker build -t symfony .
Works well! Cool!
I'm create a container with:
docker run --name symfony -d -v "$PWD":/var/www/html -p 80:80 symfony
Works well also. The web server is running on the good port.
I can go in my container with:
docker exec -ti symfony bash
But when I'm trying to do a composer update, I have some errors:
Failed to download symfony/symfony from dist: Could not decompress the archive, enable the PHP zip extension.
A php.ini file does not exist. You will have to create one.
How can I create the php.ini in Dockerfile?
I also think that I have an issue with permission.
When I'm trying to the web/app_dev.php I have this message:
You are not allowed to access this file. Check app_dev.php for more information.

You can ADD a custom php.ini configuration specifing it in the dockerfile,
As Example, you can take a look at this repo for this example:
dokerfile
# install a few more PHP extensions
RUN apt-get update && apt-get install -y php5-imagick php5-gd php5-mongo php5-curl php5-mcrypt php5-intl
# copy a custom config file from the directory where this Dockerfile resides to the image
COPY php.ini /etc/php5/fpm/php.ini
You can find various approach and various sample on the net.
Hope this help

Next to the missing php.ini file you should also install zip so you can download from dist, i.e.
RUN docker-php-ext-install zip
Which will install and enable the PHP zip extension which is requested in your error message.

Related

How to install extensions using docker-php-extension-installer

I'm new to docker.
I would like to use docker-php-extension-installer for installing PHP extensions to my container, because according to php-docker hub docs this script builds upon the docker-php-ext-* scripts and simplifies the installation of PHP extensions by automatically adding and removing Debian (apt) and Alpine (apk) packages.
The problem is when I try to install gd or pdo_mysql or something else, extensions are installed but not shown in the modules list when I run php -m command in my bash.
Here is my DockerFile
FROM php:7.3-fpm
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN apt-get update && apt-get install -y unzip \
&& apt-get install -y git nano \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# use https://github.com/mlocati/docker-php-extension-installer to simply install php extensions with dependencies
ADD https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions /usr/local/bin/
RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions gd pdo_mysql
Maybe I have to enable these extensions somehow, but how can I do it from my Dockerfile? There is nothing said about this neither in docker-php-extension-installer page docs, no in official php docker page.
If anybody knows, please help. Thanks.
The above-mentioned Dockerfile is used as part of docker-compose. When I created a docker-compose.yml file, I made a mistake. To change default php configuration, I mounted host folded, where I keep my custom php.ini, and mapped it to conf.d folder in php container. Like this:
# PHP Service
php:
build: ./.docker/php
volumes:
- ./.docker/php/conf.d:/usr/local/etc/php/conf.d:ro
depends_on:
- mysql
But the conf.d folder keeps the configuration of all installed extensions, for example:
docker-php-ext-gd.ini docker-php-ext-pdo_mysql.ini docker-php-ext-sodium.ini docker-php-ext-xdebug.ini
So the contents of my host folder replaced the original conf.d contents and as a result - extensions didn't work.

Docker with laravel fails because of php extension

Running Laravel on an appache server.
Upon building the image with docker-compose up --build with the following Dockerfile
FROM php:7.3-apache-stretch
RUN apt-get update -y && apt-get install -y libpng-dev
RUN docker-php-ext-install pdo pdo_mysql gd
FROM composer:1.9.0 as build
WORKDIR /app
COPY . /app
RUN composer global require hirak/prestissimo && composer install
I am getting the error message:
phpoffice/phpspreadsheet 1.13.0 requires ext-gd * -> the requested PHP extension gd is missing from your system.
This happens when the composer install command runs.
As you can see up, I am actually installing gd from php, so it should not give me this error message.
Do you have any idea how I can solve it?
Thanks!
It's happen, because you are using multistage building and your composer second stage have nothing to do with previous build using PHP container. Primary use case with multistaging is to produce some useful artefacts which can be used later.
So what I suggest is to copy composer file from composer image, then place it somewhere in your php container.
I will give you my solution which is working perfectly for me with laravel/symfony etc.
FROM php:7.4.4-fpm
# We copy composer from it's original image to our php container to use it later.
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www
ARG USER_ID
RUN useradd -s /bin/bash -d /home/user/ -m -G sudo,www-data user -u $USER_ID
RUN apt update && apt install -y zip unzip wget zlib1g-dev libicu-dev
RUN docker-php-ext-install pdo_mysql intl opcache gd
USER user
RUN wget https://get.symfony.com/cli/installer -O - | bash
ENV PATH="/home/user/.symfony/bin:${PATH}"
COPY php.ini /usr/local/etc/php
# You can also run here composer install, depends on your use case
You can change your docker image. For example try this:
FROM richarvey/nginx-php-fpm
WORKDIR /app
RUN php ./artisan config:cache && composer install

Install mongodb driver in docker with dockerfile

I have a mongodb docker container. I need another docker container which will have php and apache installed. I want to run a php script from this container and send some data to the mongodb container to save the data in mongodb database. So i need to install mongodb driver in the php-apache container.
To do that, i have created following dockerfile:
FROM php:7.3-apache
COPY src/ /var/www/html
RUN apt-get update
RUN apt-get install openssl libssl-dev libcurl4-openssl-dev
RUN pecl install mongodb
RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN echo "extension=mongodb.so" > /usr/local/etc/php/php.ini
EXPOSE 80
It first builds php-apache docker image. Then it should install mongodb driver.
But when i run the following command
docker build -t my-mongo .
At one point it shows following message and stops the execution:
Need to get 2213 kB of archives.
After this operation, 9593 kB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get install openssl libssl-dev libcurl4-openssl-dev' returned a non-zero code: 1
What went wrong here? Is there anything wrong in the dockerfile ?
What went wrong here? Is there anything wrong in the dockerfile ?
There are at least three things wrong with your Dockerfile IMO.
The first one is not in direct relation to your problem, but you are creating way too many layers (one for each RUN command) for something as simple as adding a driver to your image. You should put all this in a single layer (i.e. a single RUN command) and cleanup after yourself at the end to keep the layer footprint small.
Now the core of your real problem. As you can see in your output, apt-get is launched in interactive mode and asks for a confirmation. The docker build process can't handle that and therefore aborts the command causing the build to fail. To overcome this, apt-get has a -y option to answer 'yes' to all prompts by default.
The last one is in the line where you add the mongo driver to php.ini: you are redirecting echo output to your file with a single gt; sign (>), hence you are replacing the full content of the file you just copied. You must use a double gt; sign (>>) for content to be appended.
The following Dockerfile should fix the problems above (tested without the copy of sources + cp of your own php.ini file since I don't have them)
FROM php:7.3-apache
COPY src/ /var/www/html
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssl libssl-dev libcurl4-openssl-dev \
&& pecl install mongodb \
&& cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
&& echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
EXPOSE 80
Some explanation:
The && notation allows to run all commands one after the other in a single docker RUN command resulting in a single intermediate container, thus a single layer.
-y --no-install-recommends options to apt-get ask apt to not go interactive (answer yes everywhere) and to install only needed packages, not the recommended ones.
The two last instruction apt-get cleann && rm -rf /var/lib/apt/lists/* remove all caches made by running apt so that the layer stays as small as possible. (see apt-get chapter in docker best practice)
put this at the beginning of your Docker file, it is important to have it before installing all other extensions, otherwise it fails.
FROM php:7.3-apache
RUN apt-get update -y && apt-get upgrade \
&& pecl install mongodb && docker-php-ext-enable mongodb
That's all what I needed to get mongodb running FROM php:7.3-cli-buster Probably it will work for other versions - fpm, apache etc as well.

How to add Zend Guard Loader support in docker php official image instance?

I want to add Zend Guard Loader support on my php instance.
http://www.zend.com/en/products/loader/downloads#Linux
Normally, I will download the package, and then add the following settings into php.ini
[Zend Guard Loader]
zend_extension="/usr/local/webserver/php/ext/ZendGuardLoader.so"
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path="/var/developer.zl"
But, now I'm running the instance within docker.
docker run --name php_instance php:5-fpm
And I tried to get into the shell:
docker exec -it php_instance bash
But I cannot find the php.ini, how can I make it work?
Did you add a command in your Dockerfile to copy the php.ini file from local to docker container?
Similar to
FROM php:7.1-fpm
# Install system packages
RUN apt-get update && apt-get install -y \
openssl \
libssh2-1 \
libssh2-1-dev \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pecl install xdebug
# Enable php extensions
RUN docker-php-ext-install mysqli pdo_pgsql
RUN pecl install ssh2-1.1.2
# Copy custom php.ini file
ADD ./deployment/my.php.ini /usr/local/etc/php/

Permission Issue in Docker container for Symfony2

I'm tring to create an Docker image to bootstrap Symfony project.
Here is my Dockerfile:
FROM php:7-apache
LABEL Description = "This image is used to start Symfony3 project"
ENV DIRPATH /var/www/html
# apt-get command
RUN apt-get update && apt-get install -y \
vim \
git
RUN apt-get install -y zlib1g-dev && docker-php-ext-install zip
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# Install the Symfony Installer
RUN curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
# Create the php.ini file
RUN cp /usr/src/php/php.ini-development /usr/local/etc/php/php.ini
The build and the container creation works well but I have a permission issue in my container.
When I'm going to my app_dev.php, I have this message:
You are not allowed to access this file. Check app_dev.php for more information.
Apparently, I can access this file only with localhost.
Also, PHP can't delete or create anything in my container.
For exemple I have the following error when I'm running:
$php app/console cache:clear
Failed to remove directory "/var/www/html/app/cache/dev_old/doctrine
How can I solved that in my Dockerfile?
Finally found it after weeks:
Add that in you Dockerfile. It solved the permission issue.
# Workaround for write permission on write to MacOS X volumes
# See https://github.com/boot2docker/boot2docker/pull/534
RUN usermod -u 1000 www-data

Categories