From 53ce83a272c636c4eccd043f16215d7475a2d8a9 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 14 Oct 2019 22:23:52 +0200 Subject: [PATCH 1/2] Moves docker files from contrib to docker --- README.md | 6 +++--- {contrib => docker}/.env | 0 {contrib => docker}/Dockerfile | 0 {contrib => docker}/docker-compose.yml | 4 ++-- {contrib => docker}/nginx/Dockerfile | 2 +- {contrib => docker}/nginx/nginx.conf | 0 6 files changed, 6 insertions(+), 6 deletions(-) rename {contrib => docker}/.env (100%) rename {contrib => docker}/Dockerfile (100%) rename {contrib => docker}/docker-compose.yml (91%) rename {contrib => docker}/nginx/Dockerfile (86%) rename {contrib => docker}/nginx/nginx.conf (100%) diff --git a/README.md b/README.md index 9b4a3aaf..889151e1 100644 --- a/README.md +++ b/README.md @@ -117,14 +117,14 @@ PRODUCTION_REMOTE_PATH # Same as STAGING_REMOTE_PATH but for the production env ### Docker container To build the `engelsystem` and the `engelsystem-nginx` container: ```bash -cd contrib +cd docker docker-compose build ``` or to build the containers separately ```bash -docker build -f contrib/nginx/Dockerfile . -t engelsystem-nginx -docker build -f contrib/Dockerfile . -t engelsystem +docker build -f docker/nginx/Dockerfile . -t engelsystem-nginx +docker build -f docker/Dockerfile . -t engelsystem ``` Import database diff --git a/contrib/.env b/docker/.env similarity index 100% rename from contrib/.env rename to docker/.env diff --git a/contrib/Dockerfile b/docker/Dockerfile similarity index 100% rename from contrib/Dockerfile rename to docker/Dockerfile diff --git a/contrib/docker-compose.yml b/docker/docker-compose.yml similarity index 91% rename from contrib/docker-compose.yml rename to docker/docker-compose.yml index 4624cce4..f4be4eb2 100644 --- a/contrib/docker-compose.yml +++ b/docker/docker-compose.yml @@ -4,7 +4,7 @@ services: image: engelsystem-nginx build: context: .. - dockerfile: contrib/nginx/Dockerfile + dockerfile: "" ports: - 5000:80 networks: @@ -15,7 +15,7 @@ services: image: engelsystem build: context: .. - dockerfile: contrib/Dockerfile + dockerfile: "" environment: MYSQL_HOST: database MYSQL_USER: engelsystem diff --git a/contrib/nginx/Dockerfile b/docker/nginx/Dockerfile similarity index 86% rename from contrib/nginx/Dockerfile rename to docker/nginx/Dockerfile index 9f5dcb3b..47b3d50e 100644 --- a/contrib/nginx/Dockerfile +++ b/docker/nginx/Dockerfile @@ -7,5 +7,5 @@ RUN yarn build FROM nginx:alpine RUN mkdir -p /var/www/public/ && touch /var/www/public/index.php -COPY contrib/nginx/nginx.conf /etc/nginx/nginx.conf +COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf COPY --from=themes /app/public/assets /var/www/public/assets/ diff --git a/contrib/nginx/nginx.conf b/docker/nginx/nginx.conf similarity index 100% rename from contrib/nginx/nginx.conf rename to docker/nginx/nginx.conf From 545eb291b6d32c613f814eefc8e80e39899b1e90 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 14 Oct 2019 23:36:26 +0200 Subject: [PATCH 2/2] Adds a docker dev setup --- .gitignore | 4 ++ .gitlab-ci.yml | 4 +- README.md | 70 ++++++++++++++++++++++-------- docker/dev/.env | 3 ++ docker/dev/Dockerfile | 19 +++++++++ docker/dev/docker-compose.yml | 80 +++++++++++++++++++++++++++++++++++ docker/docker-compose.yml | 20 ++++----- docker/nginx/Dockerfile | 8 ++-- docker/nginx/nginx.conf | 2 +- 9 files changed, 177 insertions(+), 33 deletions(-) create mode 100644 docker/dev/.env create mode 100644 docker/dev/Dockerfile create mode 100644 docker/dev/docker-compose.yml diff --git a/.gitignore b/.gitignore index cda5cf92..6089cc32 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,10 @@ _vimrc_local.vim /.idea/ /.phpstorm.meta.php +# Docker .env files +/docker/.env +/docker/dev/.env + # Project files /config/config.php /test/coverage diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e14b1841..2590a0b3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,7 +36,7 @@ build-image.nginx: paths: - ./public/assets script: - - docker build --pull -t "${TEST_IMAGE}-nginx" -f contrib/nginx/Dockerfile . + - docker build --pull -t "${TEST_IMAGE}-nginx" -f docker/nginx/Dockerfile . - docker push "${TEST_IMAGE}-nginx" - instance=$(docker create "${TEST_IMAGE}-nginx") - docker cp "${instance}:/var/www/public/assets" public/ @@ -48,7 +48,7 @@ build-image: script: - apk -q add git - VERSION="$(git describe --abbrev=0 --tags)-${CI_COMMIT_REF_NAME}+${CI_PIPELINE_ID}.${CI_COMMIT_SHORT_SHA}" - - docker build --pull --build-arg VERSION="${VERSION}" -t "${TEST_IMAGE}" -f contrib/Dockerfile . + - docker build --pull --build-arg VERSION="${VERSION}" -t "${TEST_IMAGE}" -f docker/Dockerfile . - docker push "${TEST_IMAGE}" test: diff --git a/README.md b/README.md index 889151e1..a1470e32 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,11 @@ PRODUCTION_REMOTE # Same as STAGING_REMOTE but for the production environm PRODUCTION_REMOTE_PATH # Same as STAGING_REMOTE_PATH but for the production environment ``` -### Docker container -To build the `engelsystem` and the `engelsystem-nginx` container: +### Docker + +#### Production + +To build the `es_nginx` and the `es_php_fpm` containers: ```bash cd docker docker-compose build @@ -123,30 +126,63 @@ docker-compose build or to build the containers separately ```bash -docker build -f docker/nginx/Dockerfile . -t engelsystem-nginx -docker build -f docker/Dockerfile . -t engelsystem +docker build -f docker/nginx/Dockerfile . -t es_nginx +docker build -f docker/Dockerfile . -t es_php_fpm ``` Import database ```bash -docker exec -it engelsystem bin/migrate +docker exec -it engelsystem_es_php_fpm_1 bin/migrate ``` -#### Local development -To use the working directory in the container the docker-compose file has to be changed: -```yaml -[...] - nginx: - volumes: - - ../public/assets:/var/www/public/assets -[...] - engelsystem: - volumes: - - ../:/var/www -[...] +#### Development + +This repo [ships a docker setup](docker/dev) for a quick development start. + +If you use another uid/gid than 1000 on your machine you have to adjust it in [docker/dev/.env](docker/dev/.env). + +Run this once + +```bash +cd docker/dev +docker-compose up ``` +Run these commands once initially and then as required after changes + +``` +# Install composer dependencies +docker exec -it engelsystem_dev_es_workspace_1 composer i + +# Install node packages +docker exec -it engelsystem_dev_es_workspace_1 yarn install + +# Run a front-end build +docker exec -it engelsystem_dev_es_workspace_1 yarn build + +# Update the translation files +docker exec -it engelsystem_dev_es_workspace_1 find /var/www/resources/lang -type f -name '*.po' -exec sh -c 'file="{}"; msgfmt "${file%.*}.po" -o "${file%.*}.mo"' \; + +# Run the migrations +docker exec -it engelsystem_dev_es_workspace_1 bin/migrate +``` + +While developing you may use the watch mode to rebuild the system on changes + +``` +# Run a front-end build +docker exec -it engelsystem_dev_es_workspace_1 yarn build:watch +``` + +##### Hint for using Xdebug with *PhpStorm* + +For some reason *PhpStorm* is unable to detect the server name. +But without a server name it's impossible to set up path mappings. +Because of that the docker setup sets the server name *engelsystem*. +To get Xdebug working you have to create a server with the name *engelsystem* manually. + #### Scripts + ##### bin/deploy.sh The `bin/deploy.sh` script can be used to deploy the engelsystem. It uses rsync to deploy the application to a server over ssh. diff --git a/docker/dev/.env b/docker/dev/.env new file mode 100644 index 00000000..f1622708 --- /dev/null +++ b/docker/dev/.env @@ -0,0 +1,3 @@ +COMPOSE_PROJECT_NAME="engelsystem_dev" +UID=1000 +GID=1000 diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile new file mode 100644 index 00000000..ef514102 --- /dev/null +++ b/docker/dev/Dockerfile @@ -0,0 +1,19 @@ +# Engelsystem PHP FPM development image including Xdebug +FROM php:7-fpm-alpine AS es_php_fpm +WORKDIR /var/www +RUN apk add --no-cache icu-dev $PHPIZE_DEPS && \ + pecl install xdebug && \ + docker-php-ext-install intl pdo_mysql && \ + docker-php-ext-enable xdebug +RUN echo -e "xdebug.remote_enable=1\nxdebug.remote_connect_back=1\n" >> /usr/local/etc/php/conf.d/xdebug.ini + +ENV TRUSTED_PROXIES 10.0.0.0/8,::ffff:10.0.0.0/8,\ + 127.0.0.0/8,::ffff:127.0.0.0/8,\ + 172.16.0.0/12,::ffff:172.16.0.0/12,\ + 192.168.0.0/16,::ffff:192.168.0.0/16,\ + ::1/128,fc00::/7,fec0::/10 + +# Engelsystem development workspace +# Contains all tools required to build / manage the system +FROM es_php_fpm AS es_workspace +RUN apk add --no-cache composer gettext nodejs npm yarn diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml new file mode 100644 index 00000000..858d6232 --- /dev/null +++ b/docker/dev/docker-compose.yml @@ -0,0 +1,80 @@ +version: "3.6" +services: + es_nginx: + image: es_dev_nginx + build: + context: ./../.. + dockerfile: docker/nginx/Dockerfile + target: es_nginx + volumes: + - ./../..:/var/www + ports: + - 5000:80 + networks: + - internal + depends_on: + - es_php_fpm + es_php_fpm: + image: es_dev_php_fpm + build: + context: ./../.. + dockerfile: docker/dev/Dockerfile + target: es_php_fpm + user: "${UID}:${GID}" + volumes: + - ./../..:/var/www + environment: + MYSQL_HOST: es_database + MYSQL_USER: engelsystem + MYSQL_PASSWORD: engelsystem + MYSQL_DATABASE: engelsystem + PHP_IDE_CONFIG: serverName=engelsystem + ENVIRONMENT: development + MAIL_DRIVER: log + APP_NAME: Engelsystem DEV + networks: + - internal + - database + depends_on: + - es_database + es_workspace: + image: es_dev_workspace + build: + context: ./../.. + dockerfile: docker/dev/Dockerfile + target: es_workspace + user: "${UID}:${GID}" + volumes: + - ./../..:/var/www + environment: + HOME: /tmp + MYSQL_HOST: es_database + MYSQL_USER: engelsystem + MYSQL_PASSWORD: engelsystem + MYSQL_DATABASE: engelsystem + ENVIRONMENT: development + MAIL_DRIVER: log + APP_NAME: Engelsystem DEV + networks: + - internal + - database + depends_on: + - es_database + es_database: + image: mariadb:10.2 + environment: + MYSQL_DATABASE: engelsystem + MYSQL_USER: engelsystem + MYSQL_PASSWORD: engelsystem + MYSQL_RANDOM_ROOT_PASSWORD: 1 + MYSQL_INITDB_SKIP_TZINFO: "yes" + volumes: + - db:/var/lib/mysql + networks: + - database +volumes: + db: {} + +networks: + internal: + database: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index f4be4eb2..47b85d56 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,23 +1,23 @@ version: "3.6" services: - nginx: - image: engelsystem-nginx + es_nginx: + image: es_nginx build: context: .. - dockerfile: "" + dockerfile: docker/nginx/Dockerfile ports: - 5000:80 networks: - internal depends_on: - - engelsystem - engelsystem: - image: engelsystem + - es_php_fpm + es_php_fpm: + image: es_php_fpm build: context: .. - dockerfile: "" + dockerfile: docker/Dockerfile environment: - MYSQL_HOST: database + MYSQL_HOST: es_database MYSQL_USER: engelsystem MYSQL_PASSWORD: engelsystem MYSQL_DATABASE: engelsystem @@ -25,8 +25,8 @@ services: - internal - database depends_on: - - database - database: + - es_database + es_database: image: mariadb:10.2 environment: MYSQL_DATABASE: engelsystem diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile index 47b3d50e..f4355af9 100644 --- a/docker/nginx/Dockerfile +++ b/docker/nginx/Dockerfile @@ -1,3 +1,7 @@ +FROM nginx:alpine as es_nginx +RUN mkdir -p /var/www/public/ && touch /var/www/public/index.php +COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf + FROM node:10-alpine as themes WORKDIR /app COPY .babelrc .browserslistrc package.json webpack.config.js yarn.lock /app/ @@ -5,7 +9,5 @@ RUN yarn install COPY resources/assets/ /app/resources/assets RUN yarn build -FROM nginx:alpine -RUN mkdir -p /var/www/public/ && touch /var/www/public/index.php -COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf +FROM es_nginx COPY --from=themes /app/public/assets /var/www/public/assets/ diff --git a/docker/nginx/nginx.conf b/docker/nginx/nginx.conf index d95c18e2..63e934bc 100644 --- a/docker/nginx/nginx.conf +++ b/docker/nginx/nginx.conf @@ -33,7 +33,7 @@ http { } location ~ \.php$ { - fastcgi_pass engelsystem:9000; + fastcgi_pass es_php_fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;