環境
- Rails 7.0.5
- Postgres
- Docker 20.10.23
Dockerfile
開発環境、プロダクション環境共用。
# Build stage
FROM ruby:3.2.2-alpine AS build
ARG UID=1000
ARG GID=1000
ARG RAILS_ENV=production
ENV APP_USER=app
ENV WORKDIR=/project
# yarn は rails assets:precompile のために必要。
RUN apk add --no-cache --update build-base \
linux-headers \
postgresql-dev \
tzdata \
busybox-suid \
shared-mime-info \
gcompat \
yarn
RUN set -x && \
addgroup -g ${GID} -S ${APP_USER} && \
adduser -g '' -S -D -u ${UID} ${APP_USER} && \
mkdir -p ${WORKDIR} && \
chown -R ${APP_USER}:${APP_USER} ${WORKDIR}
WORKDIR ${WORKDIR}
USER ${APP_USER}
COPY --chown=${APP_USER}:${APP_USER} Gemfile Gemfile.lock ./
RUN if [ "$RAILS_ENV" = "production" ]; then \
bundle config set --local without 'development test' \
; fi
RUN bundle install
COPY --chown=${APP_USER}:${APP_USER} . ./
RUN bundle exec rake assets:precompile
# Final stage
FROM ruby:3.2.2-alpine
ARG UID=1000
ARG GID=1000
ARG RAILS_ENV=production
ENV APP_USER=app
ENV WORKDIR=/project
ENV RAILS_LOG_TO_STDOUT=true
# gcompatが無いとrails起動時にnokogiriがエラーになる
# https://github.com/sparklemotion/nokogiri/issues/2416
# https://nokogiri.org/tutorials/installing_nokogiri.html#linux-musl-error-loading-shared-library
RUN apk add --no-cache --update \
postgresql-dev \
tzdata \
busybox-suid \
shared-mime-info \
gcompat
RUN set -x && \
addgroup -g ${GID} -S ${APP_USER} && \
adduser -g '' -S -D -u ${UID} ${APP_USER} && \
mkdir -p ${WORKDIR} && \
chown -R ${APP_USER}:${APP_USER} ${WORKDIR}
WORKDIR ${WORKDIR}
USER ${APP_USER}
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=${APP_USER}:${APP_USER} ${WORKDIR} ${WORKDIR}
CMD [ "rails", "s", "-b", "0.0.0.0" ]
EXPOSE 3000
.dockerignore
.dockerignore
*.md
*.log
*.pid
Dockerfile*
/.bundle
/.circleci
/.dockerignore
/.editorconfig
/.env*
!/.env.production
!/.env.staging
/.git
/.gitattributes
/.github
/.gitignore
/.overcommit.yml
/.rspec
/.rubocop.yml
/.ruby-lsp
/Procfile.dev
/app/assets/builds/*
/coverage
/docker
/docker-compose.yml
/docs
/log
/miniodata
/node_modules
/public/assets
/storage
!/storage/.keep
/out
/spec
/tmp
/vendor/bundle