Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multistage Dockerfile #1021

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Expand Up @@ -41,3 +41,7 @@ Capfile
config/deploy
config/deploy.rb
Gemfile.capistrano*

Dockerfile
Dockerfile-dev
docker-compose.yml
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ public/system
public/uploads
storage
vendor/bundle
swagger

# no configuration
config/*.yml
Expand Down
93 changes: 58 additions & 35 deletions Dockerfile
@@ -1,52 +1,75 @@
FROM ruby:2.7

RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \
supercronicBin=/usr/local/bin/supercronic && \
supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \
curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \
echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \
chmod +x "$supercronicBin"
FROM ruby:2.7 as base

ENV PORT=3000 \
SMTP_SERVER_PORT=2525 \
RAILS_ENV=production \
RAILS_LOG_TO_STDOUT=true \
RAILS_SERVE_STATIC_FILES=true

WORKDIR /usr/src/app

COPY . ./

# install dependencies and generate crontab
RUN buildDeps='libmagic-dev' && \
RUN buildDeps='libmagic-dev nodejs chromium' && \
export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install --no-install-recommends -y $buildDeps && \
echo 'gem: --no-document' >> ~/.gemrc && \
gem install bundler && \
bundle config build.nokogiri "--use-system-libraries" && \
bundle install --deployment --without development test -j 4 && \
apt-get purge -y --auto-remove $buildDeps && \
rm -Rf /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \
\
bundle exec whenever >crontab
bundle config build.nokogiri "--use-system-libraries"

# compile assets with temporary mysql server
RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \
export SECRET_KEY_BASE=thisisnotimportantnow && \
export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -y mariadb-server nodejs && \
/etc/init.d/mariadb start && \
mariadb -e "CREATE DATABASE temp" && \
cp config/app_config.yml.SAMPLE config/app_config.yml && \

COPY bin bin
COPY plugins plugins
COPY Gemfile Gemfile.lock docker-entrypoint.sh ./

# Development
FROM base as development
ENV RAILS_ENV=development \
CHROMIUM_FLAGS=--no-sandbox

RUN bundle install

COPY . ./

# generate api spec file using nulldb adapter
RUN cp config/database.yml.NULLDB_SAMPLE config/database.yml && \
RAILS_ENV=test DB_ADAPTER=nulldb bundle exec rake rswag:specs:swaggerize && \
rm config/database.yml

ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["./proc-start", "web"]


# Production

FROM base as production

ENV RAILS_ENV=production

RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \
supercronicBin=/usr/local/bin/supercronic && \
supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \
curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \
echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \
chmod +x "$supercronicBin"

RUN bundle config set deployment 'true' && \
bundle install --without development test

COPY . ./
COPY --from=development /usr/src/app/swagger/v1/swagger.yaml /usr/src/app/swagger/v1/swagger.yaml

# copy sample configs
RUN cp config/app_config.yml.SAMPLE config/app_config.yml && \
cp config/database.yml.MySQL_SAMPLE config/database.yml && \
cp config/storage.yml.SAMPLE config/storage.yml && \
bundle exec rake db:setup assets:precompile && \
rm -Rf tmp/* && \
/etc/init.d/mariadb stop && \
rm -Rf /run/mysqld /tmp/* /var/tmp/* /var/lib/mysql /var/log/mysql* && \
apt-get purge -y --auto-remove mariadb-server && \
rm -Rf /var/lib/apt/lists/* /var/cache/apt/*
cp config/storage.yml.SAMPLE config/storage.yml

# precompile assets
RUN SECRET_KEY_BASE=42 bundle exec rake assets:precompile

# Cleanup
RUN apt-get purge -y --auto-remove $buildDeps && \
rm -Rf tmp/* /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \
bundle exec whenever >crontab

# Make relevant dirs and files writable for app user
RUN mkdir -p tmp storage && \
Expand All @@ -61,6 +84,6 @@ EXPOSE 3000

VOLUME /usr/src/app/storage

# cleanup, and by default start web process from Procfile
# by default start web process from Procfile
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["./proc-start", "web"]
29 changes: 0 additions & 29 deletions Dockerfile-dev

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -125,3 +125,5 @@ group :test do
# api
gem 'rswag-specs'
end

gem "activerecord-nulldb-adapter", "~> 0.9.0"
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -128,6 +128,8 @@ GEM
activerecord (7.0.4)
activemodel (= 7.0.4)
activesupport (= 7.0.4)
activerecord-nulldb-adapter (0.9.0)
activerecord (>= 5.2.0, < 7.1)
activestorage (7.0.4)
actionpack (= 7.0.4)
activejob (= 7.0.4)
Expand Down Expand Up @@ -605,6 +607,7 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.10.0)
activerecord-nulldb-adapter (~> 0.9.0)
acts_as_tree
acts_as_versioned!
apparition
Expand Down
2 changes: 1 addition & 1 deletion app/lib/foodsoft_config.rb
Expand Up @@ -116,7 +116,7 @@ def select_multifoodcoop(foodcoop)
# @param key [String, Symbol]
# @return [Object] Value of the key.
def [](key)
if RailsSettings::CachedSettings.table_exists? && allowed_key?(key)
if ActiveRecord::Base.connected? && RailsSettings::CachedSettings.table_exists? && allowed_key?(key)
value = RailsSettings::CachedSettings["foodcoop.#{scope}.#{key}"]
value = config[key] if value.nil?
value
Expand Down
4 changes: 0 additions & 4 deletions config/application.rb
Expand Up @@ -58,10 +58,6 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'

# It would be nice not to enable database connection when precompiling assets,
# but i18n-js requires initialization, that's why it's on.
config.assets.initialize_on_precompile = true

# Load legacy scripts from vendor
config.assets.precompile += ['vendor/assets/javascripts/*.js']

Expand Down
2 changes: 2 additions & 0 deletions config/database.yml.NULLDB_SAMPLE
@@ -0,0 +1,2 @@
test:
adapter: nulldb
4 changes: 2 additions & 2 deletions docker-compose-dev.yml
Expand Up @@ -10,12 +10,12 @@ services:
foodsoft_worker:
build:
context: .
dockerfile: Dockerfile-dev
target: development
platform: linux/x86_64
command: ./proc-start worker
volumes:
- bundle:/usr/local/bundle
- .:/app
- .:/usr/src/app
environment:
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4
- REDIS_URL=redis://redis:6379
Expand Down