Skip to content

Instantly share code, notes, and snippets.

@chunlea
Created January 26, 2022 21:08
Show Gist options
  • Save chunlea/d96230db631bb5249456d74c55870e10 to your computer and use it in GitHub Desktop.
Save chunlea/d96230db631bb5249456d74c55870e10 to your computer and use it in GitHub Desktop.
Dockerfile for Rails with devcontainer support
# *****************************************************************************
# * Base image based on offical ruby alpine image with minimal dependencies. *
# *****************************************************************************
FROM ruby:3.1.0-alpine as base
RUN apk add --update --no-cache ca-certificates gcompat libpq tzdata
# *****************************************************************************
# * Builder image with all dependencies installed. *
# *****************************************************************************
FROM base as builder
ARG GITHUB_PACKAGES_TOKEN
ENV BUNDLE_RUBYGEMS__PKG__GITHUB__COM ${GITHUB_PACKAGES_TOKEN}
ENV NODE_AUTH_TOKEN ${GITHUB_PACKAGES_TOKEN}
RUN apk add --update --no-cache \
build-base \
curl \
git \
graphicsmagick \
libffi-dev \
libsodium-dev \
libxml2-dev \
libxslt-dev \
nodejs \
openssh-client \
postgresql-dev \
yarn
# *****************************************************************************
# * DevContainer image based on builder with scripts to setup dev environment *
# * Reference: https://github.com/microsoft/vscode-dev-containers *
# *****************************************************************************
FROM builder as devcontainer
ARG SCRIPT_LIBRARY_VERSION=0.210.0
ARG INSTALL_ZSH="true"
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
ADD https://raw.githubusercontent.com/microsoft/vscode-dev-containers/v${SCRIPT_LIBRARY_VERSION}/script-library/common-alpine.sh /tmp/library-scripts/
RUN apk update && ash /tmp/library-scripts/common-alpine.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" \
&& rm -rf /tmp/library-scripts
# Default value to allow debug server to serve content over GitHub Codespace's port forwarding service
# The value is a comma-separated list of allowed domains
ENV RAILS_DEVELOPMENT_HOSTS=".githubpreview.dev,.test"
# *****************************************************************************
# * PreProd image based on builder to build production image *
# *****************************************************************************
FROM builder as pre-prod
ENV RAILS_ENV production
ENV SECRET_KEY_BASE fake-secret-key-base
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile Gemfile.lock /app/
RUN bundle config --global frozen 1 \
&& bundle config set --local without 'development test' \
&& bundle install -j4 --retry 3 \
&& rm -rf /usr/local/bundle/bundler/gems/*/.git /usr/local/bundle/cache/
COPY package.json yarn.lock .yarnrc .npmrc /app/
RUN yarn install
COPY . /app/
RUN bundle exec rails assets:precompile
# *****************************************************************************
# * Prod image is the production image w/o dev dependencies and node_modules *
# *****************************************************************************
FROM base as prod
ENV RAILS_ENV production
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN mkdir -p /app
WORKDIR /app
COPY --from=pre-prod /usr/local/bundle/ /usr/local/bundle/
COPY --from=pre-prod /app/ /app/
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment