Skip to content

Commit

Permalink
feat: rewrite Dockerfile with multi-stage builds
Browse files Browse the repository at this point in the history
  • Loading branch information
yksflip committed Aug 23, 2023
1 parent e4bb40a commit 1044c07
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 66 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.

4 changes: 2 additions & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
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

0 comments on commit 1044c07

Please sign in to comment.