2
2
3
3
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4
4
ARG RUBY_VERSION=3.3.0
5
+
6
+
7
+ # #########################################################################################
8
+ # BASE: Shared base docker image
9
+ # #########################################################################################
5
10
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
6
11
7
12
# Rails app lives here
@@ -10,53 +15,102 @@ WORKDIR /rails
10
15
# Set production environment
11
16
ENV RAILS_ENV="production" \
12
17
BUNDLE_DEPLOYMENT="1" \
13
- BUNDLE_PATH="/usr/local/bundle" \
14
- BUNDLE_WITHOUT="development"
18
+ BUNDLE_PATH="/usr/local/bundle"
19
+
20
+ # Start the server by default, this can be overwritten at runtime
21
+ EXPOSE 3000
15
22
16
23
17
- # Throw-away build stage to reduce size of final image
24
+ # #########################################################################################
25
+ # BUILD: Throw-away build stage
26
+ # #########################################################################################
18
27
FROM base as build
19
28
20
29
# Install packages needed to build gems
21
30
RUN apt-get update -qq && \
22
- apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config
31
+ apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config npm
32
+
33
+ # Install npm packages
34
+ COPY package.json package-lock.json ./
35
+
36
+ # Install npm packages
37
+ RUN npm install
38
+
39
+
40
+ # #########################################################################################
41
+ # DEV: Used for development and test
42
+ # #########################################################################################
43
+ FROM build as dev
44
+
45
+ ENV RAILS_ENV="development"
46
+
47
+ # Install packages needed for development
48
+ RUN apt-get update -qq && \
49
+ apt-get install --no-install-recommends -y postgresql-client graphviz && \
50
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
51
+
52
+ # Install application gems for development
53
+ COPY Gemfile Gemfile.lock ./
54
+ RUN bundle config set --local without production && \
55
+ bundle install && \
56
+ rm -rf ~/.bundle/ "${BUNDLE_PATH}" /ruby/*/cache "${BUNDLE_PATH}" /ruby/*/bundler/gems/*/.git
23
57
24
- # Install application gems
58
+ # Copy application code
59
+ COPY . .
60
+
61
+ CMD ["./bin/dev" ]
62
+
63
+
64
+ # #########################################################################################
65
+ # RELEASE-BUILD: Throw-away build stage for RELEASE
66
+ # #########################################################################################
67
+ FROM build as release-build
68
+
69
+ # Install application gems for production
25
70
COPY Gemfile Gemfile.lock ./
26
- RUN bundle install && \
27
- rm -rf ~/. bundle/ "${BUNDLE_PATH}" /ruby/*/cache "${BUNDLE_PATH}" /ruby/*/bundler/gems/*/.git && \
28
- bundle exec bootsnap precompile --gemfile
71
+ RUN bundle config set --local without development test && \
72
+ bundle install && \
73
+ rm -rf ~/.bundle/ "${BUNDLE_PATH}" /ruby/*/cache "${BUNDLE_PATH}" /ruby/*/bundler/gems/*/.git
29
74
30
75
# Copy application code
31
76
COPY . .
32
77
33
78
# Precompile bootsnap code for faster boot times
34
- RUN bundle exec bootsnap precompile app/ lib/
79
+ RUN bundle exec bootsnap precompile --gemfile app/ lib/
35
80
36
81
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
37
82
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
38
83
39
84
40
- # Final stage for app image
41
- FROM base
85
+ # #########################################################################################
86
+ # RELEASE: Used for production
87
+ # #########################################################################################
88
+ FROM base as release
89
+
90
+ # Set production environment
91
+ ENV RAILS_ENV="production" \
92
+ BUNDLE_DEPLOYMENT="1" \
93
+ BUNDLE_PATH="/usr/local/bundle"
42
94
43
95
# Install packages needed for deployment
44
96
RUN apt-get update -qq && \
45
- apt-get install --no-install-recommends -y curl libvips postgresql-client && \
46
- rm -rf /var/lib/apt/lists /var/cache/apt/archives
97
+ apt-get install -y --no-install-recommends unzip python3-venv python-is-python3 curl libvips postgresql-client && \
98
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
99
+ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" && \
100
+ unzip awscli-bundle.zip && \
101
+ ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws && \
102
+ rm -rf ./awscli-bundle awscli-bundle.zip
103
+
104
+ # Install custom db migrate script
105
+ COPY bin/db-migrate /usr/bin/
47
106
48
107
# Copy built artifacts: gems, application
49
- COPY --from=build /usr/local/bundle /usr/local/bundle
50
- COPY --from=build /rails /rails
108
+ COPY --from=release- build /usr/local/bundle /usr/local/bundle
109
+ COPY --from=release- build /rails /rails
51
110
52
111
# Run and own only the runtime files as a non-root user for security
53
112
RUN useradd rails --create-home --shell /bin/bash && \
54
113
chown -R rails:rails db log storage tmp
55
114
USER rails:rails
56
115
57
- # Entrypoint prepares the database.
58
- ENTRYPOINT ["/rails/bin/docker-entrypoint" ]
59
-
60
- # Start the server by default, this can be overwritten at runtime
61
- EXPOSE 3000
62
116
CMD ["./bin/rails" , "server" ]
0 commit comments