Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dockerfile Multi-stage build examples

About Multi-stage build

FROM node:14.17-slim AS build-node-modules

WORKDIR /app

COPY package.json /app/
COPY yarn.lock /app/

RUN yarn install


FROM ruby:2.7.2-slim

WORKDIR /app

COPY --from=build-node-modules /app/node_modules node_modules/

Node and Ruby hybrid example

FROM node:14.17-slim AS node
FROM node:14.17-slim AS build-node-modules

WORKDIR /app

COPY package.json /app/
COPY yarn.lock /app/

RUN yarn install


FROM ruby:2.7.2-slim AS ruby-base

WORKDIR /app

ENV BUNDLE_APP_CONFIG .bundle
ENV BUNDLE_PATH vendor/bundle

# For nokogiri
# https://nokogiri.org/tutorials/installing_nokogiri.html#installing-using-standard-system-libraries
# For mysql
# https://github.com/brianmario/mysql2#linux-and-other-unixes
RUN apt-get update -y && apt-get install -y \
    build-essential \
    pkg-config libxml2-dev libxslt-dev \
    libmariadb-dev


FROM ruby-base AS build-ruby-gems

COPY Gemfile* /app/

RUN bundle config build.nokogiri --use-system-libraries \
 && bundle install -j4


FROM ruby-base

WORKDIR /app

RUN apt-get install -y \
    tzdata \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

COPY --from=node /usr/local/bin/node /usr/local/bin/
COPY --from=node /opt/yarn* /opt/yarn/
RUN ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn \
 && ln -s /opt/yarn/bin/yarnpkg /usr/local/bin/yarnpkg

COPY --from=build-node-modules /app/node_modules node_modules/
COPY --from=build-ruby-gems /app/.bundle .bundle/
COPY --from=build-ruby-gems /app/vendor vendor/

VOLUME /app/node_modules /app/.bundle /app/vendor

COPY . /app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment