Skip to content

Instantly share code, notes, and snippets.

@Epictetus
Forked from nagait84/Dockerfile
Created January 1, 2021 04:59
Show Gist options
  • Save Epictetus/64b27b74e3a9ff01eab30d8e87e5986e to your computer and use it in GitHub Desktop.
Save Epictetus/64b27b74e3a9ff01eab30d8e87e5986e to your computer and use it in GitHub Desktop.
【メモ】マルチステージビルドを用いたRails用イメージ(開発・本番)
FROM ruby:2.6.3-alpine as base
LABEL maintainer="nagait84 <[email protected]>"
# ビルド時の作業ディレクトリ
WORKDIR /app
# bundler インストールパスの指定
ENV BUNDLE_APP_CONFIG /app/.bundle
# Railsのログは標準出力に流す
ENV RAILS_LOG_TO_STDOUT TRUE
# AlpineLinuxPackagesで必要そうなコマンドをインストール
# 追加するときは apk search [パッケージ名] で探して追記。
RUN apk update && \
apk upgrade && \
apk add --update --no-cache \
build-base \
git \
imagemagick \
imagemagick-dev \
libxml2-dev \
libxslt-dev \
mysql-client \
mysql-dev \
nodejs \
ruby-dev \
tzdata \
yarn
# yarnインストールの実行(先にlockファイルをコピーしてバージョンを固定)
COPY package.json yarn.lock ./
RUN yarn install
# ################################## #
# # ここから先は開発用設定 #
# ################################## #
# マルチステージビルドで開発用イメージを作成
FROM base as dev_mode
# 開発環境でのみ必要なパッケージをインストール
RUN apk add --update --no-cache \
fontconfig \
graphviz \
msttcorefonts-installer
# フォントファイルの更新
RUN update-ms-fonts && \
fc-cache -f
# ################################## #
# # ここから先は本番用設定 #
# ################################## #
# マルチステージビルドで本番用イメージを作成
FROM base as prod_mode
# Railsの秘密情報のマスターキー(docker build時にオプションで宣言する)
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY ${RAILS_MASTER_KEY}
# 本番モードで起動する
ENV RAILS_ENV production
# アプリのソースをイメージ内にコピー
COPY ./ ./
# Bundlerの設定を行い、bundle installを実行
RUN bundle config --local path vendor/bundle
RUN bundle config --local build.nokogiri --use-system-libraries
RUN bundle install --path vendor/bundle
# フロント用のライブラリのインストールと静的ファイルの作成
RUN bundle exec rails yarn:install
RUN bundle exec rails webpacker:compile
# コンパイル後、マスターキー情報はイメージから消しておく
ENV RAILS_MASTER_KEY=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment