LoginSignup
16
10

More than 3 years have passed since last update.

Docker + Rails のdocker-compose build でGemNotFoundExceptionの時の対処

Last updated at Posted at 2019-09-05

対象者

docker-compose build や docker-compose up で GemNotFoundException が吐き出されたときの対処法

エラー内容
/usr/local/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': Could not find 'bundler' (2.0.2) required by your /myapp/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.0.2`
        from /usr/local/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
        from /usr/local/bin/bundle:23:in `<main>'
ERROR: Service 'web' failed to build: The command '/bin/sh -c bundle install' returned a non-zero code: 1

経緯

https://kic-yuuki.hatenablog.com/entry/2019/02/25/091235
https://qiita.com/at-946/items/f6fbf0115d373ce58be5
これらを参考に bootstrap から Materialize に移行しようとしてました。
そこでGemfileに追加してbundle install し、docker-compose build でエラーが吐かれました。

原因

bundler のバージョンが違うため。

解決法

Dockerfile にbundle install する前に gem install bundler を走らせればとりあえず解決。

Dockerfile
FROM ruby:2.6.3

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
        && apt-get install -y nodejs

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

CMD ["rails", "server", "-b", "0.0.0.0"]
Dockerfile
++ RUN gem install bundler
Dockerfile
FROM ruby:2.6.3

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
        && apt-get install -y nodejs

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler
RUN bundle install
COPY . /myapp

CMD ["rails", "server", "-b", "0.0.0.0"]
16
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
10