forked from hackclub/hcb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduction.Dockerfile
More file actions
114 lines (93 loc) · 2.96 KB
/
production.Dockerfile
File metadata and controls
114 lines (93 loc) · 2.96 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
103
104
105
106
107
108
109
110
111
112
113
114
# syntax=docker/dockerfile:1
# check=error=true
ARG RUBY_VERSION=3.4.7
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /app
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
curl \
# Active Storage
imagemagick \
libvips \
poppler-utils \
# Better memory management
libjemalloc2 \
# Postgres
postgresql \
# Poppler gem (https://github.com/ruby-gnome/ruby-gnome/tree/main/poppler)
gir1.2-freedesktop \
gir1.2-glib-2.0 \
libcairo-gobject2 \
libgirepository-1.0-1 \
libpoppler-glib-dev \
# OCR
tesseract-ocr && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Set production environment
ENV RAILS_ENV="production" \
RACK_ENV="production" \
NODE_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base AS build
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
build-essential \
git \
libyaml-dev \
pkg-config \
# Building node
node-gyp \
python-is-python3 \
# Postgres
libpq-dev \
# Poppler gem
libcairo2-dev \
libgirepository1.0-dev \
libglib2.0-dev \
&& \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
ARG NODE_VERSION=22.10.0
ARG YARN_VERSION=1.22
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Install application gems
COPY Gemfile Gemfile.lock vendor .ruby-version ./
RUN gem install bundler -v 2.5.17
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
RUN yarn cache clean
# Copy application code
COPY . .
# Remove non-prod master key
RUN rm -f config/master.key
# Precompile assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
RUN rm -rf node_modules
# Final stage for app image
FROM base
# Add build timestamp
RUN date +%s > .build-timestamp
# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash
USER 1000:1000
# Copy built artifacts: gems, application
COPY --chown=rails:rails --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --chown=rails:rails --from=build /app /app
# Entrypoint prepares the database.
ENTRYPOINT ["./bin/docker-entrypoint"]
# Start the server
EXPOSE 3000
CMD ["./bin/rails", "server"]