I have a laravel app that I want to dockerise.
I have seen several tutorials, like:
https://medium.com/#pierangelo1982/dockerize-an-existing-laravel-application-with-docker-compose-a45eb7956cbd
https://kyleferg.com/laravel-development-with-docker/
https://medium.com/#shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c
https://www.codementor.io/patrickfohjnr/developing-laravel-applications-with-docker-4pwiwqmh4
Here is my Dockerfile for dev:
FROM php:7.1.14-fpm
WORKDIR /app
COPY . /app
RUN apt-get update -y && apt-get install -y openssl zip unzip git npm \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libmagickwand-dev --no-install-recommends \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& docker-php-ext-install pdo pdo_mysql mbstring \
&& docker-php-ext-install zip \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& touch ./resources/assets/less/_main_full/main.less \
&& apt-get purge --auto-remove -y g++ \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& composer install && php artisan key:generate && php artisan migrate:fresh --seed \
&& npm cache clean -f && npm install -g n && n stable && npm install cross-env && npm install && npm run dev
There is 1 thing I don't understand:
Should I install all my dependencies ( composer install + npm install ) in the container ? It seems necessary to me even if it makes me a 1GB image, and it is quite long to build but a lot of those tutorials don't do it. How can it work?
Note: I don't want to use laradock, as the goal is to learn and practice Docker.
there is no sense in establishing dependencies within. just call the command line of your container and run the dependency setup
docker exec -it container_name bash
Related
I need help, I can’t install pdo_mysql, ampq or opcache with docker-php-ext-install. I prepared a docker project on a local PC (Mac OSX). On the local PC after build docker image is the final PHP configuration right. When I deploy the project config to the remote Debian server after building it, I have a completely different PHP version (phpinfo). Missing extensions opcache, pdo_mysql, ampq.
I tried to configure it differently and deleted the docker cache on the server, but it didn’t help.
docker system prune -a
My PHP dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/symfony
RUN apt-get update \
&& docker-php-ext-install mysqli pdo pdo_mysql \
&& docker-php-ext-enable pdo_mysql
RUN apt update && apt-get udpate \
&& apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip libxslt1-dev\
&& docker-php-ext-install intl \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip \
&& docker-php-ext-install xsl
RUN apt-get update \
&& docker-php-ext-install opcache
RUN apt-get update \
&& apt-get install -y \
librabbitmq-dev \
libssh-dev \
&& pecl install amqp \
&& docker-php-ext-enable amqp
RUN apt-get update && \
apt-get install -y libxml2-dev \
&& docker-php-ext-install soap
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Node.js
RUN curl -sL https://deb.nodesource.com/setup_14.x -o node_setup.sh && \
bash node_setup.sh && \
apt-get install -y nodejs && \
npm install npm -g
# Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && \
apt-get install -y yarn
RUN apt-get update && apt-get install -y cron
ADD ./crontab /etc/cron.d/crontab
#
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab
RUN crontab /etc/cron.d/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
RUN apt-get update && apt-get install -y supervisor
ADD ./supervisord.conf /etc/supervisor/
# Copy docker entrypoint file
COPY ./entrypoint.sh /entrypoint.sh
# Docker init
RUN ["chmod", "+x", "/entrypoint.sh"]
Not sure if it's a solution to your issue, but let's see.
From https://hub.docker.com/_/php:
If you are having difficulty figuring out which Debian or Alpine packages need to be installed before docker-php-ext-install, then have a look at the install-php-extensions project. 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. For example, to install the GD extension you simply have to run install-php-extensions gd. This tool is contributed by community members and is not included in the images, please refer to their Git repository for installation, usage, and issues.
I had similar issues in the past and resolved it by using install-php-extensions. See this comment.
My problem: When i run command
docker build --no-cache -t php --progress=plain .
My container is building, but I get Error in my PHP-project:
Error Call to undefined function snmp2_real_walk()
Command install in Dockerfile is't work... I need help!!
My Dockerfile:
FROM php:7.4-apache
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN if command -v a2enmod >/dev/null 2>&1; then \
a2enmod rewrite headers \
;fi
RUN apt-get update
# Install tools required for build stage
RUN apt-get install -fyqq \
bash curl wget rsync ca-certificates openssl ssh git tzdata openntpd \
libxrender1 fontconfig libc6 \
mariadb-client gnupg binutils-gold autoconf \
g++ gcc gnupg libgcc1 linux-headers-amd64 make python
#RUN apt install -y libsnmp-dev
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer \
&& chmod 755 /usr/bin/composer
# Install additional PHP libraries
RUN docker-php-ext-install bcmath pdo_mysql
RUN \
apt-get update && \
apt-get install libldap2-dev -y && \
rm -rf /var/lib/apt/lists/* && \
docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \
docker-php-ext-install ldap
# Install libraries for compiling GD, then build it
RUN apt-get update && apt-get install -y \
libfreetype6 \
libfreetype6-dev \
libjpeg62-turbo \
libjpeg62-turbo-dev \
libsqlite3-dev \
libssl-dev \
libpng-dev \
libpng16-16 \
libcurl3-dev \
libxml2-dev \
libonig-dev \
libsnmp-dev \
snmp \
&& docker-php-ext-install mysqli xmlrpc \
&& docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
&& docker-php-ext-install gd \
&& apt-get remove -fyqq libfreetype6-dev libpng-dev libjpeg62-turbo-dev
# -------------------- Installing PHP Extension: snmp --------------------
RUN docker-php-ext-configure snmp --with-snmp \
# Installation
&& docker-php-ext-install -j$(getconf _NPROCESSORS_ONLN) snmp \
&& true
RUN sed -i 's/;extension=snmp/extension=snmp/' php.ini-production
# Add ZIP archives support
RUN apt-get install -fyqq zip libzip-dev \
&& docker-php-ext-install zip \
&& apt-get remove -fyqq libzip-dev
# Install xdebug
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
# Enable XDebug
ADD xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
WORKDIR /app
I've tried a lot of options. Editing the
php.ini-production
file did not lead to the expected result. At the same time, the php.ini file also does not exist, but the command is present...
That's my dockerfile
FROM php:7.4-fpm
# Install GD
RUN apt update \
&& apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) pdo_mysql bcmath exif gd
# 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
# 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
When I try to use conversions from Spatie/Medialibrary I has an error:
Call to undefined function Intervention\Image\Gd\imagejpeg()
I tried everything from here and other sites, but it still not helps.
A lot of solutions for the LAMP stack, but I have Nginx.
I'm a newbie at docker and don't understand what's going wrong.
Please help :)
upd.
Last version of dockerfile:
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
RUN apt-get update && apt-get install -y \
zlib1g-dev \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev
# Install GD
RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg
RUN cd /usr/src/php/ext/gd && make
RUN cp /usr/src/php/ext/gd/modules/gd.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/gd.so
RUN docker-php-ext-install -j$(nproc) gd
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libfontconfig1-dev \
xclip \
libjpeg62 \
libonig-dev \
libxml2-dev \
zip \
unzip
# 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
I tried a --with-jpeg and --with-freetype, also tried the trick with the folder creation. But still JPEG Support and Freetype support is false.
Your docker container is missing the php-gd Extension.
This is how we install it in our dockerfile:
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
docker-php-ext-install is a script in the official Docker-Container:
https://hub.docker.com/_/php
So in your file you could do this:
RUN apt-get update && apt-get install -y \
git \
curl \
libfontconfig1-dev \
xclip \
libjpeg62 \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd
I am trying install Docker with PHP version 7.0 with memcache support, my docker-file is as below, however it fails #STEP 10 with error
Step 10 : RUN /usr/bin/phpize
---> Running in 450678a59cd4
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module
[31mERROR[0m: Service 'php' failed to build: The command '/bin/sh -c /usr/bin/phpize' returned a non-zero code: 1
Docker file is as below
FROM php:7.0-fpm
#FROM php:5.6-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-install mysqli mbstring pdo_mysql
# Download and Installing php libraries
RUN apt-get -y install php-pear php5-dev
# Download and Installing git and vim
RUN apt-get -y install git vim gcc
# Download and Installing zip unzip
RUN apt-get -y install zip unzip
# install PHP PEAR extensions
RUN apt-get -y install wget
#RUN wget http://pecl.php.net/get/memcache-3.0.9-dev.tgz && gunzip memcache-3.0.9-dev.tgz && tar -xvf memcache-3.0.9-dev.tar && cd memcache-3.0.9-dev \
# && phpize && ./configure && make && make install
# RUN git clone https://github.com/websupport-sk/pecl-memcache && cd pecl-memcache
RUN apt-get -y install libmemcached-dev libmemcached11
RUN git clone https://github.com/php-memcached-dev/php-memcached && cd php-memcached && git checkout -b php7 origin/php7
RUN /usr/bin/phpize
RUN ./configure && make && make install
RUN apt-get install -y memcached
EXPOSE 9000
COPY ./www.conf /etc/php-fpm.d/www.conf
COPY ./php.ini /etc/php.ini
COPY ./php-fpm.conf /etc/php-fpm.conf
COPY ./40-memcache.ini /etc/php.d/40-memcache.ini
#COPY bootstrap.sh /opt/bootstrap.sh
#RUN chmod +x /opt/bootstrap.sh
#ENTRYPOINT ["/opt/bootstrap.sh"]
Here's how your Dockerfiles should look like:
PHP-FPM Dockerfile:
FROM php:7.0-fpm
# ...
RUN apt-get update && apt-get install -y \
libpq-dev \
libmemcached-dev \
curl
# ...
# Install Memcached for php 7
RUN curl -L -o /tmp/memcached.tar.gz "https://github.com/php-memcached-dev/php-memcached/archive/php7.tar.gz" \
&& mkdir -p /usr/src/php/ext/memcached \
&& tar -C /usr/src/php/ext/memcached -zxvf /tmp/memcached.tar.gz --strip 1 \
&& docker-php-ext-configure memcached \
&& docker-php-ext-install memcached \
&& rm /tmp/memcached.tar.gz
# ...
CMD ["php-fpm"]
EXPOSE 9000
Memcached Dockerfile:
FROM memcached:latest
CMD ["memcached"]
EXPOSE 11211
This is taken from https://github.com/LaraDock/laradock
You need to run that command /usr/bin/phpize in the right folder.
See this Dockerfile as an example
RUN wget https://github.com/phpredis/phpredis/archive/2.2.5.zip; unzip 2.2.5.zip
WORKDIR /tmp/php-redis/phpredis-2.2.5
RUN /usr/bin/phpize; ./configure; make; make install
In your case, you did clone the repo php-memcached and make a cd in it, but that does not change the working directory for the next Dockerfile RUN directive.
Set that working directory before the RUN directive:
WORKDIR /php-memcached
RUN /usr/bin/phpize
This work for me:
FROM php:7.1.1-fpm
RUN apt-get update
RUN docker-php-ext-install mysqli
RUN apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install git -y
RUN git --version
RUN apt-get install -y build-essential libmemcached-dev
RUN git clone https://github.com/php-memcached-dev/php-memcached.git
RUN cd php-memcached \
&& git checkout php7 \
&& phpize \
&& ./configure --disable-memcached-sasl \
&& make \
&& make install
I want to build my PHP-FPM image with php-redis extension based on the official PHP Docker image, for example, using this Dockerfile: php:5.6-fpm.
The docs say that I can install extensions this way, installing dependencies for extensions manually:
FROM php:5.6-fpm
# Install modules (iconv, mcrypt and gd extensions)
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd
CMD ["php-fpm"]
Without Docker, I installed it with apt-get install php5-redis. But how can I install it using the approach above?
Redis is not an extension that is included in “php-src”, therefore you cannot use docker-php-ext-install. Use PECL:
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
On alpine php 7.3.5 we can use:
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
My opinion, the easiest way is:
RUN pecl install redis && docker-php-ext-enable redis
;)
Slightly revised version of starikovs and skyred answers for the current PHP 7 version of the docker image (tested on php:7.0.8-fpm-alpine and php:7.0.8-alpine).
Uses the newly released 3.0 version (June 2016) for PHP 7.
ENV PHPREDIS_VERSION 3.0.0
RUN mkdir -p /usr/src/php/ext/redis \
&& curl -L https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis
I've found two ways to install php-redis extension for official php-fpm Docker image. Here they are:
The first way is to compile redis from sources and install.
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-2.2.7 /usr/src/php/ext/redis \
&& docker-php-ext-install redis
docker-php-ext-install script is included in php-fpm image and can compile extensions and install them.
The second way you can do it is with PECL.
As TimWolla answered, you can do it with PECL, but in my case, PECL isn't installed by default.
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
Based on #starikovs answer. I added a variable for docker style.
# install phpredis extension
ENV PHPREDIS_VERSION 2.2.7
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis
If you want to use redis as session handler;
RUN { \
echo 'session.save_handler = redis'; \
echo 'session.save_path = tcp://redis:6379'; \
} >> /usr/local/etc/php/conf.d/docker-php-ext-redis.ini
If you want to use redis extension with PHP 7 in 2015 (borrowed from skyred's answer);
ENV PHPREDIS_VERSION php7
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis
This works for alpine images:
RUN set -xe \
&& apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install -o -f redis \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini \
&& rm -rf /usr/share/php \
&& rm -rf /tmp/* \
&& apk del .phpize-deps
Edit: Added missing backslash
Tried few ways. On alpine php 7.3.5 we can use:
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
I'm using combination of PECL and PHP official docker extension script
RUN pecl bundle -d /usr/src/php/ext redis \
&& rm /usr/src/php/ext/redis-*.tgz \
&& docker-php-ext-install redis
For PHP7 you need to wait for official redis pecl release or use git:
RUN apt-get update \
&& apt-get install git -y -q \
&& git clone -b php7 https://github.com/phpredis/phpredis.git /usr/src/php/ext/redis \
&& docker-php-ext-install redis
Slightly revised version of starikovs and skyred answers for current version of the docker image.
Tested on php:5-fpm-alpine
# install phpredis extension
ENV PHPREDIS_VERSION 2.2.8
ADD https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz /tmp/redis.tar.gz
RUN tar xzf /tmp/redis.tar.gz -C /tmp \
&& mkdir -p /usr/src/php/ext \
&& mv /tmp/phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis \
&& rm -rf /usr/src/php/ext/redis
In your Dockerfile you can clone the repo and install it with:
RUN git clone https://github.com/phpredis/phpredis.git /tmp/phpredis \
&& cd /tmp/phpredis \
&& git checkout -b 3.1.2 \ ## or the release you need #
&& phpize \
&& ./configure \
&& make \
&& make install
For image php:7.2-fpm-alpine.
RUN apk add autoconf gcc g++ make && pecl install redis && docker-php-ext-enable redis
You may need to update before
apk --update upgrade