dockerfile-rails
dockerfile-rails copied to clipboard
Resync with rails 7.2.0 beta
Ideally the dockerfile rails generator will have no unintentional differences from the files produced by rails new
. Here are the current differences with Rails 7.2.0 beta 2:
Dockerfile:
diff --git a/Dockerfile b/Dockerfile
index 20245fa..48a9ce8 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,45 +1,38 @@
# syntax = docker/dockerfile:1
-# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
-# docker build -t my-app .
-# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app
-
-# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
-
-# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
+# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.1
-FROM docker.io/library/ruby:$RUBY_VERSION-slim as base
+FROM ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
-# Install base packages
-RUN apt-get update -qq && \
- apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libvips && \
- rm -rf /var/lib/apt/lists /var/cache/apt/archives
-
# Set production environment
-ENV RAILS_ENV="production" \
- BUNDLE_DEPLOYMENT="1" \
+ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
- BUNDLE_WITHOUT="development"
+ BUNDLE_WITHOUT="development:test" \
+ RAILS_ENV="production"
+
+# Update gems and bundler
+RUN gem update --system --no-document && \
+ gem install -N bundler
+
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems
RUN apt-get update -qq && \
- apt-get install --no-install-recommends -y build-essential git pkg-config && \
- rm -rf /var/lib/apt/lists /var/cache/apt/archives
+ apt-get install --no-install-recommends -y build-essential git pkg-config
# Install application gems
-COPY Gemfile Gemfile.lock ./
+COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
- rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
- bundle exec bootsnap precompile --gemfile
+ bundle exec bootsnap precompile --gemfile && \
+ rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
# Copy application code
-COPY . .
+COPY --link . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
@@ -48,11 +41,14 @@ RUN bundle exec bootsnap precompile app/ lib/
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
-
-
# Final stage for app image
FROM base
+# Install packages needed for deployment
+RUN apt-get update -qq && \
+ apt-get install --no-install-recommends -y curl libsqlite3-0 && \
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
+
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
@@ -60,12 +56,17 @@ COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
- chown -R rails:rails db log storage tmp
+ mkdir /data && \
+ chown -R 1000:1000 db log storage tmp /data
USER 1000:1000
+# Deployment options
+ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
+
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
+VOLUME /data
CMD ["./bin/rails", "server"]
.dockerignore:
diff --git a/.dockerignore b/.dockerignore
index cd7190b..9612375 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -2,7 +2,6 @@
# Ignore git directory.
/.git/
-/.gitignore
# Ignore bundler config.
/.bundle
@@ -36,13 +35,3 @@
/app/assets/builds/*
!/app/assets/builds/.keep
/public/assets
-
-# Ignore CI service files.
-/.github
-
-# Ignore development files
-/.devcontainer
-
-# Ignore Docker-related files
-/.dockerignore
-/Dockerfile*
bin/dockerfile-entrypoint:
index 840d093..67ef493 100755
--- a/bin/docker-entrypoint
+++ b/bin/docker-entrypoint
@@ -1,10 +1,5 @@
#!/bin/bash -e
-# Enable jemalloc for reduced memory usage and latency.
-if [ -z "${LD_PRELOAD+x}" ] && [ -f /usr/lib/*/libjemalloc.so.2 ]; then
- export LD_PRELOAD="$(echo /usr/lib/*/libjemalloc.so.2)"
-fi
-
# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare