Rails 7 Dockerfile build fails

I just generated a brand-new app (Ruby 3.3.3, Rails 7.1.3.4), which generated the following Dockerfile:

# syntax = docker/dockerfile:1

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

# Rails app lives here
WORKDIR /rails

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


# 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 curl git libpq-dev libvips pkg-config unzip

ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
ARG BUN_VERSION=1.1.13
RUN curl -fsSL https://bun.sh/install | bash -s -- "bun-v${BUN_VERSION}"

# 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

# Install node modules
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
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 libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# 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"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

Without any change, when I try to build the docker image: docker build -t abc/sample-app:latest ., the build fails:

9.616 (net-protocol (>= 0)).
9.616 Running `bundle update net-pop` should fix the problem.
------
Dockerfile:31
--------------------
  30 |     COPY Gemfile Gemfile.lock ./
  31 | >>> RUN bundle install && \
  32 | >>>     rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
  33 | >>>     bundle exec bootsnap precompile --gemfile
  34 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c bundle install &&     rm -rf ~/.bundle/ \"${BUNDLE_PATH}\"/ruby/*/cache \"${BUNDLE_PATH}\"/ruby/*/bundler/gems/*/.git &&     bundle exec bootsnap precompile --gemfile" did not complete successfully: exit code: 34

I tried to run a ruby container (docker run -it --rm --entrypoint sh ruby:latest) where I only put the Gemfile, and when running inside the container bundle install, it works. but build still fails

Any idea what’s going on here?


EDIT:

Using the disposable container, I copied onlye Gemfile and Gemfile lock, and when I try to ruby the following command:

bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile

I got the following error:

Installing net-protocol 0.2.2
Downloading net-pop-0.1.2 revealed dependencies not in the API or the lockfile (net-protocol (>= 0)).
Running `bundle update net-pop` should fix the problem.

Running bundle update net-pop does not solve the problem

UPDATE 1:

Solved the problem by adding the following line into my Gemfile:

gem 'net-pop', github: 'ruby/net-pop'
1 Like

This issue is likely due to net-protocol gets removed with ruby 3.3.3 · Issue #26 · ruby/net-pop · GitHub Bug #20581: Ruby 3.3.3 install has missing deps for bundled net-pop gem - Ruby master - Ruby Issue Tracking System

As updated, until Ruby 3.3.4 is released, adding this entry to the application Gemfile should be a good workaround.

gem 'net-pop', github: 'ruby/net-pop'
3 Likes