Merge pull request #67 from turniere/ticket/TURNIERE-262

Improve Docker Images (Ticket/turniere 262)
This commit is contained in:
Daniel Schädler 2019-10-09 13:34:28 +02:00 committed by GitHub
commit 46c903e109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 17 deletions

View File

@ -1,7 +1,7 @@
.git
test/**
spec/**
tmp/**
coverage/**
docker/**
.gitignore
.hound.yml
.rspec

View File

@ -1,26 +1,34 @@
stages:
- test
- build
- test
- deploy
rails spec:
stage: test
image: ruby:2.6.2
script:
- bundle install --path /cache
- bundle exec rails db:migrate RAILS_ENV=test
- bundle exec rails spec
kaniko:
kaniko:
tags:
- docker
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:latest --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/production/Dockerfile --destination $CI_REGISTRY_IMAGE/production --destination $CI_REGISTRY_IMAGE/production/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/production/branches:$CI_COMMIT_REF_SLUG
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/development/Dockerfile --destination $CI_REGISTRY_IMAGE/development --destination $CI_REGISTRY_IMAGE/development/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/development/branches:$CI_COMMIT_REF_SLUG
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/test/Dockerfile --destination $CI_REGISTRY_IMAGE/test --destination $CI_REGISTRY_IMAGE/test/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/test/branches:$CI_COMMIT_REF_SLUG
rails spec:
tags:
- docker
stage: test
image: $CI_REGISTRY_IMAGE/test/commits:$CI_COMMIT_SHA
script:
- cd /app
- bundle exec rails db:migrate
- bundle exec rails spec
kubernetes:
tags:
- turniere-deploy
stage: deploy
only:
- master

View File

@ -20,6 +20,28 @@ Development (without mail confirmation and separate database):
$ RAILS_ENV=development rails server
```
## Docker
[Registry](https://gitlab.com/turniere/turniere-backend/container_registry)
You can find all our Dockerfiles in the docker directory.
They depend on each other in the following order: `production``development``test`
This means, to build the `development` image, you have to build the `production` image first and tag it with the corresponding tag that is mentioned in the `FROM` line in the `development` Dockerfile.
To build all images do:
```bash
cd turniere-backend
docker build -t registry.gitlab.com/turniere/turniere-backend/production -f docker/production/Dockerfile .
docker build -t registry.gitlab.com/turniere/turniere-backend/development -f docker/development/Dockerfile .
docker build -t registry.gitlab.com/turniere/turniere-backend/test -f docker/test/Dockerfile .
```
This is done to leave test and development dependencies out of the production container.
Also we have a dedicated test container which runs the tests reproducible when you start it, but can also run the normal rails server to somewhat debug problems occuring in the test suite if needed.
While developing, if you want to use the development docker container, it should™ be sufficient to mount the root of this repository into the /app folder within the docker container to avoid building it over and over again.
**Only rebuilding the `development` container is not sufficient, as the `development` Dockerfile does not have a `COPY` Statement**
## Generate diagrams
```bash
$ rails diagram:all_with_engines

View File

@ -0,0 +1,6 @@
# Build production container locally first with following tag
FROM registry.gitlab.com/turniere/turniere-backend/production
WORKDIR /app
RUN bundle install --deployment --with development
ENV RAILS_ENV development
CMD bundle exec rails db:migrate && bundle exec rails s -p 3000 -b 0.0.0.0

View File

@ -1,8 +1,7 @@
FROM ruby:2.6.2-alpine
RUN apk add build-base tzdata sqlite sqlite-dev postgresql-dev && gem install tzinfo-data
RUN apk add --no-cache build-base tzdata sqlite sqlite-dev postgresql-dev && gem install tzinfo-data
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY Gemfile* /app/
RUN bundle install --deployment --without development test
COPY . /app
ENV RAILS_ENV production

6
docker/test/Dockerfile Normal file
View File

@ -0,0 +1,6 @@
# Build development container locally first with following tag
FROM registry.gitlab.com/turniere/turniere-backend/development
WORKDIR /app
RUN bundle install --deployment --with test
ENV RAILS_ENV test
CMD bundle exec rails db:migrate && bundle exec rails spec

View File

@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: backend-container
image: registry.gitlab.com/turniere/turniere-backend:<%= current_sha %>
image: registry.gitlab.com/turniere/turniere-backend/production/commits:<%= current_sha %>
ports:
- containerPort: 3000
env: