From c006a3705bbc213e710c68c21f828a804dfa2932 Mon Sep 17 00:00:00 2001 From: Damien Retzinger Date: Tue, 2 Jul 2019 10:58:21 -0400 Subject: [PATCH] feat(docker): add initial docker architecture for Magento 2 local environment --- .gitignore | 1 + README.md | 20 +++++ blackfire/.env.sample | 4 + docker-compose.dev.yml | 20 +++++ docker-compose.yml | 58 ++++++++++++ docs/architecture.md | 0 env.docker.php | 112 ++++++++++++++++++++++++ nginx/1.17/alpine/Dockerfile | 14 +++ nginx/1.17/alpine/conf/default.conf | 19 ++++ nginx/1.17/alpine/conf/nginx.conf | 27 ++++++ nginx/1.17/alpine/conf/prod.sample.conf | 15 ++++ php/7.2/alpine-develop/Dockerfile | 51 +++++++++++ php/7.2/alpine-develop/conf/php.ini | 8 ++ php/7.2/alpine/Dockerfile | 40 +++++++++ php/7.2/alpine/conf/php.ini | 8 ++ 15 files changed, 397 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 blackfire/.env.sample create mode 100644 docker-compose.dev.yml create mode 100644 docker-compose.yml create mode 100644 docs/architecture.md create mode 100644 env.docker.php create mode 100644 nginx/1.17/alpine/Dockerfile create mode 100644 nginx/1.17/alpine/conf/default.conf create mode 100644 nginx/1.17/alpine/conf/nginx.conf create mode 100644 nginx/1.17/alpine/conf/prod.sample.conf create mode 100644 php/7.2/alpine-develop/Dockerfile create mode 100644 php/7.2/alpine-develop/conf/php.ini create mode 100644 php/7.2/alpine/Dockerfile create mode 100644 php/7.2/alpine/conf/php.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be8ae6d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..28f4d2c --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Mage2Docker + +## Prerequisites +* Docker +* Composer +* An existing Magento 2 project + +## Getting Started +This project assumes you have already created a Magento 2 project with composer. + +You can read the docs on the Magento website on [how to create a new Magento 2 project](https://devdocs.magento.com/guides/v2.3/install-gde/composer.html). We strongly recommend using composer. + +From your project's root directory (the directory above the Magento `app`) + +```bash +git clone https://www.github.com/graycoreio/mage2docker && cd mage2docker +docker-compose -p {your_project_name} -f docker-compose.yml -f docker-compose.dev.yml up +``` + +## Documentation diff --git a/blackfire/.env.sample b/blackfire/.env.sample new file mode 100644 index 0000000..b1b2d04 --- /dev/null +++ b/blackfire/.env.sample @@ -0,0 +1,4 @@ +BLACKFIRE_CLIENT_ID=SOME_ID +BLACKFIRE_CLIENT_TOKEN=SOME_TOKEN +BLACKFIRE_SERVER_ID=YOUR_ID +BLACKFIRE_SERVER_TOKEN=YOUR_TOKEN \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..31c1fcc --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,20 @@ +version : '3' + +services: + magento2: + volumes: &appvolumes + - appdata:/var/www/html + - ../app:/var/www/html/app:cached + - ../bin:/var/www/html/bin:cached + - ../lib:/var/www/html/lib:cached + - ../pub:/var/www/html/pub:delegated + - ../setup:/var/www/html/setup:cached + - ../update:/var/www/html/update:cached + - ../index.php:/var/www/html/index.php:cached + - ../nginx.conf.sample:/var/www/html/nginx.conf:cached + - ../composer.json:/var/www/html/composer.json:delegated + - ../composer.lock:/var/www/html/composer.lock:delegated + ingress: + volumes: *appvolumes +volumes: + appdata: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ec5e040 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,58 @@ +version : '3' + +services: + ingress: + build: ./nginx/1.17/alpine + ports: + - "80:8000" + - "443:8443" + networks: + - frontend + magento2: + build: ./php/7.2/alpine-develop + restart: always + networks: + - frontend + - backend + database: + image: mysql:5.7 + volumes: + - db_data:/var/lib/mysql + restart: always + environment: + MYSQL_ROOT_PASSWORD: magento2 + MYSQL_DATABASE: magento2 + MYSQL_USER: magento2 + MYSQL_PASSWORD: magento2 + networks: + - backend + cache: + image: redis:5.0 + networks: + - backend + fullpagecache: + image: redis:5.0 + networks: + - backend + sessioncache: + image: redis:5.0 + networks: + - backend + elasticsearch: + image: elasticsearch:5.6 + ports: + - "9200:9200" + - "9300:9300" + networks: + - backend + blackfire: + image: blackfire/blackfire + env_file: + - blackfire/.env + networks: + - backend +networks: + frontend: + backend: +volumes: + db_data: diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..e69de29 diff --git a/env.docker.php b/env.docker.php new file mode 100644 index 0000000..594ac3b --- /dev/null +++ b/env.docker.php @@ -0,0 +1,112 @@ + [ + 'frontName' => 'admin' + ], + 'crypt' => [ + 'key' => 'REPLACEME' + ], + 'db' => [ + 'table_prefix' => '', + 'connection' => [ + 'default' => [ + 'host' => 'database', + 'dbname' => 'magento2', + 'username' => 'magento2', + 'password' => 'magento2', + 'active' => '1', + 'model' => 'mysql4', + 'engine' => 'innodb', + 'initStatements' => 'SET NAMES utf8;' + ] + ] + ], + 'resource' => [ + 'default_setup' => [ + 'connection' => 'default' + ] + ], + 'x-frame-options' => 'SAMEORIGIN', + 'MAGE_MODE' => 'developer', + 'session' => [ + 'save' => 'redis', + 'redis' => [ + 'host' => 'sessioncache', + 'port' => '6379', + 'password' => '', + 'timeout' => '2.5', + 'persistent_identifier' => '', + 'database' => '0', + 'compression_threshold' => '2048', + 'compression_library' => 'gzip', + 'log_level' => '4', + 'max_concurrency' => '6', + 'break_after_frontend' => '5', + 'break_after_adminhtml' => '30', + 'first_lifetime' => '600', + 'bot_first_lifetime' => '60', + 'bot_lifetime' => '7200', + 'disable_locking' => '0', + 'min_lifetime' => '60', + 'max_lifetime' => '2592000', + 'sentinel_master' => '', + 'sentinel_servers' => '', + 'sentinel_connect_retries' => '5', + 'sentinel_verify_master' => '0' + ] + ], + 'cache' => [ + 'frontend' => [ + 'default' => [ + 'id_prefix' => '40d_', + 'backend' => 'Cm_Cache_Backend_Redis', + 'backend_options' => [ + 'server' => 'cache', + 'database' => '0', + 'port' => '6379', + 'password' => '', + 'compress_data' => '1', + 'compression_lib' => '' + ] + ], + 'page_cache' => [ + 'id_prefix' => '40d_', + 'backend' => 'Cm_Cache_Backend_Redis', + 'backend_options' => [ + 'server' => 'fullpagecache', + 'database' => '0', + 'port' => '6379', + 'password' => '', + 'compress_data' => '0', + 'compression_lib' => '' + ] + ] + ] + ], + 'lock' => [ + 'provider' => 'db', + 'config' => [ + 'prefix' => '' + ] + ], + 'cache_types' => [ + 'config' => 1, + 'layout' => 1, + 'block_html' => 1, + 'collections' => 1, + 'reflection' => 1, + 'db_ddl' => 1, + 'compiled_config' => 1, + 'eav' => 1, + 'customer_notification' => 1, + 'config_integration' => 1, + 'config_integration_api' => 1, + 'full_page' => 1, + 'config_webservice' => 1, + 'translate' => 1, + 'vertex' => 1 + ], + 'install' => [ + 'date' => 'Mon, 01 Jul 2019 18:06:52 +0000' + ] +]; diff --git a/nginx/1.17/alpine/Dockerfile b/nginx/1.17/alpine/Dockerfile new file mode 100644 index 0000000..80500ed --- /dev/null +++ b/nginx/1.17/alpine/Dockerfile @@ -0,0 +1,14 @@ +FROM nginx:1.17-alpine + +MAINTAINER Damien Retzinger + +RUN apk update && apk add openssl +RUN mkdir /etc/nginx/certs \ + && echo -e "\n\n\n\n\n\n\n" | openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/certs/nginx.key -out /etc/nginx/certs/nginx.crt + +COPY ./conf/nginx.conf /etc/nginx/ +COPY ./conf/default.conf /etc/nginx/conf.d/ + +EXPOSE 8443 + +WORKDIR /var/www/html diff --git a/nginx/1.17/alpine/conf/default.conf b/nginx/1.17/alpine/conf/default.conf new file mode 100644 index 0000000..168fa13 --- /dev/null +++ b/nginx/1.17/alpine/conf/default.conf @@ -0,0 +1,19 @@ +upstream fastcgi_backend { + server magento2:9000; +} + +server { + listen 8000; + return 301 https://$host$request_uri; +} + +server { + listen 8443 ssl; + + ssl_certificate /etc/nginx/certs/nginx.crt; + ssl_certificate_key /etc/nginx/certs/nginx.key; + + set $MAGE_ROOT /var/www/html; + + include /var/www/html/nginx[.]conf; +} \ No newline at end of file diff --git a/nginx/1.17/alpine/conf/nginx.conf b/nginx/1.17/alpine/conf/nginx.conf new file mode 100644 index 0000000..3a0ebf1 --- /dev/null +++ b/nginx/1.17/alpine/conf/nginx.conf @@ -0,0 +1,27 @@ +worker_processes 2; + +error_log /var/log/nginx/error.log debug; + +events { + +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main + '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 60; + + gzip on; + + include /etc/nginx/conf.d/*.conf; +} \ No newline at end of file diff --git a/nginx/1.17/alpine/conf/prod.sample.conf b/nginx/1.17/alpine/conf/prod.sample.conf new file mode 100644 index 0000000..719fc2b --- /dev/null +++ b/nginx/1.17/alpine/conf/prod.sample.conf @@ -0,0 +1,15 @@ +## This ngninx configuration should serve as an example for a production configuration of nginx. +## Do note that this configuration only serves non-encrypted connections. This is intentional; +## traffic within the DMZ should not need to be encrypted unless absolutely required. + +server { + listen 8000; + + set $MAGE_ROOT /var/www/html; + + include /var/www/html/nginx[.]conf; +} + +# upstream fastcgi_backend { +# server magento2:9001; +# } \ No newline at end of file diff --git a/php/7.2/alpine-develop/Dockerfile b/php/7.2/alpine-develop/Dockerfile new file mode 100644 index 0000000..ad72553 --- /dev/null +++ b/php/7.2/alpine-develop/Dockerfile @@ -0,0 +1,51 @@ +FROM php:7.2-fpm-alpine + +MAINTAINER Damien Retzinger + +RUN apk update && apk add \ + gzip \ + freetype-dev \ + icu-dev \ + libjpeg-turbo-dev \ + libpng-dev \ + libxslt-dev \ + lsof \ + curl-dev \ + libsodium-dev \ + mysql-client \ + vim \ + zip + +RUN docker-php-ext-configure \ + gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ + +# https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements-tech.html#required-php-extensions +RUN docker-php-ext-install \ + bcmath \ + gd \ + hash \ + intl \ + opcache \ + pdo_mysql \ + simplexml \ + soap \ + xsl \ + zip + +RUN curl -sS https://getcomposer.org/installer | \ + php -- --install-dir=/usr/local/bin --filename=composer + +# Blackfire +ENV current_os=alpine +RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \ + && curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/$current_os/amd64/$version \ + && mkdir -p /tmp/blackfire \ + && tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \ + && mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \ + && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \ + && rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz + + +COPY conf/php.ini /usr/local/etc/php/ + +WORKDIR /var/www/html \ No newline at end of file diff --git a/php/7.2/alpine-develop/conf/php.ini b/php/7.2/alpine-develop/conf/php.ini new file mode 100644 index 0000000..a32aa33 --- /dev/null +++ b/php/7.2/alpine-develop/conf/php.ini @@ -0,0 +1,8 @@ +memory_limit = 4G +max_execution_time = 30 +zlib.output_compression = On +display_errors = On + +date.timezone = UTC +upload_max_filesize = 20M +post_max_size = 20M \ No newline at end of file diff --git a/php/7.2/alpine/Dockerfile b/php/7.2/alpine/Dockerfile new file mode 100644 index 0000000..d7a0926 --- /dev/null +++ b/php/7.2/alpine/Dockerfile @@ -0,0 +1,40 @@ +FROM php:7.2-fpm-alpine + +MAINTAINER Damien Retzinger + +RUN apk update && apk add \ + gzip \ + freetype-dev \ + icu-dev \ + libjpeg-turbo-dev \ + libpng-dev \ + libxslt-dev \ + lsof \ + curl-dev \ + libsodium-dev \ + mysql-client \ + vim \ + zip + +RUN docker-php-ext-configure \ + gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ + +# https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements-tech.html#required-php-extensions +RUN docker-php-ext-install \ + bcmath \ + gd \ + hash \ + intl \ + opcache \ + pdo_mysql \ + simplexml \ + soap \ + xsl \ + zip + +RUN curl -sS https://getcomposer.org/installer | \ + php -- --install-dir=/usr/local/bin --filename=composer + +COPY conf/php.ini /usr/local/etc/php/ + +WORKDIR /var/www/html \ No newline at end of file diff --git a/php/7.2/alpine/conf/php.ini b/php/7.2/alpine/conf/php.ini new file mode 100644 index 0000000..b998ba3 --- /dev/null +++ b/php/7.2/alpine/conf/php.ini @@ -0,0 +1,8 @@ +memory_limit = 2G +max_execution_time = 30 +zlib.output_compression = On +display_errors = On + +date.timezone = UTC +upload_max_filesize = 20M +post_max_size = 20M \ No newline at end of file