-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
102 lines (80 loc) · 3.29 KB
/
Dockerfile
File metadata and controls
102 lines (80 loc) · 3.29 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
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=4.0.1
FROM ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Set environment
ENV BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_APP_CONFIG="/usr/local/bundle" \
GEM_HOME="/usr/local/bundle" \
PATH="/usr/local/bundle/bin:${PATH}"
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems and Node.js for asset compilation
# Also include packages needed for testing when building test image
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips libyaml-dev pkg-config curl jq && \
curl -fsSL https://deb.nodesource.com/setup_24.x | bash - && \
apt-get install -y nodejs && \
npm install -g yarn
# Install application gems
COPY Gemfile ./
COPY Gemfile.lock* ./
# Remove any existing bundle config and set proper configuration
RUN rm -rf .bundle ~/.bundle && \
bundle config unset frozen && \
bundle config set --global path "${BUNDLE_PATH}" && \
bundle config set --global without "" && \
bundle config unset with && \
bundle lock --add-platform aarch64-linux-gnu --add-platform x86_64-linux && \
bundle install --jobs 4 --retry 3 && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Copy application code
COPY . .
# Install JavaScript dependencies
RUN if [ -f "package.json" ]; then \
yarn install --production=false; \
fi
# Build assets (CSS and JavaScript)
RUN if [ -f "package.json" ]; then \
yarn build:css && \
yarn build; \
fi
# Precompile assets with Propshaft
RUN SECRET_KEY_BASE_DUMMY=1 RAILS_ENV=production NODE_ENV=production YARN_PRODUCTION=false SKIP_FLIPPER_PRELOAD=1 ./bin/rails assets:precompile
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Final stage for app image
FROM base
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libvips libyaml-0-2 postgresql-client jq && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
# Remove any host bundle config that might have been copied
RUN rm -rf /rails/.bundle
# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
mkdir -p db log storage tmp && \
chown -R rails:rails db log storage tmp && \
chown -R rails:rails /usr/local/bundle
USER rails:rails
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
# Test stage - can be targeted with --target test
FROM build as test
# Install packages needed for testing
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y postgresql-client jq && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Set test environment
ENV RAILS_ENV=test
# Default command for test stage
CMD ["bundle", "exec", "rspec"]