Adds a docker dev setup

This commit is contained in:
Michael Weimann 2019-10-14 23:36:26 +02:00
parent 53ce83a272
commit 545eb291b6
No known key found for this signature in database
GPG Key ID: 34F0524D4DA694A1
9 changed files with 177 additions and 33 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -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:

View File

@ -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.

3
docker/dev/.env Normal file
View File

@ -0,0 +1,3 @@
COMPOSE_PROJECT_NAME="engelsystem_dev"
UID=1000
GID=1000

19
docker/dev/Dockerfile Normal file
View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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/

View File

@ -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;