Repository navigation
Expand file tree
/
Copy pathDockerfile.prod
More file actions
138 lines (110 loc) · 5.93 KB
/
Copy pathDockerfile.prod
File metadata and controls
138 lines (110 loc) · 5.93 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# syntax = docker/dockerfile:1
# check=error=true
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t quepid .
# docker run -d -p 80:3000 -e RAILS_MASTER_KEY=<value from config/master.key> --name quepid quepid
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=4.0.9
FROM docker.io/library/ruby:$RUBY_VERSION-slim-trixie AS base
# Accept what version of Quepid we are cutting.
ARG QUEPID_VERSION_VARIABLE
# Rails app lives here
WORKDIR /rails
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl default-mysql-client libjemalloc2 libvips libyaml-dev zip && \
ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Set production environment variables and enable jemalloc for reduced memory usage and latency.
# PORT matches the EXPOSE below - without it, Foreman (which runs the Procfile) picks its own
# default of 5000 for the web process, pre-empting Puma's own ENV.fetch("PORT", 3000) fallback.
#
# Don't set SECRET_KEY_BASE here: ENV values are baked into image layers/history for
# anyone to read, and BuildKit's SecretsUsedInArgOrEnv lint (a hard error via the
# `check=error=true` directive above) rejects it. See bin/docker-entrypoint for the
# runtime fallback instead.
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development test" \
LD_PRELOAD="/usr/local/lib/libjemalloc.so" \
PUPPETEER_SKIP_DOWNLOAD="true" \
PORT="3000"
# 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 default-libmysqlclient-dev git node-gyp pkg-config python-is-python3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install JavaScript dependencies
ARG NODE_VERSION=26.13.0
ARG YARN_VERSION=1.22.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
RUN gem install foreman
COPY Gemfile Gemfile.lock vendor ./
RUN bundle install --jobs=4 --retry=3 && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --production --frozen-lockfile
# Copy application code
COPY . .
# config/database.yml is gitignored (holds no secrets - real config comes from
# DATABASE_URL/DB_ADAPTER at runtime - but is generated locally by bin/setup*,
# which this image doesn't run). Fall back to the checked-in sample so Rails
# has a database.yml to parse; asset precompilation below boots the full app
# without DATABASE_URL set, and Rails raises if database.yml is missing
# entirely rather than falling back to DATABASE_URL alone.
RUN if [ ! -f config/database.yml ]; then cp config/database.yml.sample config/database.yml; fi
# Precompile bootsnap code for faster boot times.
# -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
RUN bundle exec bootsnap precompile -j 1 app/ lib/
ADD https://github.com/o19s/quepid-jupyterlite/releases/latest/download/jupyter-lite-build.tgz ./notebooks.gz
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
# SKIP_YARN_INSTALL avoids jsbundling-rails re-running a full (non---production) `yarn install`
# as a prerequisite of assets:precompile, which would otherwise reinstall devDependencies
# (test tooling like puppeteer/jshint) that this build image doesn't need or support.
RUN SECRET_KEY_BASE_DUMMY=1 SKIP_YARN_INSTALL=1 ./bin/rails assets:precompile
# Right now we don't relocate via sprockets/propshaft all JS and CSS assets
# so we need to keep this.
#RUN rm -rf node_modules
# Final stage for app image
FROM base
# Kamal refuses to deploy an image without this exact label.
LABEL service="quepid"
ENV QUEPID_VERSION=$QUEPID_VERSION_VARIABLE
# Install Node.js and yarn in final image for asset recompilation for nested paths /quepid at runtime
ARG NODE_VERSION=26.13.0
ARG YARN_VERSION=1.22.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
# Run and own only the runtime files as a non-root user for security
# This is commented out because if we want to nest Quepid under a directory we have to redo
# the assets on disk which requires additional permissions.
#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 /rails /rails
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Holds the SQLite database when running without DATABASE_URL (the default bare docker run).
# Mount your own directory here for persistence:
# docker run -p 3000:3000 -v ./quepid_db:/quepid_db o19s/quepid:latest
# Without an explicit mount, Docker provides an anonymous managed volume so data
# is not silently lost on container stop, but you'll want a bind mount for real persistence.
VOLUME /quepid_db
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["foreman", "s", "-f", "Procfile"]