@@ -8,36 +8,36 @@ FROM ruby:$RUBY_VERSION-slim as base
8
8
WORKDIR /rails
9
9
10
10
# Set production environment
11
- ENV RAILS_ENV="production" \
12
- BUNDLE_WITHOUT="development:test" \
13
- BUNDLE_DEPLOYMENT="1"
11
+ ENV BUNDLE_DEPLOYMENT="1" \
12
+ BUNDLE_PATH="/usr/local/bundle" \
13
+ BUNDLE_WITHOUT="development:test" \
14
+ RAILS_ENV="production"
14
15
15
16
# Update gems and bundler
16
17
RUN gem update --system --no-document && \
17
- gem install -N bundler
18
+ gem install -N bundler
18
19
19
- # Throw-away build stages to reduce size of final image
20
- FROM base as prebuild
20
+
21
+ # Throw-away build stage to reduce size of final image
22
+ FROM base as build
21
23
22
24
# Install packages needed to build gems
23
25
RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
24
- --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
25
- apt-get update -qq && \
26
- apt-get install --no-install-recommends -y build-essential curl libpq-dev pkg-config python-is-python3 libyaml-dev
27
-
28
- FROM prebuild as build
26
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
27
+ apt-get update -qq && \
28
+ apt-get install --no-install-recommends -y build-essential libpq-dev libyaml-dev pkg-config
29
29
30
30
# Install application gems
31
31
COPY --link Gemfile Gemfile.lock ./
32
32
RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
33
- bundle config set app_config .bundle && \
34
- bundle config set path /srv/vendor && \
35
- bundle install && \
36
- bundle exec bootsnap precompile --gemfile && \
37
- bundle clean && \
38
- mkdir -p vendor && \
39
- bundle config set path vendor && \
40
- cp -ar /srv/vendor .
33
+ bundle config set app_config .bundle && \
34
+ bundle config set path /srv/vendor && \
35
+ bundle install && \
36
+ bundle exec bootsnap precompile --gemfile && \
37
+ bundle clean && \
38
+ mkdir -p vendor && \
39
+ bundle config set path vendor && \
40
+ cp -ar /srv/vendor .
41
41
42
42
# Copy application code
43
43
COPY --link . .
@@ -46,36 +46,37 @@ COPY --link . .
46
46
RUN bundle exec bootsnap precompile app/ lib/
47
47
48
48
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
49
- # We need to run precompile twice so Propshaft sees the newly compiled assets
50
- RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
51
49
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
52
50
51
+
53
52
# Final stage for app image
54
53
FROM base
55
54
56
55
# Install packages needed for deployment
57
56
RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
58
- --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
59
- apt-get update -qq && \
60
- apt-get install --no-install-recommends -y curl postgresql-client ruby-foreman sudo unzip
57
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
58
+ apt-get update -qq && \
59
+ apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libyaml-dev postgresql-client ruby-foreman sudo
61
60
62
61
# Copy built artifacts: gems, application
63
- COPY --from=build /usr/local/bundle /usr/local/bundle
62
+ COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
64
63
COPY --from=build /rails /rails
65
64
66
65
# Run and own only the runtime files as a non-root user for security
67
66
RUN groupadd --system --gid 1000 rails && \
68
- useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
69
- sed -i 's/env_reset/env_keep="*"/' /etc/sudoers && \
70
- chown -R 1000:1000 db log tmp
67
+ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
68
+ sed -i 's/env_reset/env_keep="*"/' /etc/sudoers && \
69
+ chown -R 1000:1000 db log storage tmp
71
70
USER 1000:1000
72
71
73
72
# Deployment options
74
- ENV RUBY_YJIT_ENABLE="1"
73
+ ENV LD_PRELOAD="libjemalloc.so.2" \
74
+ MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true" \
75
+ RUBY_YJIT_ENABLE="1"
75
76
76
77
# Entrypoint prepares the database.
77
78
ENTRYPOINT ["/rails/bin/docker-entrypoint" ]
78
79
79
80
# Start the server by default, this can be overwritten at runtime
80
- EXPOSE 3000
81
+ EXPOSE 80
81
82
CMD ["foreman" , "start" , "--procfile=Procfile" ]
0 commit comments