-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
247 lines (208 loc) · 9.5 KB
/
Copy pathDockerfile
File metadata and controls
247 lines (208 loc) · 9.5 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
# syntax=docker/dockerfile:1
ARG RUBY_VERSION=3.2.2
ARG DISTRO_NAME=bullseye
# Here we add the the name of the stage ("base")
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME AS base
ARG PG_MAJOR
ARG NODE_MAJOR
ARG YARN_VERSION
# Common dependencies
# ...
# The following lines are exactly the same as before
# ...
# ...
WORKDIR /app
EXPOSE 9292
CMD ["/usr/bin/bash"]
# Then, we define the "development" stage from the base one
FROM base AS development
ENV RAILS_ENV=development
# The major difference from the base image is that we may have development-only system
# dependencies (like Vim or graphviz).
# We extract them into the Aptfile.dev file.
COPY Aptfile.dev /tmp/Aptfile.dev
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/Aptfile.dev | xargs)
# The production-builder image is responsible for installing dependencies and compiling assets
FROM base as production-builder
# Git for installing gems from Github
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq \
&& apt-get dist-upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
git-core \
curl
COPY AptRMagickGemDependencies /tmp/AptRMagickGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptRMagickGemDependencies | xargs)
COPY AptReactRailsGemDependencies /tmp/AptReactRailsGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptReactRailsGemDependencies | xargs)
COPY AptPgGemDependencies /tmp/AptPgGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptPgGemDependencies | xargs)
COPY AptYarnDependencies /tmp/AptYarnDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptYarnDependencies | xargs)
# # Install yarn
# RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
# RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
# RUN apt -y update && apt install -y yarn
# First, we create and configure a dedicated user to run our application
RUN groupadd --gid 1005 tramway \
&& useradd --uid 1005 --gid tramway --shell /bin/bash --create-home tramway
USER tramway
RUN mkdir /home/tramway/app
WORKDIR /home/tramway/app
# Then, we re-configure Bundler
ENV RAILS_ENV=production \
LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3 \
BUNDLE_APP_CONFIG=/home/tramway/bundle \
BUNDLE_PATH=/home/tramway/bundle \
GEM_HOME=/home/tramway/bundle
# Install Ruby gems
COPY --chown=tramway:tramway Gemfile Gemfile.lock ./
RUN mkdir $BUNDLE_PATH
RUN bundle config --local deployment 'true'
RUN bundle config --local path "${BUNDLE_PATH}"
RUN bundle config --local without 'development test'
RUN bundle config --local clean 'true'
RUN bundle config --local no-cache 'true'
RUN bundle install --jobs=${BUNDLE_JOBS}
RUN rm -rf $BUNDLE_PATH/ruby/3.1.0/cache/*
RUN rm -rf /home/tramway/.bundle/cache/*
# # Install JS packages
# COPY --chown=tramway:tramway package.json yarn.lock ./
# RUN yarn install --check-files
# Copy code
COPY --chown=tramway:tramway ./app ./app
COPY --chown=tramway:tramway ./bin ./bin
COPY --chown=tramway:tramway ./config ./config
COPY --chown=tramway:tramway ./db ./db
COPY --chown=tramway:tramway ./lib ./lib
COPY --chown=tramway:tramway ./spec ./spec
COPY --chown=tramway:tramway ./config.ru ./config.ru
COPY --chown=tramway:tramway ./package.json ./package.json
COPY --chown=tramway:tramway ./yarn.lock ./yarn.lock
COPY --chown=tramway:tramway ./node_modules ./node_modules
# Precompile assets
# NOTE: The command may require adding some environment variables (e.g., SECRET_KEY_BASE) if you're not using
# credentials.
# RUN bundle exec rails assets:precompile
# Finally, our production image definition
# NOTE: It's not extending the base image, it's a new one
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME AS production
# Production-only dependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq \
&& apt-get dist-upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
curl \
wget \
gnupg2 \
less \
tzdata \
time \
locales \
make \
ffmpeg \
postgresql-client-common \
&& update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8
# Install nodejs
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
wget https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash - && \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends nodejs
# Upgrade RubyGems and install the latest Bundler version
RUN gem update --system && \
gem install bundler
COPY AptMimemagickGemDependencies /tmp/AptMimemagickGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptMimemagickGemDependencies | xargs)
COPY AptPgGemDependencies /tmp/AptPgGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptPgGemDependencies | xargs)
COPY AptRMagickGemDependencies /tmp/AptRMagickGemDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptRMagickGemDependencies | xargs)
COPY AptNodeModulesDependencies /tmp/AptNodeModulesDependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/AptNodeModulesDependencies | xargs)
# Install yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt -y update && apt install -y yarn
# Create and configure a dedicated user (use the same name as for the production-builder image)
RUN groupadd --gid 1005 tramway \
&& useradd --uid 1005 --gid tramway --shell /bin/bash --create-home tramway
RUN mkdir /home/tramway/app
WORKDIR /home/tramway/app
USER tramway
# Ruby/Rails env configuration
ENV RAILS_ENV=production \
BUNDLE_APP_CONFIG=/home/tramway/bundle \
BUNDLE_PATH=/home/tramway/bundle \
GEM_HOME=/home/tramway/bundle \
PATH="/home/tramway/app/bin:${PATH}" \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
RAILS_LOG_TO_STDOUT=true
EXPOSE 9292
# Copy code
COPY --chown=tramway:tramway . .
# Copy artifacts
# 1) Installed gems
COPY --from=production-builder $BUNDLE_PATH $BUNDLE_PATH
# Install JS packages
RUN yarn install --check-files
RUN bundle exec rails assets:precompile
# RUN sudo rm -rf node_modules
# 2) Compiled assets
# COPY --from=production-builder /home/tramway/app/public/packs /home/tramway/app/public/packs
# COPY --from=production-builder /home/tramway/app/public/assets /home/tramway/app/public/assets
# 3) We can even copy the Bootsnap cache to speed up our Rails server load!
# COPY --chown=tramway:tramway --from=production-builder /home/tramway/app/tmp/cache/bootsnap* /home/tramway/app/tmp/cache/