Can't build php docker image [duplicate] - php

This question already has answers here:
ADD failed : No such file/Directory while building docker image
(3 answers)
Closed 2 years ago.
I got this error when building docker php image
Step 13/25 : ADD php.ini /usr/local/etc/php/php.ini
ERROR: Service 'phpt3' failed to build: ADD failed: stat
/var/lib/docker/tmp/docker-builder310748204/php.ini: no
such file or directory
Below is the docker file:
FROM php:7.3-fpm
# install the PHP extensions we need
RUN apt-get update \
&& apt-get install -y --no-install-recommends msmtp mailutils vim curl debconf subversion git apt-transport-https apt-utils \
build-essential locales acl mailutils wget nodejs \
gnupg gnupg1 gnupg2 \
zlib1g-dev zlib1g-dev libicu-dev g++ \
sudo
# Install GD
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd
# MYSQLI
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
# Install ext-zip
RUN apt-get install -y unzip libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo_mysql json calendar intl
ADD php.ini /usr/local/etc/php/php.ini
COPY additionnal.ini /usr/local/etc/php/conf.d/
COPY php-fpm-pool.conf /usr/local/etc/php/pool.d/www.conf
RUN rm -rf /var/lib/apt/lists/* \
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version
# set up sendmail config, see http://linux.die.net/man/5/ssmtp.conf for options
RUN echo "hostname=localhost.localdomain" > /etc/msmtp/msmtp.conf
RUN echo "mailhub=maildevt3" >> /etc/msmtp/msmtp.conf
# The above 'maildevt3' is the name you used for the link command
# in your docker-compose file or docker link command.
# Docker automatically adds that name in the hosts file
# of the container you're linking MailDev to.
# Set up php sendmail config
RUN echo "sendmail_path=sendmail -i -t" >> /usr/local/etc/php/conf.d/php-sendmail.ini
# Fully qualified domain name configuration for sendmail on localhost.
# Without this sendmail will not work.
# This must match the value for 'hostname' field that you set in ssmtp.conf.
RUN echo "localhost localhost.localdomain" >> /etc/hosts
WORKDIR /var/www/
EXPOSE 9000
CMD ["php-fpm"]
Can anyone help me to overcome this bug.
I'm doing this tutorial
https://passions.miary.dev/2019/08/30/docker-maildev-fr/

It looks like the image installer does not have the rights to write to the installation directory.
-> How to fix:
ADD failed : No such file/Directory while building docker image
If that doesn't work: test if the folder, which is shown in the error exists.
Give enough permissions to the directory with chmod -> https://wiki.ubuntuusers.de/chmod/

Related

PHP Extensions are disabled by default using Docker

I'm developing a Laravel application using Docker. I use multiple containers for different services; App (PHP), Webserver (Nginx), Relational Database (MySql) and In-Memory Database (Redis).
I've written a Dockerfile for App service using php:8.1-fpm as the base image. The problem is when I bring up the container and go to /usr/local/etc/php folder to check the installed extensions (I tried to use FFI extension and it turned out that it is disabled), I found two files: php.ini-development and php.ini-production. When I executed cat command on them, I found out that almost all the extensions are disabled by default in both of them.
So my question is do I need to enable them myself each time I bring up the container? Isn't there a better way?
Here is my Dockerfile:
FROM php:8.1-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get upgrade -y && apt-get install apt-utils procps -y \
&& apt-get install -y build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libonig-dev \
libxml2-dev \
locales \
libzip-dev \
libpq-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl libgmp-dev libevent-dev libssl-dev libnghttp2-dev libffi-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install sockets gd mysqli pdo pdo_mysql mbstring zip exif pcntl soap gmp bcmath ffi
# Install additional extension
RUN mkdir -p /usr/src/php/ext/ && cd /usr/src/php/ext/ \
&& pecl bundle ev \
&& docker-php-ext-install -j$(nproc) ev
# Install PrimeModule for AuthKey generation speedup
RUN git clone https://github.com/danog/PrimeModule-ext \
&& cd PrimeModule-ext && make -j$(nproc) \
&& make install \
&& cd ../ \
&& rm -rf PrimeModule-ext/
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis \
&& docker-php-source delete \
&& apt-get autoremove --purge -y && apt-get autoclean -y && apt-get clean -y \
&& rm -rf /usr/src
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install nodejs
# RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
# RUN apt-get update && apt-get install -y nodejs
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
And Here is the first part of php.ini-development file (As it is a long file, I didn't copy all lines of the file):
; Be sure to appropriately set the extension_dir directive.
;
;extension=bz2
;extension=curl
;extension=ffi
;extension=ftp
;extension=fileinfo
;extension=gd
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
;extension=mbstring
;extension=exif ; Must be after mbstring as it depends on it
;extension=mysqli
;extension=oci8_12c ; Use with Oracle Database 12c Instant Client
;extension=oci8_19 ; Use with Oracle Database 19 Instant Client
;extension=odbc
;extension=openssl
;extension=pdo_firebird
;extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop
; The MIBS data available in the PHP distribution must be installed.
; See https://www.php.net/manual/en/snmp.installation.php
;extension=snmp
;extension=soap
;extension=sockets
;extension=sodium
;extension=sqlite3
;extension=tidy
;extension=xsl
;zend_extension=opcache

PHP GD no webp support in docker

I always used GD to manipulate webp normally in my local environment, but when attempting to test my scripts in docker environment, i get "Webp format is not supported by PHP installation." error. I am using latest php version as in showed in my dockerfile below:
FROM php:8-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
pngquant
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
# php.ini
COPY ./docker-compose/php/php.ini /usr/local/etc/php/
What i am missing?
You need to install libwebp-dev and configure gd lib to support it:
RUN apt-get update && ... \
apt-get install -y libwebp-dev && \
docker-php-ext-configure gd --with-webp;
The the examples here : https://github.com/docker-library/docs/tree/master/php#how-to-install-more-php-extensions

Docker git not found in PATH

I'm still learning docker, and i'm trying to Dockerize a laravel project.
I'm trying to execute laravel composer in docker, however git does not work:
[RuntimeException] git was not found in your PATH, skipping source download
Git works normal in my local machine.
My Docker file:
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql sockets
RUN curl -sS https://getcomposer.org/installer​ | php -- \
--install-dir=/usr/local/bin --filename=composer~
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY . .
RUN composer install
I've tried to install git using dockerfile:
FROM alpine
RUN apk add --no-cache git
CMD ["git","--version"]
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
but docker build dont work:
=> ERROR [stage-0 8/8] RUN apt-get update && apt-get upgrade -y && apt-get install -y git
docker-compose.yml:
version: '3.8'
services:
php-apache-environment:
container_name: php-apache.
image: php:7.4-apache
volumes:
- ./:/var/www/html
ports:
- 8001:80
How git works in docker? I have to change my environment variables somehow or can i install git on docker?
Or perhaps both and i doing both wrong?
I believe COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer is sufficient to install composer in you docker project .
I created a dockerfile for a laravel project before and this is how it looked
FROM php:7.3-apache
# 1. development packages
RUN apt-get update && apt-get install -y \
git \
zip \
curl \
sudo \
unzip \
libzip-dev \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++
# 2. apache configs + document root
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
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
# 3. mod_rewrite for URL rewrite and mod_headers for .htaccess extra headers like Access-Control-Allow-Origin-
RUN a2enmod rewrite headers
# 4. start with base php config, then add extensions
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN docker-php-ext-install \
bz2 \
intl \
iconv \
bcmath \
opcache \
calendar \
mbstring \
pdo_mysql \
zip
#upload
RUN echo "file_uploads = On\n" \
"memory_limit = 500M\n" \
"upload_max_filesize = 500M\n" \
"post_max_size = 500M\n" \
"max_execution_time = 600\n" \
> /usr/local/etc/php/conf.d/uploads.ini
# 5. composer
COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer

Docker fails to enable php extensions

I am trying to install some php extensions all day but an error I do not understand occurs. Basically if I remove the install docker-php-ext-enable line everything seems to work normally (except my app that depends on them).
Dockerfile
FROM php:8.0-apache
RUN apt-get update && \
apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
RUN a2enmod rewrite
RUN docker-php-ext-enable bcmath gd mysqli soap zip xml ctype curl fileinfo json
COPY idoit-1.16.2.zip /temp/idoit-1.16.2.zip
RUN unzip /temp/idoit-1.16.2.zip -d /var/www
RUN rm /temp/idoit-1.16.2.zip
COPY apache-vhost.conf /etc/apache2/sites-available/000-default.conf
# RUN chown -R www-data:www-data /var/www
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
Here is what I get when i run sudo docker build -t test .
Sending build context to Docker daemon 153.9MB
Step 1/23 : FROM php:8.0-apache
---> 1157b188bd87
Step 2/23 : RUN apt-get update && apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
---> Using cache
---> aacd74715e3f
Step 3/23 : RUN a2enmod rewrite
---> Using cache
---> 1ab1cd3d1228
Step 4/23 : RUN docker-php-ext-enable fileinfo
---> Running in 7d8a84075a50
error: 'fileinfo' does not exist
usage: /usr/local/bin/docker-php-ext-enable [options] module-name [module-name ...]
ie: /usr/local/bin/docker-php-ext-enable gd mysqli
/usr/local/bin/docker-php-ext-enable pdo pdo_mysql
/usr/local/bin/docker-php-ext-enable --ini-name 0-apc.ini apcu apc
Possible values for module-name:
opcache.so sodium.so
Some of the above modules are already compiled into PHP; please check
the output of "php -i" to see which modules are already loaded.
Any ideas?

I am not able to install mbstring using docker file

Hi i am new to docker.
I have cloned my laravel code from
https://github.com/laravel/laravel/tree/8.x
and followed the steps provided in given below link.
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose
But still, mbstring is not installed in my project with php 8 version as my laravel project is compatible with a higher version than 7.2 but when i tried mbstring with 7.2 version it was easyly installed. i want to install mbstring with php 8 version.
Given below is my docker file. please have a look at it.
FROM php:8.0.3-fpm-buster
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
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 exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
FROM php:8-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions mbstring pdo_mysql zip exif pcntl gd
#previous code
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
#RUN docker-php-ext-install
#RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
RUN php artisan key:generate
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Try this..
What follows is more appropriate as a comment, but I have an insufficient reputation to comment.
Adarsh Hiwrale's excellent suggestion mostly worked for me, but I had to rearrange a few lines to avoid a race condition.
That is, the reordered lines are necessary for anyone starting from scratch who is building for the first time because they will not yet have a partial Docker build that can execute RUN php artisan key:generate.
My revision follows with comments indicating what was modified and why.
FROM php:8-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions mbstring pdo_mysql zip exif pcntl gd
#previous code
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
#RUN docker-php-ext-install
#RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# BEGIN MODIFICATIONS to Adarsh Hiwrate's suggested code
# Copy existing application directory contents
# The following line is redundant (predundant?) because of the line which copies the required items with the correct permissions.
# COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# This has to come after the above copy, otherwise the code will not yet be available in the container.
RUN php artisan key:generate
# END MODIFICATIONS to Adarsh Hiwrate's suggested code
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Categories