Skip to content

Commit

Permalink
Move production to the default and devlopment to alternate
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwi committed Oct 15, 2023
1 parent 7840d6b commit d20e2ba
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
file: ./production.Dockerfile
file: ./Dockerfile
push: false
load: true
tags: ${{ env.TEST_TAG }}
Expand Down
74 changes: 62 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# OS Level Dependencies
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_JOBS="4" \
BUNDLE_WITHOUT="development:test" \
BUNDLE_PATH="/usr/local/bundle"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
# NOTE: This example project intentionally does not require or install node.js

RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
Expand All @@ -17,20 +32,55 @@ RUN --mount=type=cache,target=/var/cache/apt \
less \
git \
libpq-dev \
postgresql-client \
libvips \
curl
pkg-config

ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3

RUN gem update --system && gem install bundler
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

WORKDIR /usr/src/app
# Copy application code
COPY . .

ENTRYPOINT ["./bin/docker-entrypoint"]
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

EXPOSE 3000
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0"]

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq \
&& apt-get install -yq --no-install-recommends \
curl \
postgresql-client \
libvips

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER rails:rails

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint-production"]

HEALTHCHECK --interval=15s --timeout=3s --start-period=0s --start-interval=5s --retries=3 \
CMD curl -f http://localhost:3000/up || exit 1

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ docker compose up --build

### with [BuildKit](https://docs.docker.com/build/buildkit/)
```
DOCKER_BUILDKIT=1 docker build --tag rails-on-docker --file production.Dockerfile . --load
DOCKER_BUILDKIT=1 docker build --tag rails-on-docker --load .
```

Test the image can be used and Rails starts up, use a fake key for testing purposes only:
Expand Down
22 changes: 14 additions & 8 deletions bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/bash
set -e
#!/bin/bash -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /usr/src/app/tmp/pids/server.pid
echo "entrypoint"
echo "${*}"

echo "bundle install..."
bundle check || bundle install --jobs 4
# NOTE: Enable this as you need to get migrations working in your deployed
# (i.e. Production) environments. Match the condition check to what you have in
# the Dockerfile CMD. I use puma in the production.Dockerfile as that's the
# recommended option by the Puma team (https://github.com/puma/puma#rails), but
# the default Rails Dockerfile uses `./bin/rails server`
#
# If running the rails server then create or migrate existing database
# if [ "${*}" == "./bin/rails server" ]; then
# ./bin/rails db:prepare
# fi

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
exec "${@}"
11 changes: 11 additions & 0 deletions bin/docker-entrypoint-development
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /usr/src/app/tmp/pids/server.pid

echo "bundle install..."
bundle check || bundle install --jobs 4

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
21 changes: 0 additions & 21 deletions bin/docker-entrypoint-production

This file was deleted.

4 changes: 3 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
services:
web:
build: .
build:
context: ./
dockerfile: development.Dockerfile
command: bash -c "rm -f tmp/pids/server.pid && bin/rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/usr/src/app
Expand Down
36 changes: 36 additions & 0 deletions development.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# syntax=docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# OS Level Dependencies
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq \
&& apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
less \
git \
libpq-dev \
postgresql-client \
libvips \
curl

ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3

RUN gem update --system && gem install bundler

WORKDIR /usr/src/app

ENTRYPOINT ["./bin/docker-entrypoint-development"]

EXPOSE 3000

CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0"]
86 changes: 0 additions & 86 deletions production.Dockerfile

This file was deleted.

0 comments on commit d20e2ba

Please sign in to comment.