docker-hy.github.io icon indicating copy to clipboard operation
docker-hy.github.io copied to clipboard

Broken Rails Project Dockerfile Example (Part 1, Section 6)

Open BartBucknill opened this issue 3 years ago • 0 comments

When using the examples given in the text to create a Dockerfile for running the Rails app, the db:migrate command fails due to a missing dependency. If all app files (not just Gemfile and Gemfile.lock) are instead copied into the container prior to running bundle install, the migrate command executes without error, and the container builds and runs.

How to Replicate

Dockerfile from Examples in Text

Create a Dockerfile from the examples in the text for the Rails app from the examples here, as below:

# We need ruby 3.1.0. I found this from Docker Hub
FROM ruby:3.1.0

EXPOSE 3000

WORKDIR /usr/src/app

# Install the correct bundler version
RUN gem install bundler:2.3.3

# Copy the files required for dependencies to be installed
COPY Gemfile* ./

# Install all dependencies
RUN bundle install

# Copy all of the source code
COPY . .

# We pick the production guide mode since we have no intention of developing the software inside the container.
# Run database migrations by following instructions from README
RUN rails db:migrate RAILS_ENV=production

# Precompile assets by following instructions from README
RUN rake assets:precompile

# And finally the command to run the application
CMD ["rails", "s", "-e", "production"]

Build and Run

docker build . -t rails-project && docker run -p 127.0.0.1:3000:3000 rails-project

Actual Result

docker build . -t rails-project && docker run -p 127.0.0.1:3000:3000 rails-project
[+] Building 11.0s (11/12)
 => [internal] load build definition from Dockerfile                                     0.0s
 => => transferring dockerfile: 746B                                                     0.0s
 => [internal] load .dockerignore                                                        0.0s
 => => transferring context: 34B                                                         0.0s
 => [internal] load metadata for docker.io/library/ruby:3.1.0                            0.0s
 => [internal] load build context                                                        0.0s
 => => transferring context: 5.81kB                                                      0.0s
 => [1/8] FROM docker.io/library/ruby:3.1.0                                              0.0s
 => CACHED [2/8] WORKDIR /usr/src/app                                                    0.0s
 => [3/8] COPY Gemfile* ./                                                               0.0s
 => [4/8] RUN gem install bundler:2.3.3                                                  1.0s
 => [5/8] RUN bundle install                                                             8.9s
 => [6/8] COPY . .                                                                       0.0s
 => ERROR [7/8] RUN rails db:migrate RAILS_ENV=production                                1.0s
------
 > [7/8] RUN rails db:migrate RAILS_ENV=production:
#11 0.966 rails aborted!
#11 0.968 LoadError: cannot load such file -- nokogiri
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:15:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:15:in `require'
#11 0.968 /usr/local/bundle/gems/loofah-2.13.0/lib/loofah.rb:4:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/rails-html-sanitizer-1.4.2/lib/rails-html-sanitizer.rb:2:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/actionview-7.0.1/lib/action_view/helpers/sanitize_helper.rb:3:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/actionview-7.0.1/lib/action_view/helpers.rb:8:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/rails/context.rb:1:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/railtie.rb:10:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/rails.rb:3:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:73:in `rescue in block in require'
#11 0.968 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:51:in `block in require'
#11 0.968 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:44:in `each'
#11 0.968 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:44:in `require'
#11 0.968 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler.rb:176:in `require'
#11 0.968 /usr/src/app/config/application.rb:7:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:42:in `require_relative'
#11 0.968 /usr/src/app/Rakefile:4:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
#11 0.968 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands/rake/rake_command.rb:20:in `block in perform'
#11 0.968 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#11 0.968 /usr/local/bundle/gems/railties-7.0.1/lib/rails/command.rb:51:in `invoke'
#11 0.968 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands.rb:18:in `<main>'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.968 bin/rails:4:in `<main>'
#11 0.968
#11 0.968 Caused by:
#11 0.978 LoadError: cannot load such file -- sprockets-rails
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:15:in `require'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:15:in `require'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:55:in `each'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:55:in `block in require'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:44:in `each'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler/runtime.rb:44:in `require'
#11 0.978 /usr/local/bundle/gems/bundler-2.3.3/lib/bundler.rb:176:in `require'
#11 0.978 /usr/src/app/config/application.rb:7:in `<main>'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:42:in `require_relative'
#11 0.978 /usr/src/app/Rakefile:4:in `<main>'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
#11 0.978 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands/rake/rake_command.rb:20:in `block in perform'
#11 0.978 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#11 0.978 /usr/local/bundle/gems/railties-7.0.1/lib/rails/command.rb:51:in `invoke'
#11 0.978 /usr/local/bundle/gems/railties-7.0.1/lib/rails/commands.rb:18:in `<main>'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.978 /usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
#11 0.978 bin/rails:4:in `<main>'
#11 0.978 (See full trace by running task with --trace)
------
executor failed running [/bin/sh -c rails db:migrate RAILS_ENV=production]: exit code: 1

Working Dockerfile

The Dockerfile below first copies all app files into the container (including Gemfile and Gemfile.lock), before installing dependencies, this builds and runs OK.

# We need ruby 3.1.0. I found this from Docker Hub
FROM ruby:3.1.0

EXPOSE 3000

WORKDIR /usr/src/app

# Install the correct bundler version
RUN gem install bundler:2.3.3

# Copy all of the source code
COPY . .
# Copy the files required for dependencies to be installed
# COPY Gemfile* ./

# Install all dependencies
RUN bundle install

# We pick the production guide mode since we have no intention of developing the software inside the container.
# Run database migrations by following instructions from README
RUN rails db:migrate RAILS_ENV=production

# Precompile assets by following instructions from README
RUN rake assets:precompile

# And finally the command to run the application
CMD ["rails", "s", "-e", "production"]

BartBucknill avatar Apr 22 '22 09:04 BartBucknill