-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
298 lines (247 loc) · 9.59 KB
/
Copy pathDockerfile
File metadata and controls
298 lines (247 loc) · 9.59 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
ARG RUBY_VERSION=3.4.9
FROM ruby:$RUBY_VERSION-slim AS base
ARG NODE_VERSION=18.20.8
ARG POSTGRES_CLIENT_MAJOR=17
ARG DEBIAN_FRONTEND=noninteractive
WORKDIR /rails
##
# Rails and SAPI has some additional dependencies, e.g. rake requires a JS
# runtime, so attempt to get these from apt, where possible
RUN apt-get update -qq \
&& apt-get install --no-install-recommends -y \
# Needed to install Node.js from official distribution archives.
curl ca-certificates xz-utils gnupg \
\
# Needed by psych native extension (`yaml.h`) when bundling on slim images.
libyaml-dev \
\
# Use jemalloc for long-running Rails/Sidekiq processes to reduce allocator fragmentation and lower RSS during
# memory-heavy jobs such as questionnaire publish loop expansion.
libjemalloc2 \
\
# Needed for various library building activities
build-essential pkg-config \
\
# Do not install Postgres libraries from Debian; needs a specific version:
# postgresql-client libpq
# Do not install Node.js from Debian; needs a specific version:
# nodejs
\
##
# Keep PostgreSQL client major version aligned with DB server major version
# to avoid the generation of SQL which is inconsistent with the DB server, e.g.
# unpinned `postgresql-client` can upgrade to 17 and generate `structure.sql`
# that includes `transaction_timeout`, which fails to load on PostgreSQL 15.
# pg_dump requires that the client library >= the server (major) version
# `postgresql-client-${POSTGRES_CLIENT_MAJOR}` requires PostgreSQL `apt`
# repository.
&& install -d /usr/share/postgresql-common/pgdg \
&& curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
| gpg --dearmor -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.gpg \
&& . /etc/os-release \
&& echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.gpg] http://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" \
> /etc/apt/sources.list.d/pgdg.list \
\
&& apt-get install --no-install-recommends -y --force-yes \
# Install the correct version of the postgres client
"postgresql-client-${POSTGRES_CLIENT_MAJOR}" libpq-dev \
\
# Install libvips for Active Storage preview support
libvips \
\
# Zip for exports
zip \
#
libsodium-dev libgmp3-dev libssl-dev \
\
# socat is just for binding ports within docker, not needed for the application
socat \
\
# TeX is used for the generation of CITES Checklist PDFs.
texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives \
;
# Detect the runtime architecture from the image itself instead of trusting a
# caller-provided build arg. Local Docker Compose builds do not reliably set
# TARGETARCH, and falling back to amd64 installs an x64 Node binary into an
# arm64 image. ExecJS then tries to launch that binary during template asset
# compilation and fails with the Rosetta loader error seen in development.
RUN image_arch="$(dpkg --print-architecture)" \
&& case "$image_arch" \
in \
amd64) NODE_ARCH=x64 ;; \
arm64) NODE_ARCH=arm64 ;; \
*) echo "Unsupported architecture: '$image_arch'"; exit 1 ;; \
esac \
&& echo https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$NODE_ARCH.tar.xz \
&& curl -fsSL https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$NODE_ARCH.tar.xz \
| tar -xJ -C /usr/local --strip-components=1 \
;
# Debian installs jemalloc in an architecture-specific library directory. Resolve the actual path at build time and
# expose one stable preload path so both the Rails web process and Sidekiq use the same allocator automatically.
RUN jemalloc_path="$(find /usr/lib -name 'libjemalloc.so.2' | head -n 1)" \
&& test -n "${jemalloc_path}" \
&& ln -sf "${jemalloc_path}" /usr/local/lib/libjemalloc.so.2 \
;
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2
FROM base AS build
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qq \
&& apt-get install --no-install-recommends -y \
build-essential git libyaml-dev pkg-config \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives \
;
##
# For local development, we run `bundler` during the entrypoint
FROM build AS build-develop
##
# BUNDLE_DEPLOYMENT prevents changes to Gemfile.lock
ENV RAILS_ENV="development" \
NODE_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle"
##
# Install the same version of bundler as the one used to create the lockfile
RUN --mount=type=bind,source=Gemfile.lock,target=Gemfile.lock \
grep -A1 '^BUNDLED WITH$' Gemfile.lock | tail -n1 | tr -d ' ' \
| xargs -I _BUNDLER_VERSION_ gem install bundler -v _BUNDLER_VERSION_ \
;
FROM build-develop AS built-develop
##
# You may wish to do the following:
#
# export LOCAL_UID=$(id -u)
# export LOCAL_GID=$(id -g)
ARG LOCAL_UID=1000
ARG LOCAL_GID=1000
ARG DEFAULT_GROUPNAME=railsgroup
ARG DEFAULT_USERNAME=railsuser
##
# Docker UID/GID Mapping to Host: reuse or create group, then reuse or create
# user, and give them passwordless sudo.
RUN if getent group $LOCAL_GID > /dev/null; then \
grp=$(getent group $LOCAL_GID | cut -d: -f1); \
else \
groupadd -g $LOCAL_GID $DEFAULT_GROUPNAME; \
grp=$DEFAULT_GROUPNAME; \
fi \
&& if getent passwd $LOCAL_UID > /dev/null; then \
user=$(getent passwd $LOCAL_UID | cut -d: -f1); \
else \
useradd -m -u $LOCAL_UID -g "$grp" $DEFAULT_USERNAME; \
user=$DEFAULT_USERNAME; \
fi \
&& mkdir -p /etc/sudoers.d/ \
&& echo "${user} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/${user}"\
&& chmod 0440 "/etc/sudoers.d/${user}" \
;
##
# Ensure writable dirs for the non-root user
RUN mkdir -p /usr/local/bundle \
./ ./db ./log ./storage ./tmp \
&& chown -R $LOCAL_UID:$LOCAL_GID \
/usr/local/bundle \
./ ./db ./log ./storage ./tmp \
;
##
# Run as the numeric UID:GID so it works regardless of whether we created
# a user
USER ${LOCAL_UID}:${LOCAL_GID}
##
# The build step for staging
FROM build AS build-staging
ENV NODE_ENV="production" \
RAILS_ENV="staging" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
COPY Gemfile Gemfile.lock ./
RUN grep -A1 '^BUNDLED WITH$' Gemfile.lock | tail -n1 | tr -d ' ' \
| xargs -I _BUNDLER_VERSION_ \
gem install bundler -v _BUNDLER_VERSION_ \
;
RUN bundle install \
&& rm -rf \
~/.bundle/ \
"${BUNDLE_PATH}"/ruby/*/cache \
"${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git \
&& bundle exec bootsnap precompile --gemfile \
;
COPY . .
RUN bundle exec bootsnap precompile app/ lib/
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
FROM base AS built-staging
ARG DEFAULT_GROUPNAME=railsgroup
ARG DEFAULT_USERNAME=railsuser
ENV BUNDLE_PATH="/usr/local/bundle"
# Run and own only the runtime files as a non-root user for security
RUN groupadd $DEFAULT_GROUPNAME \
--gid 1000 --system \
&& useradd $DEFAULT_USERNAME \
--uid 1000 --gid 1000 --create-home --shell /bin/bash \
;
USER 1000:1000
COPY --from="build-staging" --chown=1000:1000 $BUNDLE_PATH $BUNDLE_PATH
COPY --from="build-staging" --chown=1000:1000 /rails/ /rails/
FROM built-staging AS deploy-staging
ENV RAILS_ENV="staging" \
BUNDLE_WITHOUT="development"
CMD ["tail", "-f", "/dev/null"]
FROM built-staging AS exec-staging
ENV RAILS_ENV="staging" \
BUNDLE_WITHOUT="development"
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
EXPOSE 80
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
##
# The build step for production
FROM build AS build-production
ENV NODE_ENV="production" \
RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
COPY Gemfile Gemfile.lock ./
RUN grep -A1 '^BUNDLED WITH$' Gemfile.lock | tail -n1 | tr -d ' ' \
| xargs -I _BUNDLER_VERSION_ \
gem install bundler -v _BUNDLER_VERSION_ \
;
RUN bundle install \
&& rm -rf \
~/.bundle/ \
"${BUNDLE_PATH}"/ruby/*/cache \
"${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git \
&& bundle exec bootsnap precompile --gemfile \
;
COPY . .
RUN bundle exec bootsnap precompile app/ lib/
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
FROM base AS built-production
ARG DEFAULT_GROUPNAME=railsgroup
ARG DEFAULT_USERNAME=railsuser
ENV BUNDLE_PATH="/usr/local/bundle"
# Run and own only the runtime files as a non-root user for security
RUN groupadd $DEFAULT_GROUPNAME \
--gid 1000 --system \
&& useradd $DEFAULT_USERNAME \
--uid 1000 --gid 1000 --create-home --shell /bin/bash \
;
USER 1000:1000
COPY --from="build-production" --chown=1000:1000 $BUNDLE_PATH $BUNDLE_PATH
COPY --from="build-production" --chown=1000:1000 /rails/ /rails/
FROM built-production AS deploy-production
ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development"
CMD ["tail", "-f", "/dev/null"]
FROM built-production AS exec-production
ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development"
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
EXPOSE 80
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
FROM built-develop AS deploy-develop
CMD ["tail", "-f", "/dev/null"]
FROM built-develop AS exec-develop
ENTRYPOINT ["/rails/bin/docker-entrypoint-develop"]
EXPOSE 80
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]