From 5c95a204d4ec3111b907d09643e7a24f445c5ae7 Mon Sep 17 00:00:00 2001 From: Nik Wolfgramm Date: Fri, 29 Mar 2019 16:33:30 +0100 Subject: [PATCH 1/2] Adding docker-compose and alpine based image which seperates the app from the database --- .dockerignore | 11 +++++++++++ Dockerfile.alpine | 27 +++++++++++++++++++++++++++ docker-compose.yml | 31 +++++++++++++++++++++++++++++++ docker-up.sh | 18 ++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile.alpine create mode 100644 docker-compose.yml create mode 100755 docker-up.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b781c6ab --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +log +tmp +.bundle +doc +.git +.gitignore +README.md +DOCKER_README.md +ROADMAP.md +CHANGELOG.md +.dockerignore diff --git a/Dockerfile.alpine b/Dockerfile.alpine new file mode 100644 index 00000000..6749b160 --- /dev/null +++ b/Dockerfile.alpine @@ -0,0 +1,27 @@ +FROM ruby:2.6.2-alpine + +RUN apk add --update --no-cache \ + build-base \ + mariadb-dev \ + tzdata \ + nodejs + +USER root + +WORKDIR /staytus +COPY Gemfile* ./ +RUN bundle install --binstubs --without development test +COPY . ./ + +ARG DB_HOST +ARG DB_USER +ARG DB_PASSWORD +ARG DB_DATABASE + +RUN bundle exec rake staytus:build + +EXPOSE 5000 + +ENV RAILS_LOG_TO_STDOUT=TRUE + +ENTRYPOINT ["./docker-up.sh"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..db699ad8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: '2' + +services: + db: + image: mysql:5.7 + restart: always + environment: + MYSQL_ROOT_PASSWORD: admin + MYSQL_DATABASE: staytus + MYSQL_USER: staytus + MYSQL_PASSWORD: staytus + volumes: + - db_data:/var/lib/mysql + + app: + build: + context: . + dockerfile: Dockerfile.alpine + depends_on: + - db + ports: + - 5000:5000 + volumes: + - .:/staytus + environment: + DB_HOST: db + RAILS_ENV: production + LOG_LEVEL: DEBUG + +volumes: + db_data: {} diff --git a/docker-up.sh b/docker-up.sh new file mode 100755 index 00000000..69235425 --- /dev/null +++ b/docker-up.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# copy and update database.yml +cp config/database.example.yml config/database.yml + +# set defaults for ENV vars +export DB_HOST="${DB_HOST:-127.0.0.1}" +export DB_DATABASE="${DB_DATABASE:-staytus}" +export DB_USER="${DB_USER:-staytus}" +export DB_PASSWORD="${DB_PASSWORD:-staytus}" + +sed -i "s|host:.*|host: <%= ENV['DB_HOST'] %>|" config/database.yml +sed -i "s|username:.*|username: <%= ENV['DB_USER'] %>|" config/database.yml +sed -i "s|password:.*|password: <%= ENV['DB_PASSWORD'] %>|" config/database.yml +sed -i "s|database:.*|database: <%= ENV['DB_DATABASE'] %>|" config/database.yml + +bundle exec rake staytus:install staytus:upgrade +bundle exec foreman start From 3a2ce0456c188a747f76a141de8018c27b4a23b1 Mon Sep 17 00:00:00 2001 From: Nik Wolfgramm Date: Fri, 23 Aug 2019 12:49:20 +0200 Subject: [PATCH 2/2] Optimized non root alpine setup --- Dockerfile.alpine | 22 ++++++++++------------ docker-compose.yml | 4 ++-- docker-up.sh | 14 -------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 6749b160..8dca3e02 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,27 +1,25 @@ -FROM ruby:2.6.2-alpine +FROM ruby:2.6.3-alpine RUN apk add --update --no-cache \ build-base \ mariadb-dev \ tzdata \ - nodejs - -USER root + nodejs \ + libc6-compat +RUN adduser -D staytus WORKDIR /staytus COPY Gemfile* ./ +COPY config/database.example.yml config/database.yml RUN bundle install --binstubs --without development test -COPY . ./ - -ARG DB_HOST -ARG DB_USER -ARG DB_PASSWORD -ARG DB_DATABASE +COPY --chown=staytus . . +ARG DATABASE_URL +ENV RAILS_LOG_TO_STDOUT=TRUE RUN bundle exec rake staytus:build +RUN chown -R staytus:staytus /staytus +USER staytus EXPOSE 5000 -ENV RAILS_LOG_TO_STDOUT=TRUE - ENTRYPOINT ["./docker-up.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index db699ad8..d9432814 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '2' services: db: - image: mysql:5.7 + image: mariadb:10.4 restart: always environment: MYSQL_ROOT_PASSWORD: admin @@ -23,7 +23,7 @@ services: volumes: - .:/staytus environment: - DB_HOST: db + DATABASE_URL: "mysql2://staytus:staytus@db/staytus" RAILS_ENV: production LOG_LEVEL: DEBUG diff --git a/docker-up.sh b/docker-up.sh index 69235425..34984ff5 100755 --- a/docker-up.sh +++ b/docker-up.sh @@ -1,18 +1,4 @@ #!/bin/sh -# copy and update database.yml -cp config/database.example.yml config/database.yml - -# set defaults for ENV vars -export DB_HOST="${DB_HOST:-127.0.0.1}" -export DB_DATABASE="${DB_DATABASE:-staytus}" -export DB_USER="${DB_USER:-staytus}" -export DB_PASSWORD="${DB_PASSWORD:-staytus}" - -sed -i "s|host:.*|host: <%= ENV['DB_HOST'] %>|" config/database.yml -sed -i "s|username:.*|username: <%= ENV['DB_USER'] %>|" config/database.yml -sed -i "s|password:.*|password: <%= ENV['DB_PASSWORD'] %>|" config/database.yml -sed -i "s|database:.*|database: <%= ENV['DB_DATABASE'] %>|" config/database.yml - bundle exec rake staytus:install staytus:upgrade bundle exec foreman start