Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Rails 8 upgrade #359

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
.DS_Store
.bin
.git
.gitignore
.bundleignore
.bundle
.byebug_history
.rspec
tmp
log
test
config/credentials.yml.enc
config/master.key
public/packs
public/packs-test
node_modules
yarn-error.log

# Ignore CI service files.
/.github
# See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files.

# Ignore git directory.
/.git/
/.gitignore

# Ignore bundler config.
/.bundle

# Ignore all environment files.
/.env*

# Ignore all default key files.
/config/master.key
/config/credentials/*.key

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/.keep

# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/.keep

# Ignore assets.
/node_modules/
/app/assets/builds/*
!/app/assets/builds/.keep
/public/assets

# Ignore development files
/.devcontainer

# Ignore Docker-related files
/.dockerignore
/Dockerfile*

.rspec
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.

# Mark the database schema as having been generated.
db/schema.rb linguist-generated

# Mark any vendored files as having been vendored.
vendor/* linguist-vendored
config/credentials/*.yml.enc diff=rails_credentials
config/credentials.yml.enc diff=rails_credentials
48 changes: 21 additions & 27 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Temporary files generated by your text editor or operating system
# belong in git's global ignore instead:
# `$XDG_CONFIG_HOME/git/ignore` or `~/.config/git/ignore`

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-*
# Ignore all environment files.
/.env*

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore uploaded files in development.
# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/
!/tmp/storage/.keep

/public/assets
.byebug_history

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/credentials.yml.enc

/public/packs
/public/packs-test
/node_modules
.pnp.*
.yarn/*

spec/examples.txt

.env
.envrc
config/credentials.yml.enc

## custom
docker/**/*.env
/app/assets/builds/*
!/app/assets/builds/.keep

CURRENT_VERSION
.makerc-vars

# Vite Ruby
.yarn
/public/vite*
node_modules
# Vite uses dotenv and suggests to ignore local-only env files. See
# https://vitejs.dev/guide/env-and-mode.html#env-files
*.local

.env
.makerc-vars
179 changes: 98 additions & 81 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,115 +1,132 @@
# jemalloc builder
FROM ruby:3.3.6-alpine AS builder_jemalloc
# syntax=docker/dockerfile:1
# check=error=true

RUN apk add build-base
RUN wget -O - https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2 | tar -xj && \
cd jemalloc-5.3.0 && \
./configure && \
make && \
make install

# ruby builder image
FROM ruby:3.3.6-alpine AS builder

RUN apk add --no-cache --update build-base \
linux-headers \
git \
postgresql-dev \
tzdata \
less \
sudo
RUN gem install bundler --no-document

COPY Gemfile Gemfile.lock /srv/
WORKDIR /srv
# RUN bundle config --global frozen 1 && bundle install --no-binstubs --without development test --jobs $(nproc) --retry 3
RUN bundle config --global frozen 1 && bundle install --no-binstubs --jobs $(nproc) --retry 3

FROM ruby:3.3.6-alpine AS development
ARG RUBY_VERSION=3.3.6

# BASE IMAGE
FROM ruby:$RUBY_VERSION-alpine AS base
ARG CONTAINER_USER_ID
ARG CONTAINER_GROUP_ID
ARG CONTAINER_USER_NAME
ENV CONTAINER_USER_NAME=${CONTAINER_USER_NAME:-app} \
CONTAINER_USER_ID=${CONTAINER_USER_ID:-1000} \
CONTAINER_GROUP_ID=${CONTAINER_GROUP_ID:-1000} \
APP_PATH="/srv/app"

ENV APP_PATH /srv/app
ENV RAILS_LOG_TO_STDOUT true
ENV CONTAINER_USER_NAME=${CONTAINER_USER_NAME:-app}
ENV CONTAINER_USER_ID=${CONTAINER_USER_ID:-1000}
ENV CONTAINER_GROUP_ID=${CONTAINER_GROUP_ID:-1000}
RUN mkdir -p $APP_PATH
WORKDIR $APP_PATH
RUN apk add --no-cache --update bash

RUN apk add --no-cache --update build-base \
linux-headers \
git \
curl \
postgresql-client \
# DEV BUILDER IMAGE
FROM base AS builder_development
COPY Gemfile Gemfile.lock ./
RUN apk add --no-cache --update \
build-base \
postgresql-dev \
nodejs-current \
git
RUN bundle install --jobs $(nproc) --retry 3 && \
rm -rf ~/.bundle/ /usr/local/bundle/ruby/*/cache /usr/local/bundle/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

## DEVELOPMENT IMAGE
FROM base AS development
ENV RAILS_ENV="development"
RUN apk add --no-cache --update \
build-base \
git \
tzdata \
nodejs-current \
sqlite \
postgresql-client \
less \
graphviz \
ttf-dejavu

RUN addgroup -S -g ${CONTAINER_GROUP_ID} $CONTAINER_USER_NAME && adduser -S -u ${CONTAINER_USER_ID} -g $CONTAINER_USER_NAME -h /home/$CONTAINER_USER_NAME -s /bin/bash $CONTAINER_USER_NAME

# jemalloc
COPY --from=builder_jemalloc /usr/local/lib/libjemalloc.so.2 /usr/local/lib/
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2
RUN corepack enable
RUN addgroup -S -g ${CONTAINER_GROUP_ID} $CONTAINER_USER_NAME && \
adduser -S -u ${CONTAINER_USER_ID} -g $CONTAINER_USER_NAME -h /home/$CONTAINER_USER_NAME -s /bin/bash $CONTAINER_USER_NAME

# gems
COPY --from=builder --chown=${CONTAINER_USER_ID}:${CONTAINER_GROUP_ID} /usr/local/bundle/ /usr/local/bundle/
COPY --from=builder_development --chown=${CONTAINER_USER_ID}:${CONTAINER_GROUP_ID} /usr/local/bundle /usr/local/bundle

RUN corepack enable

RUN mkdir $APP_PATH
WORKDIR $APP_PATH
USER $CONTAINER_USER_NAME

CMD ["rails", "server", "-b", "0.0.0.0"]
# Entrypoint prepares the database and the rest of environment
ENTRYPOINT ["/srv/app/docker/dev/docker-entrypoint.sh"]
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]

# real image
FROM ruby:3.3.6-alpine AS production

ARG CONTAINER_USER_ID
ARG CONTAINER_GROUP_ID
ARG CONTAINER_USER_NAME

ENV CONTAINER_USER_NAME=${CONTAINER_USER_NAME:-app}
ENV CONTAINER_USER_ID=${CONTAINER_USER_ID:-1000}
ENV CONTAINER_GROUP_ID=${CONTAINER_GROUP_ID:-1000}
ENV APP_PATH /srv/app
ENV RAILS_ENV production
ENV NODE_ENV production
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_SERVE_STATIC_FILES true
ENV RUBY_GC_HEAP_INIT_SLOTS 2000000
ENV RUBY_HEAP_FREE_MIN 20000
ENV RUBY_GC_MALLOC_LIMIT 100000000








## JEMALLOC IMAGE
FROM base AS builder_jemalloc
RUN apk add --no-cache --update build-base
RUN wget -O - https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2 | tar -xj && \
cd jemalloc-5.3.0 && \
./configure && \
make && \
make install

# BUILDER IMAGE
FROM base AS builder_prod
ENV RAILS_ENV=production \
NODE_ENV=production \
BUNDLE_FROZEN="1" \
BUNDLE_WITHOUT="development"

RUN apk add --no-cache --update \
postgresql-client \
build-base \
bash \
git \
tzdata \
postgresql-dev \
nodejs-current \
tzdata \
less

COPY --from=builder_jemalloc /usr/local/lib/libjemalloc.so.2 /usr/local/lib/
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2

RUN corepack enable

RUN addgroup -S -g ${CONTAINER_GROUP_ID} $CONTAINER_USER_NAME && adduser -S -u ${CONTAINER_USER_ID} -g $CONTAINER_USER_NAME -h /home/$CONTAINER_USER_NAME -s /bin/bash $CONTAINER_USER_NAME

COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
COPY Gemfile Gemfile.lock ./
RUN bundle install --no-binstubs --jobs $(nproc) --retry 3 && \
rm -rf ~/.bundle/ /usr/local/bundle/ruby/*/cache /usr/local/bundle/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

COPY . .
RUN bundle exec bootsnap precompile app/ lib/
RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile && \
rm -r node_modules .yarn

## PRODUCTION IMAGE
FROM base AS production
ENV RAILS_ENV=production \
NODE_ENV=production \
BUNDLE_WITHOUT="development" \
RAILS_SERVE_STATIC_FILES=true \
RUBY_GC_HEAP_INIT_SLOTS=2000000 \
RUBY_HEAP_FREE_MIN=20000 \
RUBY_GC_MALLOC_LIMIT=100000000

COPY --from=builder_prod /usr/local/bundle /usr/local/bundle
COPY --from=builder_prod $APP_PATH $APP_PATH
COPY --from=builder_jemalloc /usr/local/lib/libjemalloc.so.2 /usr/local/lib/

RUN mkdir -p $APP_PATH
WORKDIR $APP_PATH
COPY --chown=$CONTAINER_USER_NAME:$CONTAINER_USER_NAME . $APP_PATH
ADD ./docker/prod/docker-entrypoint.sh $APP_PATH
RUN apk add --no-cache --update \
libpq \
tzdata

RUN chown $CONTAINER_USER_NAME:$CONTAINER_USER_NAME $APP_PATH
RUN addgroup -S -g ${CONTAINER_GROUP_ID} $CONTAINER_USER_NAME && \
adduser -S -u ${CONTAINER_USER_ID} -g $CONTAINER_USER_NAME -h /home/$CONTAINER_USER_NAME -s /bin/bash $CONTAINER_USER_NAME && \
chown $CONTAINER_USER_NAME:$CONTAINER_USER_NAME db log storage tmp

USER $CONTAINER_USER_NAME
RUN yarn --immutable
RUN DATABASE_URL=postgresql://db SECRET_KEY_BASE=`bin/rails secret` bundle exec rails assets:precompile

CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
# Entrypoint prepares the database.
ENTRYPOINT ["/srv/app/docker/prod/docker-entrypoint.sh"]
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
Loading