From 29d88893fa4b9d37be2cdbd397f04946c07c73d0 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 9 Feb 2021 12:00:00 +0000 Subject: [PATCH] Add Docker and Docker Compose --- .docker.env.example | 4 ++ .dockerignore | 7 ++ .gitignore | 6 +- docker-compose.yaml | 69 ++++++++++++++++++++ tools/assets/development/.gitignore | 1 + tools/docker/Dockerfile | 35 ++++++++++ tools/docker/images/nginx/configs/vhost.conf | 19 ++++++ tools/docker/images/php/configs/php.ini | 2 + web/sites/default/settings.docker.php | 16 +++++ web/sites/default/settings.php | 3 + web/themes/custom/opdavies/webpack.config.js | 1 + 11 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 .docker.env.example create mode 100644 .dockerignore create mode 100644 docker-compose.yaml create mode 100644 tools/assets/development/.gitignore create mode 100644 tools/docker/Dockerfile create mode 100644 tools/docker/images/nginx/configs/vhost.conf create mode 100644 tools/docker/images/php/configs/php.ini create mode 100644 web/sites/default/settings.docker.php diff --git a/.docker.env.example b/.docker.env.example new file mode 100644 index 0000000..d6dd1a4 --- /dev/null +++ b/.docker.env.example @@ -0,0 +1,4 @@ +MYSQL_DATABASE=drupal +MYSQL_PASSWORD=drupal +MYSQL_ROOT_PASSWORD=root +MYSQL_USER=drupal diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ea3f4b4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +/**/*.{md,txt} +/.git-blame-ignore-revs +/.git/ +/.github/ +/.gitignore +/.idea/ +/.notes/ diff --git a/.gitignore b/.gitignore index fb87d60..20e9122 100644 --- a/.gitignore +++ b/.gitignore @@ -2,12 +2,15 @@ !*/ !/.gitignore !/**/.gitkeep +!/.docker.env.example +!/.dockerignore !/.github/** !/.idea/ !/assets/** !/bin/ !/config/** -!/Makefile +!/Dockerfile +!/docker-compose.yaml !/phpcs.xml.dist !/phpstan.neon !/phpunit.xml.dist @@ -17,6 +20,7 @@ !/web/modules/custom/** !/web/sites/default/environments/settings.*.php !/web/sites/default/settings.php +!/web/sites/default/settings.docker.php !/web/themes/custom/** /.idea/workspace.xml /node_modules/ diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8dae266 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,69 @@ +version: '2.4' + +volumes: + mysql_data: + +networks: + web: + external: + name: traefik_proxy + +services: + nginx: + image: nginx:1-alpine + working_dir: /app + volumes: + - ./:/app + - ./tools/docker/images/nginx/configs/vhost.conf:/etc/nginx/conf.d/default.conf + depends_on: + node: + condition: service_started + php: + condition: service_started + networks: + default: {} + web: {} + labels: + - traefik.docker.network=traefik_proxy + - traefik.enable=true + - traefik.http.routers.oliverdavies.rule=Host(`oliverdavies.docker.localhost`) + + php: + build: + context: . + dockerfile: tools/docker/Dockerfile + target: dev + volumes: + - ./:/app + env_file: + - .docker.env + depends_on: + mysql: + condition: service_healthy + networks: + default: {} + labels: + - "traefik.enable=false" + + mysql: + image: mariadb:10 + volumes: + - mysql_data:/var/lib/mysql + - ./tools/docker/images/mysql/databases.sql:/docker-entrypoint-initdb.d/databases.sql + env_file: + - .docker.env + healthcheck: + test: ["CMD-SHELL", "bash", "-c", "echo > /dev/tcp/localhost/3306"] + interval: 1s + networks: + default: {} + labels: + - "traefik.enable=false" + + node: + image: node:12 + working_dir: /node + volumes: + - ./web:/node + labels: + - "traefik.enabled=false" diff --git a/tools/assets/development/.gitignore b/tools/assets/development/.gitignore new file mode 100644 index 0000000..6911f25 --- /dev/null +++ b/tools/assets/development/.gitignore @@ -0,0 +1 @@ +/*.sql* diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile new file mode 100644 index 0000000..c120f71 --- /dev/null +++ b/tools/docker/Dockerfile @@ -0,0 +1,35 @@ +FROM php:7.4-fpm-buster AS base + +RUN apt update -yqq && apt install -yqq \ + git \ + libpng-dev \ + mariadb-client \ + zip \ + zlib1g-dev \ + && docker-php-ext-install \ + exif \ + gd \ + pdo_mysql + +### + +FROM base AS dev + +WORKDIR /app + +ENV PATH="./bin:./vendor/bin:$PATH" + +RUN apt update -yqq \ + && apt install -yqq \ + pv \ + vim \ + && pecl install xdebug-2.9.0 \ + && docker-php-ext-enable xdebug + +COPY --from=composer:1 /usr/bin/composer /usr/bin/composer + +COPY composer.json composer.lock /app/ +COPY assets /app/assets +RUN composer install + +COPY tools/docker/images/php/configs/php.ini /usr/local/etc/php/conf.d/php.ini diff --git a/tools/docker/images/nginx/configs/vhost.conf b/tools/docker/images/nginx/configs/vhost.conf new file mode 100644 index 0000000..c615b75 --- /dev/null +++ b/tools/docker/images/nginx/configs/vhost.conf @@ -0,0 +1,19 @@ +server { + server_name _; + root /app/web; + + location / { + try_files $uri /index.php?$query_string; + } + + location ~ \.php(/|$) { + fastcgi_split_path_info ^(.+?\.php)(|/.*)$; + try_files $fastcgi_script_name =404; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param QUERY_STRING $query_string; + fastcgi_intercept_errors on; + fastcgi_pass php:9000; + } +} diff --git a/tools/docker/images/php/configs/php.ini b/tools/docker/images/php/configs/php.ini new file mode 100644 index 0000000..b59ac11 --- /dev/null +++ b/tools/docker/images/php/configs/php.ini @@ -0,0 +1,2 @@ +date.timezone=UTC +expose_php=Off diff --git a/web/sites/default/settings.docker.php b/web/sites/default/settings.docker.php new file mode 100644 index 0000000..74491aa --- /dev/null +++ b/web/sites/default/settings.docker.php @@ -0,0 +1,16 @@ + 'mysql', + 'host' => 'mysql', + 'database' => getenv('MYSQL_DATABASE'), + 'username' => getenv('MYSQL_USER'), + 'password' => getenv('MYSQL_PASSWORD'), + 'port' => '3306', + 'prefix' => '', + 'collation' => 'utf8mb4_general_ci', +]; + +$settings['trusted_host_patterns'] = [ + '^oliverdavies.docker.localhost$', +]; diff --git a/web/sites/default/settings.php b/web/sites/default/settings.php index be25020..e1b5d02 100644 --- a/web/sites/default/settings.php +++ b/web/sites/default/settings.php @@ -764,3 +764,6 @@ if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) { include $app_root . '/' . $site_path . '/settings.local.php'; } +if (file_exists('/.dockerenv') && file_exists($app_root . '/' . $site_path . '/settings.docker.php')) { + include $app_root . '/' . $site_path . '/settings.docker.php'; +} diff --git a/web/themes/custom/opdavies/webpack.config.js b/web/themes/custom/opdavies/webpack.config.js index b19a7ba..e596427 100644 --- a/web/themes/custom/opdavies/webpack.config.js +++ b/web/themes/custom/opdavies/webpack.config.js @@ -6,6 +6,7 @@ Encore .cleanupOutputBeforeBuild() .setOutputPath('build/') .setPublicPath('/themes/custom/opdavies/build') + .setManifestKeyPrefix('build/') .addEntry('app', '@/app.js')