0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerfileのARGはFROMに対しても使える

Posted at

よく考えれば当たり前なのかもしれないが最近気付いたのでメモ

ARGで処理系のバージョン等を設定しておいてFROMにそれを埋め込めばmulti-stage buildを使う時にバージョンを一元管理できたりと色々便利そう

ARG RUBY_VERSION=2.6.2
ARG RAILS_ENV=production

FROM ruby:${RUBY_VERSION}-alpine AS node_modules
WORKDIR /opt/project
RUN apk add --no-cache --update nodejs yarn
ADD bin/yarn /opt/project/bin/
ADD package.json yarn.lock /opt/project/
ENV PATH=/opt/project/bin:${PATH}
RUN yarn install


FROM ruby:${RUBY_VERSION}-alpine AS bundle
WORKDIR /opt/project
RUN apk add --no-cache --update build-base mariadb-dev
ADD bin/bundle /opt/project/bin/
ADD .ruby-version .gemrc Gemfile Gemfile.lock /opt/project/
ENV PATH=/opt/project/bin:${PATH}
RUN \
if [ "${RAILS_ENV}" = "production" ]; then \
  bundle install --jobs 8 --without development test --deployment; \
else \
  bundle install --jobs 8 --without production --path vendor/bundle; \
fi


FROM ruby:${RUBY_VERSION}-alpine
WORKDIR /opt/project
RUN apk add --no-cache --update mariadb-dev tzdata nodejs yarn
COPY --from=bundle ${BUNDLE_APP_CONFIG} /opt/project/.bundle
COPY --from=bundle /opt/project/vendor/bundle /opt/project/vendor/bundle
COPY --from=node_modules /opt/project/node_modules /opt/project/node_modules
ADD . /opt/project/api
ENV RAILS_ENV=${RAILS_ENV}
ENV BUNDLE_APP_CONFIG=/opt/project/.bundle
ENV PATH=/opt/project/bin:${PATH}
ENV RAILS_SERVE_STATIC_FILES=true
RUN webpack
EXPOSE 3000
ENTRYPOINT ["rails"]
CMD ["server", "-b", "0.0.0.0"]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?