-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.production
237 lines (181 loc) · 7.95 KB
/
Dockerfile.production
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
# syntax = docker/dockerfile:1
# docker build --build-arg RAILS_MASTER_KEY=$RAILS_MASTER_KEY -f Dockerfile.production "."
# docker build --build-arg TARGET_RAILS_ENV="production" --build-arg RAILS_MASTER_KEY="$RAILS_MASTER_KEY" -f Dockerfile.production .
# docker run -it -e RAILS_SERVE_STATIC_FILES=true -e RAILS_LOG_TO_STDOUT=true -p 3000:3000 <image from last step>
ARG RUBY_VERSION=3.2.2
ARG PG_MAJOR=14
ARG DISTRO_NAME=bullseye
ARG TARGET_RAILS_ENV=development
ARG RAILS_MASTER_KEY=DUMMY
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME as base
ARG DISTRO_NAME
ARG PG_MAJOR
ARG TARGET_RAILS_ENV
ARG RAILS_MASTER_KEY
# Common dependencies
# Using --mount to speed up build with caching, see https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#run---mount
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git
CMD ["/bin/bash"]
# Then, we define the "development" stage from the base one
FROM base AS development
ARG DISTRO_NAME
ARG PG_MAJOR
ARG TARGET_RAILS_ENV
ARG RAILS_MASTER_KEY
ENV RAILS_ENV=$TARGET_RAILS_ENV
# Application dependencies
# We use an external Aptfile for this, stay tuned
COPY .dockerdev/Aptfile /tmp/Aptfile
RUN DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/Aptfile | xargs)
# The env-builder image is responsible for installing dependencies and compiling assets
FROM base as common-builder
ARG DISTRO_NAME
ARG PG_MAJOR
ARG TARGET_RAILS_ENV
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
ENV RAILS_ENV=$TARGET_RAILS_ENV
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgres-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/postgres-archive-keyring.gpg] https://apt.postgresql.org/pub/repos/apt/" \
$DISTRO_NAME-pgdg main $PG_MAJOR | tee /etc/apt/sources.list.d/postgres.list > /dev/null
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR
# https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development
# Create and configure a dedicated user (use the same name as for the common-builder image)
RUN groupadd --gid 1005 relaunchcommunity \
&& useradd --uid 1005 --gid relaunchcommunity --shell /bin/bash --create-home relaunchcommunity
USER relaunchcommunity
RUN mkdir /home/relaunchcommunity/app
WORKDIR /home/relaunchcommunity/app
# Then, we re-configure Bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3 \
BUNDLE_APP_CONFIG=/home/relaunchcommunity/bundle \
BUNDLE_PATH=/home/relaunchcommunity/bundle \
GEM_HOME=/home/relaunchcommunity/bundle
# Install Ruby gems
COPY --chown=relaunchcommunity:relaunchcommunity Gemfile Gemfile.lock ./
RUN mkdir $BUNDLE_PATH \
&& bundle config --local deployment 'true' \
&& bundle config --local path "${BUNDLE_PATH}" \
&& bundle config --local without 'development test' \
&& bundle config --local clean 'true' \
&& bundle config --local no-cache 'true' \
&& bundle install --jobs=${BUNDLE_JOBS} \
&& rm -rf $BUNDLE_PATH/ruby/3.1.0/cache/* \
&& rm -rf /home/relaunchcommunity/.bundle/cache/*
# Finally, our pre-env image definition has steps to run prior to env-specific details
# NOTE: It's not extending the base image, it's a new one
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME AS pre-env
ARG DISTRO_NAME
ARG PG_MAJOR
ARG TARGET_RAILS_ENV
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
ENV RAILS_ENV=$TARGET_RAILS_ENV
# For dual stack networking, let's prefer ipv4
RUN echo "precedence ::ffff:0:0/96 100" >> /etc/gai.conf
# Production-only dependencies
RUN apt-get update -qq \
&& apt-get dist-upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
curl \
gnupg2 \
less \
tzdata \
time \
locales \
&& update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgres-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/postgres-archive-keyring.gpg] https://apt.postgresql.org/pub/repos/apt/" \
$DISTRO_NAME-pgdg main $PG_MAJOR | tee /etc/apt/sources.list.d/postgres.list > /dev/null
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR
# Upgrade RubyGems and install the latest Bundler version
RUN gem update --system && \
gem install bundler
# Create and configure a dedicated user (use the same name as for the common-builder image)
RUN groupadd --gid 1005 relaunchcommunity \
&& useradd --uid 1005 --gid relaunchcommunity --shell /bin/bash --create-home relaunchcommunity
RUN mkdir /home/relaunchcommunity/app
RUN --mount=type=secret,id=RAILS_MASTER_KEY_SECRET,required \
cat /run/secrets/RAILS_MASTER_KEY_SECRET > /home/relaunchcommunity/secret
WORKDIR /home/relaunchcommunity/app
USER relaunchcommunity
# Let rails know that we're building the image, so that
# particular initializers can skip loading if necessary!
#
# In particular, this is a lazy solution to Flipper's
# initialization on boot.
ENV APP_BUILD_ENVIRONMENT=1
# Ruby/Rails env configuration
ENV BUNDLE_APP_CONFIG=/home/relaunchcommunity/bundle \
BUNDLE_PATH=/home/relaunchcommunity/bundle \
GEM_HOME=/home/relaunchcommunity/bundle \
PATH="/home/relaunchcommunity/app/bin:${PATH}" \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8
COPY --chown=relaunchcommunity:relaunchcommunity . .
COPY --from=common-builder $BUNDLE_PATH $BUNDLE_PATH
# Precompile for all envs for
RUN RAILS_MASTER_KEY="$(cat /home/relaunchcommunity/secret)" \
./bin/rails assets:precompile
SHELL ["/bin/bash", "-c"]
FROM pre-env AS env-production-builder
ARG TARGET_RAILS_ENV
ARG RAILS_MASTER_KEY
ENV RAILS_ENV=$TARGET_RAILS_ENV
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
# Precompile assets
# NOTE: The command may require adding some environment variables (e.g., SECRET_KEY_BASE) if you're not using
# credentials.
# TODO: make use of SECRET_KEY_BASE_DUMMY=1
# Ref: https://rubyonrails.org/2022/12/23/this-week-in-rails-rails-on-docker-local-environment-inquirer-and-more-27aa9ed4
RUN if [[ -n "${RAILS_ENV}" ]] 2> /dev/null && [ "${RAILS_ENV}" == "production" ]; then \
echo "in production"; \
fi
FROM pre-env AS env-test-builder
ARG TARGET_RAILS_ENV
ENV RAILS_ENV=$TARGET_RAILS_ENV
USER root
RUN if [[ -n "${RAILS_ENV}" ]] 2> /dev/null && [ "${RAILS_ENV}" == "test" ]; then \
echo "in test" && \
# Install build-essential for dev/test gems if needed
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends build-essential firefox-esr xvfb && \
bundle config --delete without && \
bundle config set --local with 'test' && \
bundle config set --local without 'development' && \
bundle install; \
fi
FROM pre-env AS env-development-builder
ARG TARGET_RAILS_ENV
ENV RAILS_ENV=$TARGET_RAILS_ENV
USER root
RUN if [[ -n "${RAILS_ENV}" ]] 2> /dev/null && [ "${RAILS_ENV}" == "development" ]; then \
echo "in development" && \
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends build-essential && \
bundle config --delete without && \
bundle config set --local with 'development' && \
bundle config set --local without 'test' && \
bundle install; \
fi
FROM env-${TARGET_RAILS_ENV}-builder AS env-builder
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]