2
2
3
3
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4
4
ARG RUBY_VERSION=3.2.4
5
- FROM registry.docker.com/library/ruby:$RUBY_VERSION-alpine as base
5
+ FROM registry.docker.com/library/ruby:$RUBY_VERSION-alpine AS base
6
6
7
7
# Rails app lives here
8
8
WORKDIR /rails
@@ -24,15 +24,17 @@ RUN yarn install --frozen-lockfile --modules-folder $NODE_PATH
24
24
# Throw-away build stage to reduce size of final image
25
25
FROM base AS build
26
26
27
- RUN apk add --no-cache --virtual .build-deps build-base git postgresql-dev vips-dev tzdata pkgconfig
28
- RUN apk add --no-cache curl vips-dev postgresql-client tzdata
29
- RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
27
+ WORKDIR /rails
28
+
29
+ RUN apk add --no-cache --virtual .build-deps build-base git postgresql-dev vips-dev tzdata pkgconfig && \
30
+ apk add --no-cache curl vips-dev postgresql-client tzdata && \
31
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
30
32
31
33
# Install application gems
32
34
COPY Gemfile Gemfile.lock ./
33
- RUN bundle install
34
- RUN rm -rf ~/.bundle/ "${BUNDLE_PATH}" /ruby/*/cache "${BUNDLE_PATH}" /ruby/*/bundler/gems/*/.git
35
- RUN bundle exec bootsnap precompile --gemfile
35
+ RUN bundle install && \
36
+ rm -rf ~/.bundle/ "${BUNDLE_PATH}" /ruby/*/cache "${BUNDLE_PATH}" /ruby/*/bundler/gems/*/.git && \
37
+ bundle exec bootsnap precompile --gemfile
36
38
37
39
# Copy application code
38
40
COPY . .
@@ -41,32 +43,36 @@ COPY . .
41
43
RUN bundle exec bootsnap precompile app/ lib/
42
44
43
45
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
44
- RUN ./bin/rails assets:precompile
46
+ COPY --from=node /node_modules /node_modules
47
+ RUN apk add --virtual .build-deps yarn
48
+ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
45
49
46
50
# Final stage for app image
47
51
FROM base
48
52
53
+ WORKDIR /rails
54
+
49
55
# Copy built artifacts: gems, application
50
56
COPY --from=build /usr/local/bundle /usr/local/bundle
51
57
COPY --from=build /rails /rails
52
58
53
59
# Install packages needed for deployment
54
- RUN apk add --no-cache --virtual .build-deps curl build-base postgresql-dev vips-dev tzdata yarn
55
- RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
60
+ RUN apk add --no-cache --virtual .build-deps curl build-base postgresql-dev vips-dev tzdata yarn && \
61
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
56
62
57
63
# Copy node build artifacts
58
64
COPY --from=node /node_modules /node_modules
59
65
RUN yarn global add nodemon sass postcss-cli --prefix /usr/local
60
66
61
67
# Run and own only the runtime files as a non-root user for security
62
- RUN adduser -D rails --shell /bin/bash
68
+ RUN adduser -D rails --shell /bin/ash
63
69
RUN mkdir -p /usr/local/bundle/ruby/3.2.0/cache
64
70
RUN chown -R rails:rails db log storage tmp /usr/local/bundle/ruby/3.2.0/cache
65
71
RUN chmod -R 777 /usr/local/bundle/ruby/3.2.0/cache
66
72
USER rails:rails
67
73
68
74
# Entrypoint prepares the database.
69
- ENTRYPOINT ["/rails /bin/docker-entrypoint" ]
75
+ ENTRYPOINT [". /bin/docker-entrypoint" ]
70
76
71
77
# Start the server by default, this can be overwritten at runtime
72
78
EXPOSE 3000
0 commit comments