From a23b17b86a6a7adff881e60ead89842bb78f413e Mon Sep 17 00:00:00 2001 From: Julien De Conti Date: Thu, 12 Apr 2018 17:41:55 +0200 Subject: [PATCH 1/3] code(customization): general docker improvements * minimize RUN number (perf) * add container names * add ports forwarding * upgrade debian 8 to 9 (and to a lighter version) * upgrade PHP 7.1 to 7.2 * fix permission with /etc/hosts --- README.md | 2 +- docker-compose.yml | 8 ++++++++ nginx/Dockerfile | 12 ++++++------ php7-fpm/Dockerfile | 39 +++++++++++++++++++-------------------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d4d4db14..f0a08a72 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Docker-symfony gives you everything you need for developing Symfony application. ```bash # UNIX only: get containers IP address and update host (replace IP according to your configuration) (on Windows, edit C:\Windows\System32\drivers\etc\hosts) - $ sudo echo $(docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+') "symfony.local" >> /etc/hosts + $ echo $(docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+') 'symfony.local' | sudo tee --append /etc/hosts ``` **Note:** For **OS X**, please take a look [here](https://docs.docker.com/docker-for-mac/networking/) and for **Windows** read [this](https://docs.docker.com/docker-for-windows/#/step-4-explore-the-application-and-run-examples) (4th step). diff --git a/docker-compose.yml b/docker-compose.yml index 90cd989e..0dcd7863 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ version: '2' services: db: image: mysql + container_name: docker-symfony-mysql volumes: - "./.data/db:/var/lib/mysql" environment: @@ -10,16 +11,22 @@ services: MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} + ports: + - 3306:3306 php: build: context: php7-fpm args: TIMEZONE: ${TIMEZONE} + container_name: docker-symfony-php volumes: - ${SYMFONY_APP_PATH}:/var/www/symfony - ./logs/symfony:/var/www/symfony/app/logs + ports: + - 9000:9000 nginx: build: nginx + container_name: docker-symfony-nginx ports: - 80:80 volumes_from: @@ -28,6 +35,7 @@ services: - ./logs/nginx/:/var/log/nginx elk: image: willdurand/elk + container_name: docker-symfony-elk ports: - 81:80 volumes: diff --git a/nginx/Dockerfile b/nginx/Dockerfile index 8011b3ab..7799a764 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -1,15 +1,15 @@ -FROM debian:jessie +FROM debian:stretch-slim -MAINTAINER Maxence POUTORD +LABEL creator="Maxence POUTORD " +LABEL maintainer="Julien DE CONTI " -RUN apt-get update && apt-get install -y \ - nginx +RUN apt-get update && apt-get install -y nginx ADD nginx.conf /etc/nginx/ ADD symfony.conf /etc/nginx/sites-available/ -RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/symfony -RUN rm /etc/nginx/sites-enabled/default +RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/symfony \ + && rm /etc/nginx/sites-enabled/default RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf diff --git a/php7-fpm/Dockerfile b/php7-fpm/Dockerfile index 6737810b..01dcb87b 100644 --- a/php7-fpm/Dockerfile +++ b/php7-fpm/Dockerfile @@ -1,8 +1,9 @@ # See https://github.com/docker-library/php/blob/master/7.1/fpm/Dockerfile -FROM php:7.1-fpm +FROM php:7.2-fpm ARG TIMEZONE -MAINTAINER Maxence POUTORD +LABEL creator="Maxence POUTORD " +LABEL maintainer="Julien DE CONTI " RUN apt-get update && apt-get install -y \ openssl \ @@ -10,31 +11,29 @@ RUN apt-get update && apt-get install -y \ unzip # Install Composer -RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer -RUN composer --version +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ + && composer --version # Set timezone -RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone -RUN printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini -RUN "date" +RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone \ + && printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini \ + && "date" # Type docker-php-ext-install to see available extensions RUN docker-php-ext-install pdo pdo_mysql # 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 echo 'alias sf="php app/console"' >> ~/.bashrc -RUN echo 'alias sf3="php bin/console"' >> ~/.bashrc +RUN pecl install xdebug && docker-php-ext-enable xdebug +RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + +RUN echo 'alias sf="php app/console"' >> ~/.bashrc \ + && echo 'alias sf3="php bin/console"' >> ~/.bashrc WORKDIR /var/www/symfony From 6fa74fb78f8cafacc73a5ebe3419290bacc23d88 Mon Sep 17 00:00:00 2001 From: Julien De Conti Date: Fri, 13 Apr 2018 11:18:58 +0200 Subject: [PATCH 2/3] feat(customization): add the ability to customize the server name * add an environment variable to customize the server name (SYMFONY_NGINX_HOST) * use the official nginx image: lighter + works withour Dockerfile --- .env.dist | 3 +- README.md | 4 +-- docker-compose.yml | 6 +++- nginx/Dockerfile | 21 -------------- nginx/{symfony.conf => default.template.conf} | 6 ++-- nginx/nginx.conf | 29 ------------------- 6 files changed, 13 insertions(+), 56 deletions(-) delete mode 100644 nginx/Dockerfile rename nginx/{symfony.conf => default.template.conf} (79%) delete mode 100644 nginx/nginx.conf diff --git a/.env.dist b/.env.dist index ee55e182..480d1c6b 100644 --- a/.env.dist +++ b/.env.dist @@ -1,5 +1,6 @@ -# Symfony application's path (absolute or relative) +# Symfony application SYMFONY_APP_PATH=../path/to/symfony/folder +SYMFONY_NGINX_HOST=symfony.local # MySQL MYSQL_ROOT_PASSWORD=root diff --git a/README.md b/README.md index f0a08a72..27a238ac 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ Docker-symfony gives you everything you need for developing Symfony application. $ docker-compose up -d ``` -3. Update your system host file (add symfony.local) +3. Update your system host file (add value you've assigned to `SYMFONY_NGINX_HOST` ENV variable) ```bash # UNIX only: get containers IP address and update host (replace IP according to your configuration) (on Windows, edit C:\Windows\System32\drivers\etc\hosts) - $ echo $(docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+') 'symfony.local' | sudo tee --append /etc/hosts + $ echo $(docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+') 'YOUR_HOSTNAME' | sudo tee --append /etc/hosts ``` **Note:** For **OS X**, please take a look [here](https://docs.docker.com/docker-for-mac/networking/) and for **Windows** read [this](https://docs.docker.com/docker-for-windows/#/step-4-explore-the-application-and-run-examples) (4th step). diff --git a/docker-compose.yml b/docker-compose.yml index 0dcd7863..c1d81d15 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,14 +25,18 @@ services: ports: - 9000:9000 nginx: - build: nginx + image: nginx container_name: docker-symfony-nginx ports: - 80:80 volumes_from: - php volumes: + - ./nginx/default.template:/etc/nginx/conf.d/default.template - ./logs/nginx/:/var/log/nginx + environment: + - SYMFONY_NGINX_HOST=${SYMFONY_NGINX_HOST} + command: /bin/sh -c "envsubst '$$SYMFONY_NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" elk: image: willdurand/elk container_name: docker-symfony-elk diff --git a/nginx/Dockerfile b/nginx/Dockerfile deleted file mode 100644 index 7799a764..00000000 --- a/nginx/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM debian:stretch-slim - -LABEL creator="Maxence POUTORD " -LABEL maintainer="Julien DE CONTI " - -RUN apt-get update && apt-get install -y nginx - -ADD nginx.conf /etc/nginx/ -ADD symfony.conf /etc/nginx/sites-available/ - -RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/symfony \ - && rm /etc/nginx/sites-enabled/default - -RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf - -RUN usermod -u 1000 www-data - -CMD ["nginx"] - -EXPOSE 80 -EXPOSE 443 diff --git a/nginx/symfony.conf b/nginx/default.template.conf similarity index 79% rename from nginx/symfony.conf rename to nginx/default.template.conf index f17c5539..ab8b792b 100644 --- a/nginx/symfony.conf +++ b/nginx/default.template.conf @@ -1,5 +1,7 @@ server { - server_name symfony.local; + listen 80 default_server; + listen [::]:80 default_server; + server_name ${SYMFONY_NGINX_HOST}; root /var/www/symfony/web; location / { @@ -11,7 +13,7 @@ server { } location ~ ^/(app|app_dev|config)\.php(/|$) { - fastcgi_pass php-upstream; + fastcgi_pass php:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; diff --git a/nginx/nginx.conf b/nginx/nginx.conf deleted file mode 100644 index a9ecf0e5..00000000 --- a/nginx/nginx.conf +++ /dev/null @@ -1,29 +0,0 @@ -user www-data; -worker_processes 4; -pid /run/nginx.pid; - -events { - worker_connections 2048; - multi_accept on; - use epoll; -} - -http { - server_tokens off; - sendfile on; - tcp_nopush on; - tcp_nodelay on; - keepalive_timeout 15; - types_hash_max_size 2048; - include /etc/nginx/mime.types; - default_type application/octet-stream; - access_log off; - error_log off; - gzip on; - gzip_disable "msie6"; - include /etc/nginx/conf.d/*.conf; - include /etc/nginx/sites-enabled/*; - open_file_cache max=100; -} - -daemon off; From 22ed40223ff9c0bccda288b0372d1116eed864cd Mon Sep 17 00:00:00 2001 From: Julien De Conti Date: Thu, 12 Apr 2018 17:44:53 +0200 Subject: [PATCH 3/3] code(customization): improve symfony integration * install intl (sf dependency) * install opcache (perf) * install the symfony application to init projects * adapt log configs to work with sf3 --- docker-compose.yml | 3 ++- elk/logstash/logstash.conf | 4 ++-- php7-fpm/Dockerfile | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c1d81d15..57d0a3c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,7 +21,8 @@ services: container_name: docker-symfony-php volumes: - ${SYMFONY_APP_PATH}:/var/www/symfony - - ./logs/symfony:/var/www/symfony/app/logs + - ./logs/symfony:/var/www/symfony/var/logs + - ${SYMFONY_APP_PATH}:/var/www/symfony/var/logs ports: - 9000:9000 nginx: diff --git a/elk/logstash/logstash.conf b/elk/logstash/logstash.conf index f9e14bd5..314d5f56 100644 --- a/elk/logstash/logstash.conf +++ b/elk/logstash/logstash.conf @@ -6,12 +6,12 @@ input { } file { type => "symfony_dev" - path => "/var/www/symfony/app/logs/dev.log" + path => "/var/www/symfony/var/logs/dev.log" start_position => beginning } file { type => "symfony_prod" - path => "/var/www/symfony/app/logs/prod.log" + path => "/var/www/symfony/var/logs/prod.log" start_position => beginning } } diff --git a/php7-fpm/Dockerfile b/php7-fpm/Dockerfile index 01dcb87b..ec321a50 100644 --- a/php7-fpm/Dockerfile +++ b/php7-fpm/Dockerfile @@ -8,20 +8,31 @@ LABEL maintainer="Julien DE CONTI " RUN apt-get update && apt-get install -y \ openssl \ git \ - unzip + unzip \ + libicu-dev # Install Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ && composer --version +# Install Symfony +RUN curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony \ + && chmod a+x /usr/local/bin/symfony + # Set timezone RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone \ && printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini \ && "date" # Type docker-php-ext-install to see available extensions +# Install pdo mysql RUN docker-php-ext-install pdo pdo_mysql +# Install symfony php-intl dependency +RUN docker-php-ext-configure intl && docker-php-ext-install intl + +# Install php-opcache +RUN docker-php-ext-install opcache && docker-php-ext-enable opcache # install xdebug RUN pecl install xdebug && docker-php-ext-enable xdebug @@ -33,7 +44,6 @@ RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-x && echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini -RUN echo 'alias sf="php app/console"' >> ~/.bashrc \ - && echo 'alias sf3="php bin/console"' >> ~/.bashrc +RUN echo 'alias sf="php bin/console"' >> ~/.bashrc WORKDIR /var/www/symfony