1
1
# syntax = docker/dockerfile:1
2
2
3
- # Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
3
+ # This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
4
+ # docker build -t my-app .
5
+ # docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app
6
+
7
+ # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
8
+
9
+ # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
4
10
ARG RUBY_VERSION=your-ruby-version
5
- FROM registry. docker.com /library/ruby:$RUBY_VERSION-slim as base
11
+ FROM docker.io /library/ruby:$RUBY_VERSION-slim as base
6
12
7
13
# Rails app lives here
8
14
WORKDIR /rails
9
15
16
+ # Install base packages
17
+ RUN apt-get update -qq && \
18
+ apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libvips && \
19
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
20
+
10
21
# Set production environment
11
22
ENV RAILS_ENV="production" \
12
23
BUNDLE_DEPLOYMENT="1" \
13
24
BUNDLE_PATH="/usr/local/bundle" \
14
25
BUNDLE_WITHOUT="development"
15
26
16
-
17
27
# Throw-away build stage to reduce size of final image
18
28
FROM base as build
19
29
20
30
# Install packages needed to build gems
21
31
RUN apt-get update -qq && \
22
- apt-get install --no-install-recommends -y build-essential git libvips pkg-config
32
+ apt-get install --no-install-recommends -y build-essential git pkg-config && \
33
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
23
34
24
35
# Install application gems
25
36
COPY Gemfile Gemfile.lock ./
@@ -37,22 +48,20 @@ RUN bundle exec bootsnap precompile app/ lib/
37
48
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
38
49
39
50
51
+
52
+
40
53
# Final stage for app image
41
54
FROM base
42
55
43
- # Install packages needed for deployment
44
- RUN apt-get update -qq && \
45
- apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
46
- rm -rf /var/lib/apt/lists /var/cache/apt/archives
47
-
48
56
# Copy built artifacts: gems, application
49
- COPY --from=build /usr/local/bundle /usr/local/bundle
57
+ COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
50
58
COPY --from=build /rails /rails
51
59
52
60
# Run and own only the runtime files as a non-root user for security
53
- RUN useradd rails --create-home --shell /bin/bash && \
61
+ RUN groupadd --system --gid 1000 rails && \
62
+ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
54
63
chown -R rails:rails db log storage tmp
55
- USER rails:rails
64
+ USER 1000:1000
56
65
57
66
# Entrypoint prepares the database.
58
67
ENTRYPOINT ["/rails/bin/docker-entrypoint" ]
0 commit comments