Docker compose for Nginx/PHP-FPM/Composer - php

I am trying to setup Docker for my application relying on an LEMP stack. To this end, I plan to use Docker compose in order to spawn one container for Nginx, one for PHP-FPM and one for MySQL.
This is all working well for the basic illustration use-cases found in the online tutorials, but when I try to apply it to my use-case, I struggle with a design issue.
To give a bit of context, my web application relies on Composer for PHP dependencies and Gulp+Bower for CSS/JS dependencies (and LESS compilation, assets minimization, etc.).
The problem is that I need to build the application (i.e. install all dependencies and run some gulp tasks) and provide the result of this build to both Nginx and PHP-FPM containers.
Here is what I have come to so far:
docker-compose.yml:
version: "3"
networks:
database:
server:
volumes:
mysql-data:
source:
services:
php:
build: .
volumes:
- source:/app:ro
restart: always
networks:
- database
- server
depends_on:
- mysql
mysql:
image: mysql:5.7
volumes:
- mysql-data:/var/lib/mysql
restart: always
networks:
- database
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_DATABASE: test
nginx:
image: nginx
volumes:
- source:/app:ro
restart: always
networks:
- server
depends_on:
- php
Dockerfile:
FROM php:7.1-fpm
WORKDIR /app
# Install dependencies
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y \
git \
nodejs \
zip
RUN curl -sL https://getcomposer.org/installer | php -- --install-dir /usr/bin --filename composer
# Get application dependencies
COPY composer.json ./
RUN composer install -o
COPY package.json gulpfile.js bower.json ./
RUN npm install
RUN npm run gulp
RUN npm run rev
# Copy application
COPY . ./
However, as a Docker beginner, I am not sure it is right to rely on a volume for the build result of the application, or to have the build steps part of one of the containers running the application.
Thanks in advance for any help or advice!
Nicolas

However, as a Docker beginner, I am not sure it is right to rely on a volume for the build result of the application, or to have the build steps part of one of the containers running the application.
Short answer: Named volumes are the correct way to handle this.
In previous versions of compose, you would use the volumes-from option to reference an existing volume in a different container. This is deprecated in v3, being replaced by named volumes which you appear to be implementing correctly. I mention volumes-from because I think it does a good job of showing the intention of volumes - to persist data between containers - where named volumes is a bit less descriptive.

Look this for PHP7-FPM - NGINX - MySQL - ELK : https://github.com/maxpou/docker-symfony
And this for container management : https://portainer.io
My custom docker file with Node / Gulp
/php7-fpm/Dockerfile
FROM php:7.0-fpm
RUN apt-get update && apt-get install -y \
git \
unzip \
wget \
ntp \
gnupg
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN npm config set registry "http://registry.npmjs.org/"
RUN npm install -g gulp bower
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version
# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"
# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo_mysql
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-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
# install xdebug
#RUN pecl install xdebug
#RUN docker-php-ext-enable xdebug
#RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN apt-get update \
&& apt-get -y install \
libmagickwand-dev \
--no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& rm -r /var/lib/apt/lists/*
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
RUN echo "realpath_cache_ttl = 7200\n" \
"realpath_cache_size = 4M\n" \
> /usr/local/etc/php/conf.d/opti-symfony.ini
RUN echo "date.timezone=Europe/Paris" \
> /usr/local/etc/php/conf.d/time-zone.ini
RUN docker-php-ext-install opcache
WORKDIR /var/www/symfony
If you have an error gives us ;)
Happy Docker

Related

"deploy-php-ext-configure" in /bin/sh missing when building docker laravel image

i am trying to build image and container from a project but i am having an issue when trying to build the image. the project is a template and I already have a project using this template in my computer. I would like to know how to solve it and understand a little more. I hope somebody would help me.
here is the error log when I run the command "docker build -t my-payments ." :
#7 200.3 /bin/sh: 1: deploy-php-ext-configure: not found
here is the "docker-compose.yml" :
version: '3.8'
services:
web:
container_name: laravel-template-...
build:
context: ./
dockerfile: ./Dockerfile.dev
volumes:
- .:/var/www-data
environment:
- PORT=80
ports:
- 81:80
links:
- db
db:
image: postgres:13
container_name: db-lrv-postgres-...
restart: always
environment:
POSTGRES_DB: db-lrv-postgres
POSTGRES_USER: ...
POSTGRES_PASSWORD: ...
TZ: ...
PORT: ${PORT:-5432}
ports:
- "5434:5432"
here is "Dockerfile" :
FROM php:7.4-fpm
USER root
WORKDIR /var/www-data
RUN apt-get update \
# gd
&& apt-get install -y --no-install-recommends build-essential openssl nginx libfreetype6-dev libjpeg-dev libpng-dev libwebp-dev zlib1g-dev libzip-dev gcc g++ make nano vim unzip curl git jpegoptim optipng pngquant gifsicle locales libonig-dev nodejs npm libpq-dev \
#custom-dependencies installed: libpq-dev
&& deploy-php-ext-configure gd \
&& deploy-php-ext-install gd \
# gmp
&& apt-get install -y --no-install-recommends libgmp-dev \
&& deploy-php-ext-install gmp \
# pdo
&& deploy-php-ext-install pdo \
# pdo_pgsql
&& deploy-php-ext-install pgsql pdo_pgsql mbstring \
# opcache
&& deploy-php-ext-enable opcache \
# zip
&& deploy-php-ext-install zip \
&& apt-get autoclean -y \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/pear/
COPY . /var/www-data
COPY ./deploy/php.ini /usr/local/etc/php/conf.d/local.ini
COPY ./deploy/nginx.conf /etc/nginx/nginx.conf
RUN mv .env.production .env
RUN chmod +rwx /var/www-data
RUN chmod -R 777 /var/www-data
#RUN npm install -g npm#latest
RUN npm install
RUN npm run prod
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --working-dir="/var/www-data"
RUN composer dump-autoload --working-dir="/var/www-data"
RUN php artisan optimize
EXPOSE 8080
RUN ["chmod", "+x", "./deploy/post_deploy.sh"]
CMD [ "sh", "./deploy/post_deploy.sh" ]

Laravel Sail container suddenly unreachable

A few days ago my sail environment was working just fine. Now it isn't. Currently I'm using Laravel to refactor a bunch of api's over from our legacy system. So the way it works is our legacy system makes an http request, using Guzzle, to Laravel which does the processing and then returns. I noticed on my local machine I am no longer able to hit my laravel project. I get an error of connection refused. I also noticed I'm not able to hit the application through my chrome browser either and I get ERR_EMPTY_RESPONSE, however, if I log into any of my other containers on the same network and try to ping my laravel container it works just fine. I'd also like to note I have a rabbitmq service in the same docker-compose.yml and I'm able to hit the ui portion of that just fine in my browser.
This is a shared project being used on both Mac and Windows. The one mac machine it's running on is fine. My windows machine is the only one that's experiencing this issue. Below is an image of the output from sail up -d
sail up -d output
See laravel.test Error??? That's never come up before. Also notice how the DockerFile is running on CACHED commands. Not sure if that's an issue, but it never did that before when building from scratch.
Current Configuration:
Windows 10
Docker Desktop 4.8.2
PHP 8.1
WSL2
Things I've tried (stupid or not)
Restart my machine
Add php_flag opcache.enable Off to my .htaccess
Add memory_limit = 1024M to my php.ini
Point to a different DockerFile
Change the port
Wipe all associate images and rebuild from scratch
docker-compose.yml:
version: '3'
services:
laravel.test:
build:
context: ./docker_config/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
- default
depends_on:
- rabbitmq3
rabbitmq3:
container_name: "rabbitmq"
image: rabbitmq:3-management
environment:
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
volumes:
- ./docker_config/rabbitmq_init/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
- ./docker_config/rabbitmq_init/definitions.json:/etc/rabbitmq/definitions.json:ro
ports:
# AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
networks:
default:
external: true
name: localdevstructure_default
sail:
driver: bridge
DockerFile:
FROM ubuntu:21.10
LABEL maintainer="Taylor Otwell"
ARG WWWGROUP
ARG NODE_VERSION=16
WORKDIR /var/www/html
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update \
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
&& mkdir -p ~/.gnupg \
&& chmod 600 ~/.gnupg \
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
&& apt-key adv --homedir ~/.gnupg --keyserver hkps://keyserver.ubuntu.com --recv-keys 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C \
&& echo "deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu impish main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
&& apt-get update \
&& apt-get install -y php8.1-cli php8.1-dev \
php8.1-pgsql php8.1-sqlite3 php8.1-gd \
php8.1-curl \
php8.1-imap php8.1-mysql php8.1-mbstring \
php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \
php8.1-intl php8.1-readline \
php8.1-ldap \
php8.1-msgpack php8.1-igbinary php8.1-redis php8.1-swoole \
php8.1-memcached php8.1-pcov php8.1-xdebug \
&& php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get install -y yarn \
&& apt-get install -y mysql-client \
&& apt-get install -y postgresql-client \
&& apt-get install -y vim \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.1
RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY highPriority-worker.conf /etc/supervisor/conf.d/highPriority-worker.conf
COPY lowPriority-worker.conf /etc/supervisor/conf.d/lowPriority-worker.conf
COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]
EDIT:
The only error I keep coming across is this:
"Error response from daemon: Get "https://sail-8.1/v2/": Failed to lookup host: sail-8.1"
I believe this relates to the error shown in the provided image for service laravel.test. What's happening is that service enters the "Pulling" stage and within a second errors out. So this is leading me to believe that something is happening during image creation
So I figured out what was happening and this was really stupid on my part. I thought that inherently supervisor isn't setup to autostart on local dev environments, but when using sail it is. In the supervisord.conf file that gets copied over to the container from local, there's a program in there to run the artisan serve command. Long story short I commented this out thinking it wasn't necessary. Didn't realize sail was using supervisor to keep that command running.

ENV caching issue on local development (PHP-FPM, NGINX & LARAVEL)

We are running a laravel application by docker. We are creating a docker container using PHP-FPM (php:7.0-fpm official image) & NGINX. We use a common .env file for both docker and laravel applications.
we up & running our application by - docker-compose --env-file=./.env -f docker-compose.yml up -d
But the problem is when we change our .env file, the application not detected the change. We want to get every .env file change without re-creating the container.
Dockerfile -
FROM php:7.0-fpm
# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"
# Install dependencies
RUN apt-get update && apt-get install -y \
nginx \
libpng-dev \
zlib1g-dev \
libsasl2-dev \
libssl-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libpng-dev \
libxpm-dev \
libvpx-dev \
libxml2-dev \
libicu-dev \
git
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
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-configure intl
RUN docker-php-ext-install -j$(nproc) pdo_mysql mbstring zip calendar soap gd intl
# Install mongodb extension
RUN pecl install mongodb-1.4.4 \
&& echo "extension=mongodb.so" > /usr/local/etc/php/conf.d/mongo.ini
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=1.8.6
RUN composer global require hirak/prestissimo
COPY entrypoint.sh /etc/entrypoint.sh
RUN chmod +x /etc/entrypoint.sh
RUN usermod -u 1000 www-data
# Set working directory
WORKDIR /var/www
EXPOSE 80 443
Docker Compose -
version: '3.5'
services:
api-service:
environment:
SERVICE_NAME: app
VIRTUAL_HOST: ${API_DOMAIN}
working_dir: /var/www
entrypoint: /etc/entrypoint.sh
volumes:
- ../../:/var/www
- ../nginx/conf.d/nginx-local.conf:/etc/nginx/sites-enabled/default
- ../php/local.ini:/usr/local/etc/php/conf.d/local.ini
- ../php/memory_limit.ini:/usr/local/etc/php/conf.d/memory_limit.ini
- ../php/php.ini:/usr/local/etc/php/conf.d/php.override.ini
- ../php/www.conf:/usr/local/etc/php-fpm.d/www.conf
ports:
- ${PORT}:80
networks:
api-service-network:
external:
name: ${EXTERNAL_NETWORK}
entrypoint -
#!/usr/bin/env bash
service nginx start
php-fpm
So is it possible to reload ENV (on local development by PHP-FPM) without recreating the container.
Try using env_file in docker-compose
Maybe can help you

How to install yarn and npm on a PHP docker image (symfony 4 project)

Im working on a symfony 4/posgresql project. Im using docker toolbox.
I need to install webpack encore bundle on symfony, but in order to do this, i need to add yarn and npm to my project. Somebody told me i should add theses 2 installations on my php docker container, but i don't know which command to add (im a linux/docker beginner).
This is my docker-compose.yaml :
services:
database:
image: postgres:11-alpine
ports:
- "5432:5432"
volumes:
- 'boeki_database:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: root
database_pg_admin:
image: dpage/pgadmin4
ports:
- "8001:80"
environment:
PGADMIN_DEFAULT_EMAIL: root#root.com
PGADMIN_DEFAULT_PASSWORD: root
application:
build:
context: .
dockerfile: ./docker/Dockerfile
working_dir: /var/www/project
ports:
- "8000:80"
volumes:
- ./:/var/www/project:rw,cached
- ./docker/http/000-default.conf:/etc/apache2/sites-enabled/000-default.conf:rw,cached
volumes:
boeki_database: {}
And this is my Dockerfile :
RUN apt-get update && \
apt-get install -y libpq-dev g++ zlib1g-dev libicu-dev vim git zip
#GD
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
poppler-utils
RUN rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install -j "$(nproc)" gd pdo_pgsql
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql intl zip opcache
RUN pecl install redis && echo "extension=redis.so" > /usr/local/etc/php/conf.d/docker-php-ext-redis.ini
RUN a2enmod rewrite
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Thanks for the help !
according to https://classic.yarnpkg.com/en/docs/install#debian-stable which provides some commands to follow, adding the following to your dockerfile should work (disclaimer: I'm not extremely familiar with docker)
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && sudo apt-get install -y yarn
(the -y removes the questions)

Setup a Docker container with only the docker-compose up command

I have an assessment in which I have to create a Docker container with CakePHP. I already have a working Docker container with CakePHP and I run the following commands for my container:
docker-compose build
docker-compose run cakephp composer install --no-interaction
docker-compose run cakephp bin/cake migrations migrate
docker-compose run cakephp bin/cake migrations seed
docker-compose up
The goal is to reduce the process down to only running the single command docker-compose up to be able to start testing the container. I'm very new to both Docker and CakePHP so I'm not sure how to do this.
Any help is greatly appreciated!
Dockerfile
#start with our base image (the foundation) - version 7.1.29
FROM php:7.1.29-apache
#install all the system dependencies and enable PHP modules
RUN apt-get update && apt-get install -y \
gcc \
make \
autoconf \
libc-dev \
pkg-config \
libicu-dev \
libpq-dev \
libmcrypt-dev \
mysql-client \
git \
zip \
unzip \
&& rm -r /var/lib/apt/lists/* \
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
&& docker-php-ext-install \
intl \
mbstring \
mcrypt \
pcntl \
pdo_mysql \
pdo_pgsql \
pgsql \
opcache
RUN set -eux; apt-get update; apt-get install -y libzip-dev zlib1g-dev; docker-php-ext-install zip
#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
ENV COMPOSER_ALLOW_SUPERUSER=1
#set our application folder as an environment variable
ENV APP_HOME /var/www/html
#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
#change the web_root to laravel /var/www/html/public folder
RUN sed -i -e "s/html/html\/webroot/g" /etc/apache2/sites-enabled/000-default.conf
# enable apache module rewrite
RUN a2enmod rewrite && \
echo "ServerName localhost" >> /etc/apache2/apache2.conf
#copy source files and run composer
COPY . $APP_HOME
# install all PHP dependencies
RUN composer install --no-interaction
#change ownership of our applications
RUN chown -R www-data:www-data $APP_HOME
docker-compose.yml
version: '2'
services:
cakephp:
build: .
depends_on:
- mysql
links:
- "mysql"
ports:
- "4000:80"
volumes:
- .:/var/www/html/
environment:
- SECURITY_SALT= *some salt here*
- MYSQL_URL=mysql
- MYSQL_USERNAME=root
- MYSQL_PASSWORD=root
mysql:
image: mysql:5.6
volumes:
- mysql-data:/var/lib/mysql
environment:
- MYSQL_DATABASE=cakephp
- MYSQL_ROOT_PASSWORD=root
volumes:
mysql-data:
Option 1
What I usually do to reduce my commands when handling Docker and Docker Compose in general is using a Makefile.
So, in your case, you could write something like:
Makefile
SUDO := $(shell groups | grep -q docker || echo sudo)
.PHONY: start
start:
$(SUDO) docker-compose build \
&& $(SUDO) docker-compose run cakephp composer install --no-interaction \
&& $(SUDO) docker-compose run cakephp bin/cake migrations migrate \
&& $(SUDO) docker-compose run cakephp bin/cake migrations seed \
&& $(SUDO) docker-compose up
All you would have to do then is putting this file in your project folder and run make start.
(The $(SUDO) part ensures that you can run this comfortably even with a user that is not in the docker group.)
Option 2
To really just run docker-compose up (perhaps with --build flag), you would have to write a little script that you COPY into the Docker image (you're already doing that with COPY . $APP_HOME – provided that you put this script at where your Docker build context points to) and then use it as ENTRYPOINT.
Something like this should work for you.
entrypoint.sh:
#!/bin/sh
set -e
cakephp composer install --no-interaction
cakephp bin/cake migrations migrate
cakephp bin/cake migrations seed
exec "$#"
In your Dockerfile, you would then have to put ENTRYPOINT ["/bin/sh", "entrypoint.sh"]

Categories