-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
159 lines (130 loc) · 4.77 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
###############################
# Stage wkhtmltopdf
FROM madnight/docker-alpine-wkhtmltopdf as wkhtmltopdf
######################
# Stage: ruby
FROM ruby:2.5.1-alpine3.7 as ruby
LABEL description="Base ruby image used by other stages"
######################
# Stage: bundler
FROM ruby as bundler
LABEL description="Install and cache gems for all environments"
WORKDIR /home/app
# Copy the Gemfile and Gemfile.lock
COPY Gemfile* /home/app/
# Install build deps as virtual dependencies.
#
# - build-base -- used to install gcc, make, etc.
# - libxml2-dev -- used to install nokogiri native extension
# - libxslt-dev -- used to install nokogiri native extension
RUN apk add --update --no-cache --virtual .build-deps \
build-base \
libxml2-dev \
libxslt-dev \
&& bundle config build.nokogiri --use-system-libraries \
&& bundle install --frozen --deployment --jobs 4 --retry 3 \
# Remove unneeded files (*/.git, *.o, *.c) but keeps cached gems for later stages
&& find vendor/bundle/ -name ".git" -exec rm -rv {} + \
&& find vendor/bundle/ -name "*.c" -delete \
&& find vendor/bundle/ -name "*.o" -delete \
&& rm -rf vendor/bundle/ruby/*/cache \
# Remove unneeded build deps
&& apk del .build-deps
###############################
# Stage runner
FROM ruby as runner
LABEL description="Builds an image ready to be run"
# Install runtime deps and create non-root user.
#
# If you need to install specific libraries for test environment
# please use a virtual pkg holder you can easily remove on the
# `release` stage.
#
# For example:
# RUN apk add --update --no-cache --virtual .test-deps \
# somelib-only-used-for-test
# Then in the `release` image:
# RUN apk del .test-deps
#
# - glib -- runtime deps for wkhtmltopdf
# - libcrypto1.0 -- runtime deps for wkhtmltopdf
# - libgcc -- runtime deps for wkhtmltopdf
# - libintl -- runtime deps for wkhtmltopdf
# - libssl1.0 -- runtime deps for wkhtmltopdf
# - libstdc++ -- runtime deps for wkhtmltopdf
# - libx11 -- runtime deps for wkhtmltopdf
# - libxext -- runtime deps for wkhtmltopdf
# - libxml2 -- used to run nokogiri
# - libxrender -- runtime deps for wkhtmltopdf
# - libxslt -- used to run nokogiri
# - nodejs -- used to compile assets
# - ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family -- runtime deps for wkhtmltopdf
# - tzdata -- used to install TZinfo data
RUN apk add --update --no-cache \
glib \
libcrypto1.0 \
libgcc \
libintl \
libssl1.0 \
libstdc++ \
libx11 \
libxext \
libxml2 \
libxrender \
libxslt \
nodejs \
ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family \
tzdata \
&& addgroup -g 1000 -S app \
&& adduser -u 1000 -S app -G app
USER app
WORKDIR /home/app
# Copy wkhtmltopdf bin from wkhtmltopdf stage
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /usr/bin/
# Copy bundle config from bundler stage
COPY --chown=app:app --from=bundler /usr/local/bundle/config /usr/local/bundle/config
# Copy bundled gems from bundler stage
COPY --chown=app:app --from=bundler /home/app/vendor /home/app/vendor
# Copy source files according to .dockerignore policy
# Make sure your .dockerignore file is properly configure to ensure proper layer caching
COPY --chown=app:app . /home/app
ENV PORT 3000
# Expose web server port
EXPOSE 3000
ENTRYPOINT ["bundle", "exec"]
CMD ["puma", "-C", "config/puma.rb"]
###############################
# Stage compiler
FROM runner as compiler
LABEL description="Builds a compiler image used to compile assets"
# Copy cached compiled assets to avoid re-compiling them
# We use latest compiler image to get already compiled assets
# and save lots of time on assets compilation for this new image
COPY --chown=app:app --from=my-app:compiler /home/app/public /home/app/public
COPY --chown=app:app --from=my-app:compiler /home/app/tmp /home/app/tmp
# Env variables required to run rake assets:precompile
ARG ASSET_HOST
ARG ENV=production
# Precompile assets and keep the cache for future releases
RUN ASSET_HOST=$ASSET_HOST \
RAILS_ENV=$ENV \
bundle exec rake assets:precompile
###############################
# Stage release
FROM runner as release
LABEL description="Builds a release image removing unneeded files and dependencies"
# Removes development and test gems by re-running the bundle
# install command using cached gems and simply removing unneeded
# gems using the clean option.
RUN bundle install --local --clean --without development test \
# Remove unneeded cached gems
&& find vendor/bundle/ -name "*.gem" -delete \
# Remove unneeded files and folders
&& rm -rf spec tmp/cache node_modules app/assets vendor/assets lib/assets
# Copy compiled assets
COPY --chown=app:app --from=compiler /home/app/public /home/app/public
ARG ASSET_HOST
ARG ENV=production
# Set App env variables
ENV ASSET_HOST $ASSET_HOST
ENV RAILS_ENV $ENV