-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDockerfile
119 lines (90 loc) · 4.33 KB
/
Dockerfile
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
# 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 dummy .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name dummy dummy
# 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=3.3.6
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /app
ENV BUNDLE_PATH="/usr/local/bundle"
# The development stage is used to run the app locally as serves as the base for building the version
# that will contain the data for prod.
FROM base AS common-build
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips lsb-release gnupg2 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Set up jemalloc for better memory management
RUN ln -s /usr/lib/*-linux-gnu/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
ENV LD_PRELOAD=/usr/lib/libjemalloc.so.2
ENV MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"
# Install Postgres 16, so that schema dumping works
RUN echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# Trust the PGDG gpg key
RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc| gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config libpq-dev postgresql-client-16 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install application gems
COPY Gemfile Gemfile.lock ./
# Install npm packages
COPY .nvmrc package.json yarn.lock ./
RUN if [ $(uname -m) = "aarch64" ]; then NODE_ARCH=arm64 ; else NODE_ARCH=x64 ; fi; \
uname -m && \
NODE_VERSION=$(cat .nvmrc) && \
NODE_TAR_FILE="node-$NODE_VERSION-linux-$NODE_ARCH.tar.gz" && \
curl -s "https://nodejs.org/dist/$NODE_VERSION/$NODE_TAR_FILE" --output $NODE_TAR_FILE && \
mkdir -p /opt/nodejs && \
tar -xvzf "$NODE_TAR_FILE" -C /opt/nodejs/ && \
mv "/opt/nodejs/node-$NODE_VERSION-linux-$NODE_ARCH" "/opt/nodejs/current" && \
ln -s /opt/nodejs/current/bin/node /usr/local/bin/node && \
ln -s /opt/nodejs/current/bin/npm /usr/local/bin/npm && \
rm "node-$NODE_VERSION-linux-$NODE_ARCH.tar.gz" && \
node -v
RUN npm install --global yarn && ln -s /opt/nodejs/current/bin/yarn /usr/local/bin/yarn
RUN yarn install
FROM common-build AS dev
# Run bundle install with all packages
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
EXPOSE 80
# Entrypoint script always runs, even if command is overwritten
ENTRYPOINT ["/app/config/docker/dev-entrypoint.sh"]
# CMD script doesn't run if command is overwritten (e.g. for migrations)
CMD ["bundle exec puma"]
FROM common-build AS prod-build
# Copy application code
COPY . .
# Bundle but without dev and test groups
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_WITHOUT="development:test"
RUN rm -rf "${BUNDLE_PATH}" && \
bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 PRECOMPILING_ASSETS=true ./bin/rails assets:precompile
# Final stage for app image
FROM base AS prod
# Install packages needed to run the app
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y libpq5 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=prod-build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=prod-build /app /app
RUN rm -rf /app/tmp/cache /app/tmp/pids /app/tmp/sockets /app/log
RUN mkdir /app/tmp/pids
EXPOSE 3000
# Entrypoint script always runs, even if command is overwritten
ENTRYPOINT ["/app/config/docker/entrypoint.sh"]
# CMD script doesn't run if command is overwritten (e.g. for migrations)
CMD ["bundle exec puma"]