Skip to content

Commit 22ba0a3

Browse files
committed
feat: rewrite Dockerfile with multi-stage builds
1 parent 10b67de commit 22ba0a3

5 files changed

+65
-66
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ Capfile
4141
config/deploy
4242
config/deploy.rb
4343
Gemfile.capistrano*
44+
45+
Dockerfile
46+
Dockerfile-dev
47+
docker-compose.yml

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public/system
99
public/uploads
1010
storage
1111
vendor/bundle
12+
swagger
1213

1314
# no configuration
1415
config/*.yml

Dockerfile

+58-35
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,75 @@
1-
FROM ruby:2.7
2-
3-
RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \
4-
supercronicBin=/usr/local/bin/supercronic && \
5-
supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \
6-
curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \
7-
echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \
8-
chmod +x "$supercronicBin"
1+
FROM ruby:2.7 as base
92

103
ENV PORT=3000 \
114
SMTP_SERVER_PORT=2525 \
12-
RAILS_ENV=production \
135
RAILS_LOG_TO_STDOUT=true \
146
RAILS_SERVE_STATIC_FILES=true
157

168
WORKDIR /usr/src/app
179

18-
COPY . ./
19-
2010
# install dependencies and generate crontab
21-
RUN buildDeps='libmagic-dev' && \
11+
RUN buildDeps='libmagic-dev nodejs chromium' && \
12+
export DEBIAN_FRONTEND=noninteractive && \
2213
apt-get update && \
2314
apt-get install --no-install-recommends -y $buildDeps && \
2415
echo 'gem: --no-document' >> ~/.gemrc && \
2516
gem install bundler && \
26-
bundle config build.nokogiri "--use-system-libraries" && \
27-
bundle install --deployment --without development test -j 4 && \
28-
apt-get purge -y --auto-remove $buildDeps && \
29-
rm -Rf /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \
30-
\
31-
bundle exec whenever >crontab
17+
bundle config build.nokogiri "--use-system-libraries"
3218

33-
# compile assets with temporary mysql server
34-
RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \
35-
export SECRET_KEY_BASE=thisisnotimportantnow && \
36-
export DEBIAN_FRONTEND=noninteractive && \
37-
apt-get update && \
38-
apt-get install -y mariadb-server nodejs && \
39-
/etc/init.d/mariadb start && \
40-
mariadb -e "CREATE DATABASE temp" && \
41-
cp config/app_config.yml.SAMPLE config/app_config.yml && \
19+
20+
COPY bin bin
21+
COPY plugins plugins
22+
COPY Gemfile Gemfile.lock docker-entrypoint.sh ./
23+
24+
# Development
25+
FROM base as development
26+
ENV RAILS_ENV=development \
27+
CHROMIUM_FLAGS=--no-sandbox
28+
29+
RUN bundle install
30+
31+
COPY . ./
32+
33+
# generate api spec file using nulldb adapter
34+
RUN cp config/database.yml.NULLDB_SAMPLE config/database.yml && \
35+
RAILS_ENV=test DB_ADAPTER=nulldb bundle exec rake rswag:specs:swaggerize && \
36+
rm config/database.yml
37+
38+
ENTRYPOINT ["./docker-entrypoint.sh"]
39+
CMD ["./proc-start", "web"]
40+
41+
42+
# Production
43+
44+
FROM base as production
45+
46+
ENV RAILS_ENV=production
47+
48+
RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \
49+
supercronicBin=/usr/local/bin/supercronic && \
50+
supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \
51+
curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \
52+
echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \
53+
chmod +x "$supercronicBin"
54+
55+
RUN bundle config set deployment 'true' && \
56+
bundle install --without development test
57+
58+
COPY . ./
59+
COPY --from=development /usr/src/app/swagger/v1/swagger.yaml /usr/src/app/swagger/v1/swagger.yaml
60+
61+
# copy sample configs
62+
RUN cp config/app_config.yml.SAMPLE config/app_config.yml && \
4263
cp config/database.yml.MySQL_SAMPLE config/database.yml && \
43-
cp config/storage.yml.SAMPLE config/storage.yml && \
44-
bundle exec rake db:setup assets:precompile && \
45-
rm -Rf tmp/* && \
46-
/etc/init.d/mariadb stop && \
47-
rm -Rf /run/mysqld /tmp/* /var/tmp/* /var/lib/mysql /var/log/mysql* && \
48-
apt-get purge -y --auto-remove mariadb-server && \
49-
rm -Rf /var/lib/apt/lists/* /var/cache/apt/*
64+
cp config/storage.yml.SAMPLE config/storage.yml
65+
66+
# precompile assets
67+
RUN SECRET_KEY_BASE=42 bundle exec rake assets:precompile
68+
69+
# Cleanup
70+
RUN apt-get purge -y --auto-remove $buildDeps && \
71+
rm -Rf tmp/* /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \
72+
bundle exec whenever >crontab
5073

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

6285
VOLUME /usr/src/app/storage
6386

64-
# cleanup, and by default start web process from Procfile
87+
# by default start web process from Procfile
6588
ENTRYPOINT ["./docker-entrypoint.sh"]
6689
CMD ["./proc-start", "web"]

Dockerfile-dev

-29
This file was deleted.

docker-compose-dev.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ services:
1010
foodsoft_worker:
1111
build:
1212
context: .
13-
dockerfile: Dockerfile-dev
13+
target: development
1414
platform: linux/x86_64
1515
command: ./proc-start worker
1616
volumes:
1717
- bundle:/usr/local/bundle
18-
- .:/app
18+
- .:/usr/src/app
1919
environment:
2020
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4
2121
- REDIS_URL=redis://redis:6379

0 commit comments

Comments
 (0)