-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathDockerfile.erb
More file actions
331 lines (278 loc) · 11 KB
/
Dockerfile.erb
File metadata and controls
331 lines (278 loc) · 11 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# syntax=docker/dockerfile:1
# check=error=true
<% if using_kamal? -%>
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t demo .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name demo demo
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
<% end -%>
# Make sure RUBY_VERSION matches the Ruby version in <%= ruby_version_file || '.ruby-version' %>
ARG RUBY_VERSION=<%= RUBY_VERSION %>
<% if api_client_dir -%>
<%= render partial: 'node_client' %>
<% end -%>
<% if options.fullstaq -%>
FROM <%= platform %>quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-<%= options.jemalloc ? 'jemalloc-' : 'malloctrim-' %><%= variant %><% unless options.precompile == "defer" %> AS base<% end %>
<% elsif options.hardened -%>
FROM <%= platform %>dhi.io/ruby:$RUBY_VERSION-dev<% unless options.precompile == "defer" %> AS base<% end %>
<% else -%>
FROM <%= platform %><%= options.registry %>ruby:$RUBY_VERSION-<%= variant %><% unless options.precompile == "defer" %> AS base<% end %>
<% end -%>
<% unless options.label.empty? -%>
<% options.label.each do |key, value| -%>
LABEL <%= key =~ /^\w[.\w]*$/ ? key : key.inspect %>=<%= value.inspect %>
<% end -%>
<% end -%>
# Rails app lives here
WORKDIR /rails
<% unless base_args.empty? -%>
# Base build arguments
ARG <%= base_args.map {|key, value| "#{key}=#{value.inspect}"}.join(" \\\n ") %>
<% end -%>
# Update gems and bundler
RUN gem update --system <% if RUBY_VERSION.start_with? '2' %>3.4.22 <% end %>--no-document && \
gem install -N <%= base_gems.join(" ") %>
<% unless base_packages.empty? -%>
# Install base packages<% unless base_requirements.empty? -%> needed to install <%= base_requirements %><% end %>
<%= render partial: 'apt_install', locals: {packages: base_packages, clean: true, repos: base_repos} %>
<% end -%>
# Set production environment
ENV <%= base_env.join(" \\\n ") %>
<% if using_execjs? and node_version != 'lts' -%>
<%= render partial: 'install_node', locals: {yarn_version: nil} %>
<% end -%>
<% if base_instructions -%>
<%= base_instructions %>
<% end -%>
<% unless options.precompile == "defer" -%>
# Throw-away build stage<%= parallel? ? 's' : '' %> to reduce size of final image
FROM base AS <%= parallel? ? 'pre' : '' %>build
<% end -%>
# Install packages needed to build gems<%= using_node? ? " and node modules" : "" %>
<%= render partial: 'apt_install', locals: {packages: build_packages, clean: true, repos: ''} %>
<% if parallel? -%>
FROM prebuild AS <% if using_bun? %>bun<% else %>node<% end %>
<% end -%>
<% if using_bun? and (!using_execjs? || File.exist?('bun.lockb') || File.exist?('bun.lock')) -%>
<%= render partial: 'install_node', locals: {bun_version: using_execjs? ? nil : bun_version} %>
<% elsif using_node? and (!using_execjs? || File.exist?('yarn.lock')) -%>
<%= render partial: 'install_node', locals: {node_version: using_execjs? ? nil : node_version, yarn_version: File.exist?('yarn.lock') ? yarn_version : nil} %>
<% end -%>
<% if parallel? -%>
<%= render partial: 'npm_install', locals: {sources: Dir[*%w(package.json yarn.lock bun.lockb bun.lock)]} %>
FROM prebuild AS build
<% end -%>
<% unless build_args.empty? -%>
# Build arguments
ARG <%= build_args.map {|key, value| "#{key}=#{value.inspect}"}.join(" \\\n ") %>
<% end -%>
<% unless build_env.empty? -%>
# Build options
ENV <%= build_env.join(" \\\n ") %>
<% end -%>
# Install application gems
COPY<% if options.link? %> --link<% end %> Gemfile Gemfile.lock <% if references_ruby_version_file? %><%= ruby_version_file %> <% end %>./
<% if options.nproc > 1 -%>
ENV MAKE="make -j<%= options.nproc %>"
<% end -%>
<% if @netpopbug && Rails.env != "test" -%>
RUN sed -i "/net-pop (0.1.2)/a\ net-protocol" Gemfile.lock
<% end -%>
<% if options.cache? -%>
RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
<% if private_gemserver_env_variable_name -%>
--mount=type=secret,id=gemserver_credentials,target=/kaniko/gemserver_credentials \
<%= private_gemserver_env_variable_name %>="$(cat /kaniko/gemserver_credentials)" && \
export <%= private_gemserver_env_variable_name %> && \
<% end -%>
bundle config set app_config .bundle && \
bundle config set path /srv/vendor && \
bundle install && \
<% if depend_on_bootsnap? && options.precompile != "defer" -%>
bundle exec bootsnap precompile --gemfile && \
<% end -%>
bundle clean && \
mkdir -p vendor && \
bundle config set path vendor && \
cp -ar /srv/vendor .
<% else -%>
<% if private_gemserver_env_variable_name -%>
RUN --mount=type=secret,id=gemserver_credentials,target=/kaniko/gemserver_credentials \
<%= private_gemserver_env_variable_name %>="$(cat /kaniko/gemserver_credentials)" && \
export <%= private_gemserver_env_variable_name %> && \
bundle install && \
<% else -%>
RUN <% if options["precompiled-gems"] != true %>bundle config set force_ruby_platform true && \<%= "\n " %><% end %>bundle install && \
<% end -%>
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git<% if depend_on_bootsnap? && options.precompile != "defer" -%> && \
bundle exec bootsnap precompile --gemfile<% end %>
<% end -%>
<% if using_passenger? -%>
# Compile passenger native support
RUN passenger-config build-native-support
<% end -%>
<% if parallel? -%>
<% if using_bun? -%>
# Copy bun modules
COPY --from=bun /rails/node_modules /rails/node_modules
COPY --from=bun /usr/local/bun /usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
<% else -%>
# Copy node modules
COPY --from=node /rails/node_modules /rails/node_modules
COPY --from=node /usr/local/node /usr/local/node
ENV PATH=/usr/local/node/bin:$PATH
<% end -%>
<% elsif using_node? || using_bun? -%>
<%= render partial: 'npm_install', locals: {sources: Dir[*%w(.npmrc .yarnrc .yarnrc.yml package.json package-lock.json yarn.lock bun.lockb bun.lock)]} %>
<% end -%>
# Copy application code
COPY<% if options.link? %> --link<% end %> . .
<% if build_instructions -%>
<%= build_instructions %>
<% end -%>
<% if depend_on_bootsnap? -%>
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
<% end -%>
<% unless binfile_fixups.empty? -%>
<% if options['bin-cd'] and binfile_fixups.length == 1 -%>
# Adjust binfiles to set current working directory
<% else -%>
# Adjust binfiles to be executable on Linux<%= options['bin-cd'] ? ' and set current working directory' : '' %>
<% end -%>
<%= "RUN " + binfile_fixups.join(" && \\\n ") %>
<% end -%>
<% unless options.precompile == "defer" -%>
<% if Dir.exist?('app/assets') and !api_only? -%>
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE<%= Rails::VERSION::MAJOR<7 || Rails::VERSION::STRING.start_with?('7.0') ? '=DUMMY' : '_DUMMY=1' %> ./bin/rails assets:precompile
<% end -%>
# Final stage for app image
FROM base
<% end -%>
<% if using_litefs? -%>
# Install, configure litefs
COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs
COPY<% if options.link? %> --link<% end %> config/litefs.yml /etc/litefs.yml
<% end -%>
<% unless deploy_args.empty? -%>
# Deployment build arguments
ARG <%= deploy_args.map {|key, value| "#{key}=#{value.inspect}"}.join(" \\\n ") %>
<% end -%>
<% unless deploy_packages.empty? -%>
# Install packages needed for deployment
<%= render partial: 'apt_install', locals: {packages: deploy_packages, clean: true, repos: deploy_repos} %>
<% end -%>
<% if using_passenger? -%>
<%= render partial: 'passenger' %>
<% elsif options.nginx? -%>
<%= render partial: 'nginx' %>
<% elsif procfile.size > 1 -%>
RUN gem install foreman
<% end -%>
<% unless options.precompile == "defer" -%>
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
<% if using_passenger? -%>
# Copy passenger native support
COPY --from=build /root/.passenger/native_support /root/.passenger/native_support
<% end -%>
<% if api_client_dir -%>
# Copy built client
COPY --from=client /rails/<%= api_client_dir %>/build /rails/public
<% end -%>
<% end -%>
<% if run_as_root? -%>
<% if deploy_database == 'sqlite3' -%>
RUN mkdir /data
<% end -%>
<% else -%>
# Run and own only the runtime files as a non-root user for security
<% if options.compose? -%>
<% user = "rails:rails" -%>
ARG UID=1000 \
GID=1000
<% if options.alpine? -%>
RUN addgroup --system --gid $GID rails && \
adduser --system rails --uid $UID --ingroup rails --home /home/rails --shell /bin/sh rails && \
<% else -%>
RUN groupadd -f -g $GID rails && \
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
<% end -%>
<% else -%>
<% user = "1000:1000" -%>
<% if options.alpine? -%>
RUN addgroup --system --gid 1000 rails && \
adduser --system rails --uid 1000 --ingroup rails --home /home/rails --shell /bin/sh rails && \
<% else -%>
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
<% end -%>
<% end -%>
<% if options.nginx? -%>
chown <%= user %> /var/lib/nginx /var/log/nginx/* && \
<% end -%>
<% if deploy_packages.include?("sudo") && options.sudo? -%>
sed -i 's/env_reset/env_keep="*"/' /etc/sudoers && \
<% end -%>
<% if deploy_database == 'sqlite3' -%>
mkdir /data<% if using_litefs? %> /litefs<% end %> && \
chown -R <%= user %> <%= Dir[*%w(db log storage tmp)].join(" ") %> /data<% if using_litefs? %> /litefs<% end %>
<% else -%>
chown -R <%= user %> <%= Dir[*%w(db log storage tmp)].join(" ") %>
<% end -%>
<% unless options.swap? or using_passenger? or using_litefs? -%>
USER <%= user %>
<% end -%>
<% end -%>
<% if deploy_instructions -%>
<%= deploy_instructions.strip %>
<% end -%>
<% if using_litefs? and !run_as_root? -%>
# Authorize rails user to launch litefs
COPY <<-"EOF" /etc/sudoers.d/rails
rails ALL=(root) /usr/local/bin/litefs
EOF
<% end -%>
<% unless deploy_env.empty? -%>
# Deployment options
ENV <%= deploy_env.join(" \\\n ") %>
<% end -%>
<% if options.prepare -%>
# Entrypoint prepares the database.
<% else -%>
# Entrypoint sets up the container.
<% end -%>
<% if using_litefs? -%>
ENTRYPOINT ["litefs", "mount"]
<% else -%>
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
<% end -%>
<% if procfile.size > 1 -%>
# Build a Procfile for production use
COPY <<-"EOF" /rails/Procfile.prod
<% procfile.each do |name, command| -%>
<%= name %>: <%= command %>
<% end -%>
EOF
<% end -%>
<% if using_thruster? -%>
# Start server via Thruster by default, this can be overwritten at runtime
<% else -%>
# Start the server by default, this can be overwritten at runtime
<% end -%>
EXPOSE <%= using_thruster? ? '80' : '3000' %>
<% if deploy_database == 'sqlite3' -%>
VOLUME /data
<% end -%>
<% unless fly_processes && !using_thruster? -%>
<% if !options.procfile.blank? -%>
CMD ["foreman", "start", "--procfile=<%= options.procfile %>"]
<% elsif procfile.size > 1 -%>
CMD ["foreman", "start", "--procfile=Procfile.prod"]
<% elsif !using_litefs? -%>
CMD <%= procfile.values.first.split(" ").inspect %>
<% end -%>
<% end -%>