Completely untie the backend from the frontend.

Build nginx and mariadb together with the app image.
Upgrade docker-compose configuration to 3 version.
Add cleanup step to the gitlab-ci.
This commit is contained in:
ErickSkrauch 2019-04-06 15:52:23 +02:00
parent e13b6f0d94
commit f33439d829
21 changed files with 302 additions and 81 deletions

View File

@ -6,6 +6,3 @@ vendor
# Все временные файлы # Все временные файлы
*/runtime */runtime
# Их по идее их не должно образовываться, но мало ли
*/web/assets

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
# Fix EOL for all files, that should be executed inside Docker container
*.sh text eol=lf
yii text eol=lf

View File

@ -2,9 +2,13 @@ stages:
- test - test
- build - build
- release - release
- cleanup
variables: variables:
CONTAINER_IMAGE: "registry.ely.by/elyby/accounts" VERSION: "${CI_COMMIT_TAG:-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA:0:7}}"
APP_IMAGE_NAME: "registry.ely.by/elyby/accounts"
WEB_IMAGE_NAME: "registry.ely.by/elyby/accounts-nginx"
DB_IMAGE_NAME: "registry.ely.by/elyby/accounts-mariadb"
php-cs-fixer: php-cs-fixer:
image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine
@ -55,15 +59,28 @@ build:production:
image: docker:18.02 image: docker:18.02
stage: build stage: build
before_script: before_script:
- docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by - sed -i -e "s/{{PLACE_VERSION_HERE}}/$VERSION/g" common/config/config.php
- export VERSION="${CI_COMMIT_TAG:-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA:0:7}}"
- echo "$SSH_PRIVATE_KEY" > id_rsa
- sed -i"" -e "s/{{PLACE_VERSION_HERE}}/$VERSION/g" common/config/config.php
script: script:
- export IMAGE_NAME="$CONTAINER_IMAGE:latest" - >
- docker build --pull --build-arg build_env=prod -t $IMAGE_NAME . docker build
--pull
--target app
--build-arg "build_env=prod"
-t "$APP_IMAGE_NAME:$CI_PIPELINE_ID" .
- >
docker build
--pull
--target web
--build-arg "build_env=prod"
-t "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" .
- >
docker build
--pull
--target db
--build-arg "build_env=prod"
-t "$DB_IMAGE_NAME:$CI_PIPELINE_ID" .
only: only:
- develop - master
- tags - tags
release:latest: release:latest:
@ -74,9 +91,16 @@ release:latest:
before_script: before_script:
- docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by - docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by
script: script:
- docker push $CONTAINER_IMAGE:latest - docker tag "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:latest"
- docker push "$APP_IMAGE_NAME:latest"
- docker tag "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:latest"
- docker push "$WEB_IMAGE_NAME:latest"
- docker tag "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:latest"
- docker push "$DB_IMAGE_NAME:latest"
only: only:
- develop - master
- tags - tags
release:tag: release:tag:
@ -87,8 +111,21 @@ release:tag:
before_script: before_script:
- docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by - docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by
script: script:
- export IMAGE_NAME="$CONTAINER_IMAGE:$CI_BUILD_TAG" - docker tag "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:$VERSION"
- docker tag $CONTAINER_IMAGE:latest $IMAGE_NAME - docker push "$APP_IMAGE_NAME:$VERSION"
- docker push $IMAGE_NAME
- docker tag "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:$VERSION"
- docker push "$WEB_IMAGE_NAME:$VERSION"
- docker tag "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:$VERSION"
- docker push "$DB_IMAGE_NAME:$VERSION"
only: only:
- tags - tags
cleanup:
stage: cleanup
script:
- docker rmi "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:$VERSION" "$APP_IMAGE_NAME:latest" || true
- docker rmi "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:$VERSION" "$WEB_IMAGE_NAME:latest" || true
- docker rmi "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:$VERSION" "$DB_IMAGE_NAME:latest" || true
when: always

View File

@ -63,9 +63,57 @@ COPY ./common /var/www/html/common/
COPY ./console /var/www/html/console/ COPY ./console /var/www/html/console/
COPY ./yii /var/www/html/yii COPY ./yii /var/www/html/yii
# Expose everything under /var/www/html to share it with nginx
VOLUME ["/var/www/html"]
WORKDIR /var/www/html
ENTRYPOINT ["docker-entrypoint.sh"] ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["php-fpm"] CMD ["php-fpm"]
# ================================================================================
FROM nginx:1.15.10-alpine AS web
ENV PHP_SERVERS php:9000
RUN rm /etc/nginx/conf.d/default.conf \
&& mkdir -p /data/nginx/cache \
&& mkdir -p /var/www/html
WORKDIR /var/www/html
COPY ./docker/nginx/docker-entrypoint.sh /
COPY ./docker/nginx/generate-upstream.sh /usr/bin/generate-upstream
COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/nginx/account.ely.by.conf.template /etc/nginx/conf.d/
COPY --from=app /var/www/html/vendor/ely/email-renderer/dist/assets /var/www/html/vendor/ely/email-renderer/dist/assets
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
# ================================================================================
FROM mariadb:10.3.14-bionic AS db
COPY ./docker/mariadb/config.cnf /etc/mysql/conf.d/
RUN set -ex \
&& fetchDeps='ca-certificates wget' \
&& apt-get update \
&& apt-get install -y --no-install-recommends $fetchDeps \
&& rm -rf /var/lib/apt/lists/* \
&& wget -O /mysql-sys.tar.gz 'https://github.com/mysql/mysql-sys/archive/1.5.1.tar.gz' \
&& mkdir /mysql-sys \
&& tar -zxf /mysql-sys.tar.gz -C /mysql-sys \
&& rm /mysql-sys.tar.gz \
&& cd /mysql-sys/*/ \
&& ./generate_sql_file.sh -v 56 -m \
# Fix mysql-sys for MariaDB according to the https://www.fromdual.com/mysql-sys-schema-in-mariadb-10-2 notes
# and https://mariadb.com/kb/en/library/system-variable-differences-between-mariadb-100-and-mysql-56/ reference
&& sed -i -e "s/@@global.server_uuid/@@global.server_id/g" gen/*.sql \
&& sed -i -e "s/@@master_info_repository/NULL/g" gen/*.sql \
&& sed -i -e "s/@@relay_log_info_repository/NULL/g" gen/*.sql \
&& mv gen/*.sql /docker-entrypoint-initdb.d/ \
&& cd / \
&& rm -rf /mysql-sys \
&& apt-get purge -y --auto-remove $fetchDeps
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["mysqld"]

View File

@ -6,25 +6,25 @@ use yii\web\Application;
$time = microtime(true); $time = microtime(true);
require __DIR__ . '/../../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
defined('YII_DEBUG') || define('YII_DEBUG', in_array(getenv('YII_DEBUG'), ['true', '1'])); defined('YII_DEBUG') || define('YII_DEBUG', in_array(getenv('YII_DEBUG'), ['true', '1'], false));
defined('YII_ENV') || define('YII_ENV', getenv('YII_ENV')); defined('YII_ENV') || define('YII_ENV', getenv('YII_ENV'));
// Initialize an application aspect container // Initialize an application aspect container
AspectKernel::getInstance()->init([ AspectKernel::getInstance()->init([
'debug' => YII_DEBUG, 'debug' => YII_DEBUG,
'appDir' => __DIR__ . '/../../', 'appDir' => dirname(__DIR__),
'cacheDir' => __DIR__ . '/../runtime/aspect', 'cacheDir' => __DIR__ . '/runtime/aspect',
'excludePaths' => [ 'excludePaths' => [
__DIR__ . '/../runtime/aspect', __DIR__ . '/runtime/aspect',
__DIR__ . '/../../vendor', __DIR__ . '/../vendor',
], ],
]); ]);
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php'; require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../common/config/bootstrap.php'; require __DIR__ . '/../common/config/bootstrap.php';
require __DIR__ . '/../config/bootstrap.php'; require __DIR__ . '/config/bootstrap.php';
$config = ConfigLoader::load('api'); $config = ConfigLoader::load('api');

View File

@ -1,2 +0,0 @@
User-agent: *
Disallow:

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,5 +1,21 @@
version: '2' version: '3.4'
services: services:
web:
image: registry.ely.by/elyby/accounts-nginx:latest
build:
context: .
target: web
args:
build_env: dev
depends_on:
- app
env_file: .env
volumes:
- ./:/var/www/html
networks:
- default
- nginx-proxy
app: app:
image: registry.ely.by/elyby/accounts:dev image: registry.ely.by/elyby/accounts:dev
build: build:
@ -10,8 +26,12 @@ services:
- db - db
- redis - redis
volumes: volumes:
- ./:/var/www/html/ - ./:/var/www/html
env_file: .env env_file: .env
networks:
default:
aliases:
- php
worker: worker:
image: registry.ely.by/elyby/accounts:dev image: registry.ely.by/elyby/accounts:dev
@ -24,7 +44,7 @@ services:
- db - db
- redis - redis
volumes: volumes:
- ./:/var/www/html/ - ./:/var/www/html
env_file: .env env_file: .env
cron: cron:
@ -39,22 +59,15 @@ services:
- db - db
- redis - redis
volumes: volumes:
- ./:/var/www/html/ - ./:/var/www/html
env_file: .env env_file: .env
web:
image: registry.ely.by/elyby/accounts-nginx:latest
volumes_from:
- app
links:
- app:php
env_file: .env
networks:
- default
- nginx-proxy
db: db:
build: ./docker/mariadb build:
context: .
target: db
args:
build_env: dev
env_file: .env env_file: .env
volumes: volumes:
- ./data/mysql:/var/lib/mysql - ./data/mysql:/var/lib/mysql
@ -99,7 +112,11 @@ services:
# 3) Uncomment the next 2 services (testdb and testredis): # 3) Uncomment the next 2 services (testdb and testredis):
# #
# testdb: # testdb:
# build: ./docker/mariadb # build:
# context: .
# target: db
# args:
# build_env: dev
# volumes: # volumes:
# - ./data/mysql-test:/var/lib/mysql # - ./data/mysql-test:/var/lib/mysql
# environment: # environment:

View File

@ -1,5 +1,24 @@
version: '2' version: '3.4'
x-logging:
&default-logging
options:
max-size: 50m
services: services:
web:
image: registry.ely.by/elyby/accounts-nginx:latest
restart: always
depends_on:
- app
env_file: .env
volumes:
- ./frontend:/var/www/html/frontend
networks:
- default
- nginx-proxy
logging: *default-logging
app: app:
image: registry.ely.by/elyby/accounts:latest image: registry.ely.by/elyby/accounts:latest
restart: always restart: always
@ -7,6 +26,11 @@ services:
- db - db
- redis - redis
env_file: .env env_file: .env
networks:
default:
aliases:
- php
logging: *default-logging
worker: worker:
image: registry.ely.by/elyby/accounts:latest image: registry.ely.by/elyby/accounts:latest
@ -16,6 +40,7 @@ services:
- db - db
- redis - redis
env_file: .env env_file: .env
logging: *default-logging
cron: cron:
image: registry.ely.by/elyby/accounts:latest image: registry.ely.by/elyby/accounts:latest
@ -26,21 +51,10 @@ services:
- db - db
- redis - redis
env_file: .env env_file: .env
logging: *default-logging
web:
image: registry.ely.by/elyby/accounts-nginx:1.0.3
restart: always
volumes_from:
- app
links:
- app:php
env_file: .env
networks:
- default
- nginx-proxy
db: db:
build: ./docker/mariadb image: registry.ely.by/elyby/accounts-mariadb:latest
restart: always restart: always
env_file: .env env_file: .env
volumes: volumes:

View File

@ -1,6 +0,0 @@
FROM mariadb:10.2.11
COPY custom.cnf /etc/mysql/conf.d/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["mysqld"]

View File

@ -4,6 +4,7 @@ default-character-set = utf8mb4
[mysqld] [mysqld]
character-set-server = utf8mb4 character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci collation-server = utf8mb4_unicode_ci
performance_schema = ON
[client] [client]
default-character-set = utf8mb4 default-character-set = utf8mb4

View File

@ -0,0 +1,66 @@
server {
listen 80;
set $root_path '/var/www/html';
set $frontend_path '${root_path}/frontend';
root $root_path;
charset utf-8;
add_header X-Frame-Options "sameorigin" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
# You can uncomment the next lines to enable debug mode
# rewrite_log on;
# error_log /var/log/nginx/error.log debug;
set $request_url $request_uri;
set $host_with_uri '${host}${request_uri}';
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/auth') {
set $request_url '/api/authserver${request_uri}';
rewrite ^/auth /api/authserver$uri last;
}
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/session') {
set $request_url '/api/minecraft${request_uri}';
rewrite ^/session /api/minecraft$uri last;
}
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/api/(user|profiles)') {
set $request_url '/api/mojang${request_uri}';
rewrite ^/api/(user|profiles) /api/mojang$uri last;
}
location / {
root $frontend_path;
access_log off;
etag on;
expires $cache_duration;
try_files $uri /index.html =404;
}
location /api {
try_files $uri /api/index.php$is_args$args;
}
location /images/emails/assets {
alias '${root_path}/vendor/ely/email-renderer/dist/assets';
access_log off;
}
location ~* \.php$ {
fastcgi_pass php; # Use generated upstream. See generate-upstream.sh
fastcgi_index /index.php;
fastcgi_cache cache;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REQUEST_URI $request_url;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# Override HTTPS param to handle ssl from nginx-proxy container
fastcgi_param HTTPS $http_x_forwarded_ssl if_not_empty;
}
}

View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
generate-upstream
envsubst '$AUTHSERVER_HOST' < /etc/nginx/conf.d/account.ely.by.conf.template > /etc/nginx/conf.d/default.conf
exec "$@"

View File

@ -0,0 +1,15 @@
#!/usr/bin/env sh
result="upstream php {"
for x in $(echo $PHP_SERVERS | tr "," "\n"); do
parts=$(echo $x | tr "x" "\n")
host=$(echo $parts | awk '{print $1}')
weight=$(echo $parts | awk '{print $2}')
result="$result\n server $host weight=${weight:-1};"
done
result="$result\n}"
echo -e $result > /etc/nginx/conf.d/upstream.conf

33
docker/nginx/nginx.conf Normal file
View File

@ -0,0 +1,33 @@
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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 10;
fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=cache:128m inactive=600m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
map $uri $cache_duration {
"~*^.+\.(jpe?g|gif|png|svg|js|json|css|zip|rar|eot|ttf|woff|woff2|ico|xml)$" "max";
default "off";
}
include /etc/nginx/conf.d/*.conf;
}

View File

@ -18,8 +18,8 @@ fi
cd /var/www/html cd /var/www/html
# Create all necessary folders # Create all necessary folders
mkdir -p api/runtime api/web/assets console/runtime mkdir -p api/runtime console/runtime
chown -R www-data:www-data api/runtime api/web/assets console/runtime chown -R www-data:www-data api/runtime console/runtime
if [ "$YII_ENV" = "test" ] if [ "$YII_ENV" = "test" ]
then then

View File

@ -1,8 +1,3 @@
FROM phpmyadmin/phpmyadmin:4.7.9-1 FROM phpmyadmin/phpmyadmin:4.8.5
RUN printf "\n\nrequire('./config.local.php');\n" >> /www/config.inc.php COPY config.inc.php /usr/src/phpmyadmin/
COPY config.local.php /www/
ENTRYPOINT [ "/run.sh" ]
CMD ["phpmyadmin"]