-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDockerfile
More file actions
102 lines (78 loc) · 3.04 KB
/
Copy pathDockerfile
File metadata and controls
102 lines (78 loc) · 3.04 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 it matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=4.0.5
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
RUBY_YJIT_ENABLE="1"
# Throw-away build stage to reduce size of final image
FROM base AS build
# Install packages needed to build gems and Node.js for Tailwind CSS
ARG PNPM_VERSION=10.28.0
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
build-essential git pkg-config curl libyaml-dev libssl-dev libvips ca-certificates && \
curl -fsSL https://deb.nodesource.com/setup_24.x | bash - && \
apt-get install --no-install-recommends -y nodejs && \
npm install -g pnpm@$PNPM_VERSION && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle clean --force && \
gem install thruster && \
gem cleanup
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile --gemfile
# Install Node dependencies (before copying app for better caching)
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
# Copy application code
COPY . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Build Tailwind CSS
RUN pnpm run build:css
# Precompile assets and clean up build artifacts
RUN mkdir -p /rails/storage/logs && \
SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \
rm -rf node_modules tmp/cache .git
# Final stage for app image
FROM base
# Install runtime packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
curl libsqlite3-0 libvips libjemalloc2 libyaml-0-2 ca-certificates \
ffmpeg redis-server git sqlite3 && \
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
# Configure environment defaults
ENV LD_PRELOAD="/usr/local/lib/libjemalloc.so" \
HTTP_IDLE_TIMEOUT=60 \
HTTP_READ_TIMEOUT=300 \
HTTP_WRITE_TIMEOUT=300
# Create non-root user for running the application
RUN groupadd -r sabha && useradd -r -g sabha -d /rails -s /bin/bash sabha
# Copy built artifacts: gems, application (--chown avoids extra chown -R layer)
COPY --from=build --chown=sabha:sabha /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=sabha:sabha /rails /rails
# Set version
ARG APP_VERSION
ENV APP_VERSION=$APP_VERSION
# Image metadata (source/description set by CI via docker/metadata-action labels)
LABEL org.opencontainers.image.licenses="MIT"
# Expose app ports
EXPOSE 3000
# Health check
HEALTHCHECK --interval=5s --timeout=3s --start-period=30s --retries=3 \
CMD curl -f http://localhost:3000/up || exit 1
# Switch to non-root user
USER sabha
# Start the server
CMD ["bin/boot"]