-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,105 @@ | ||
ARG RUBY_VER="3.3" | ||
############################## | ||
# 1) BUILD STAGE | ||
############################## | ||
FROM ruby:$RUBY_VER-alpine AS build-env | ||
|
||
ARG PACKAGES="git libxml2 libxslt build-base curl-dev libxml2-dev libxslt-dev zlib-dev tzdata libpq-dev yaml-dev" | ||
|
||
RUN apk update && \ | ||
apk upgrade && \ | ||
apk add --update --no-cache $PACKAGES && \ | ||
cp /usr/share/zoneinfo/Australia/Sydney /etc/localtime && \ | ||
# 1a) Packages required to build native extensions + runtime libs | ||
# (e.g. if you need xml or postgres in production). | ||
ARG BUILD_PACKAGES="build-base curl-dev libxml2-dev libxslt-dev zlib-dev libpq-dev yaml-dev git" | ||
# 1b) Minimal runtime libraries you actually need | ||
# (remove anything you do not actually use in production) | ||
ARG RUNTIME_PACKAGES="tzdata libxml2 libxslt curl zlib libpq yaml" | ||
|
||
ENV RAILS_ENV=production \ | ||
RACK_ENV=production \ | ||
# Exclude dev/test gems so they’re not installed at all | ||
BUNDLE_WITHOUT=development:test \ | ||
BUNDLE_FROZEN=1 \ | ||
# Where gems will live in the image | ||
BUNDLE_PATH=/app/vendor/bundle | ||
|
||
RUN apk add --no-cache $BUILD_PACKAGES $RUNTIME_PACKAGES | ||
|
||
# Optional: If you do NOT strictly need a correct timezone in production, | ||
# you can remove tzdata to save a few MB: | ||
# RUN apk add --no-cache $BUILD_PACKAGES $RUNTIME_PACKAGES && \ | ||
# apk del tzdata | ||
|
||
# Set timezone if needed | ||
RUN cp /usr/share/zoneinfo/Australia/Sydney /etc/localtime && \ | ||
echo "Australia/Sydney" > /etc/timezone | ||
|
||
ENV APP_DIR="/app" | ||
RUN mkdir $APP_DIR | ||
WORKDIR $APP_DIR | ||
WORKDIR /app | ||
|
||
# Copy Gemfiles first for layer caching | ||
COPY Gemfile* ./ | ||
|
||
ENV BUNDLE_APP_CONFIG="$APP_DIR/.bundle" | ||
|
||
COPY Gemfile* $APP_DIR/ | ||
RUN gem install bundler | ||
RUN bundle config set without 'test:assets' | ||
RUN bundle config set --local path 'vendor/bundle' | ||
RUN bundle config set --local without 'test development' | ||
RUN bundle config --global frozen 1 \ | ||
&& bundle install -j4 --retry 3 \ | ||
&& bundle binstubs bundler puma --force \ | ||
# Remove unneeded files (cached *.gem, *.o, *.c) | ||
&& rm -rf vendor/bundle/ruby/3.3.0/cache/*.gem \ | ||
&& find vendor/bundle/ruby/3.3.0/gems/ -name "*.c" -delete \ | ||
&& find vendor/bundle/ruby/3.3.0/gems/ -name "*.o" -delete | ||
# Install bundler (no docs) | ||
RUN gem install bundler --no-document | ||
|
||
# Install production gems | ||
RUN bundle install -j4 --retry 3 | ||
|
||
# Copy the rest of your Rails code | ||
COPY . . | ||
|
||
RUN rm -rf /app/tmp/pids/ && rm -rf /app/spec | ||
# Remove any stale binstubs referencing dev/test gems | ||
RUN rm -rf bin/* | ||
|
||
# Instead of `bundle binstubs bundler`, which triggers dev/test checks, | ||
# just binstub puma (or skip binstubs entirely and use `bundle exec puma`) | ||
RUN bundle binstubs puma --force | ||
|
||
############### Build step done ############### | ||
# Clean up gem caches, .o/.c files, leftover test dirs | ||
RUN rm -rf vendor/bundle/ruby/3.3.0/cache/*.gem && \ | ||
find vendor/bundle/ruby/3.3.0/gems/ -name "*.c" -delete && \ | ||
find vendor/bundle/ruby/3.3.0/gems/ -name "*.o" -delete && \ | ||
rm -rf tmp/pids spec | ||
|
||
############################## | ||
# 2) FINAL STAGE | ||
############################## | ||
FROM ruby:$RUBY_VER-alpine | ||
|
||
# Copy the application and bundled gems | ||
# Keep only the minimal runtime libs you truly need in production | ||
ARG RUNTIME_PACKAGES="libxml2 libxslt curl zlib libpq yaml tzdata" | ||
RUN apk add --no-cache $RUNTIME_PACKAGES | ||
|
||
# Again, if tzdata is not strictly needed, omit it: | ||
# RUN apk add --no-cache libxml2 libxslt curl zlib libpq yaml | ||
|
||
# Optional: set timezone if you kept tzdata | ||
RUN cp /usr/share/zoneinfo/Australia/Sydney /etc/localtime && \ | ||
echo "Australia/Sydney" > /etc/timezone | ||
|
||
ENV APP_DIR="/app" | ||
COPY --from=build-env $APP_DIR $APP_DIR | ||
WORKDIR $APP_DIR | ||
|
||
ENV BUNDLE_APP_CONFIG="$APP_DIR/.bundle" | ||
# Keep same environment so bundler won't look for dev/test | ||
ENV RAILS_ENV=production \ | ||
RACK_ENV=production \ | ||
BUNDLE_WITHOUT=development:test \ | ||
BUNDLE_FROZEN=1 \ | ||
BUNDLE_PATH=$APP_DIR/vendor/bundle | ||
|
||
# Install runtime packages | ||
ARG PACKAGES="tzdata libxml2 libxslt libc6-compat libpq-dev yaml-dev" | ||
RUN apk update \ | ||
&& apk upgrade \ | ||
&& apk add --update --no-cache $PACKAGES | ||
# Copy only the fully built app (with installed gems) from builder | ||
COPY --from=build-env /app /app | ||
|
||
# Create a non-privileged user | ||
# Create non-privileged user | ||
ARG IMAGE_UID="10001" | ||
ENV UID=$IMAGE_UID | ||
ENV USER=appuser | ||
RUN adduser -D -g "" -h "/nonexistent" -s "/sbin/nologin" -H -u "${IMAGE_UID}" appuser && \ | ||
chown -R appuser:appuser $APP_DIR | ||
|
||
RUN adduser -D -g "" -h "/nonexistent" -s "/sbin/nologin" -H -u "${UID}" "${USER}" | ||
RUN chown appuser:appuser -R /app/tmp | ||
RUN chown appuser:appuser -R /app/config/ | ||
RUN chown appuser:appuser -R /app/vendor/bundle # Ensure appuser owns the gems | ||
USER appuser | ||
EXPOSE 8080 | ||
|
||
# Use the unprivileged user | ||
USER appuser:appuser | ||
# Healthcheck, optional | ||
HEALTHCHECK CMD ["wget","--no-verbose","-q","--spider","http://0.0.0.0:8080/auth/authority?health=true"] | ||
|
||
EXPOSE 8080 | ||
HEALTHCHECK CMD ["wget", "--no-verbose", "-q", "--spider", "http://0.0.0.0:8080/auth/authority?health=true"] | ||
# Use `bundle exec puma` if you prefer. | ||
# If you *only* generated `bin/puma` (and not `bin/bundle`), then: | ||
ENTRYPOINT ["./bin/puma", "-b", "tcp://0.0.0.0:8080"] | ||
|
||
# Or (if you didn't generate a puma binstub at all) do: | ||
# ENTRYPOINT ["bundle", "exec", "puma", "-b", "tcp://0.0.0.0:8080"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.