前提
こちらの記事はDockerに対して不慣れな人間が書いているので、そらそうやろ!という内容が含まれます。
それでも個人用メモとして放流します。
技術要素
Dockerを扱うディレクトリ構成
- アプリディレクトリ/
- Gemfile
- Gemfile.lock
- Dockerfile
- docker-compose.yml
遭遇したエラー
docker-compose run web bundle install --path vendor/bundler
を実行した後なのに
docker-compose up
でサーバーを起動しようとすると Run bundle install to install missing gems.
というエラーが出てしまう...
Could not find bson-4.4.2 in any of the sources Run `bundle install` to install missing gems.
bson-4.4.2
が無い?(bundle installしたのになぜ?)
関連しそうなファイルの構成
Gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.2', '>= 5.2.2.1' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false gem 'mongoid' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Gemfile.lock
GEM remote: https://rubygems.org/ specs: actioncable (5.2.2.1) actionpack (= 5.2.2.1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) actionmailer (5.2.2.1) actionpack (= 5.2.2.1) actionview (= 5.2.2.1) activejob (= 5.2.2.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) actionpack (5.2.2.1) actionview (= 5.2.2.1) activesupport (= 5.2.2.1) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (5.2.2.1) activesupport (= 5.2.2.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) activejob (5.2.2.1) activesupport (= 5.2.2.1) globalid (>= 0.3.6) activemodel (5.2.2.1) activesupport (= 5.2.2.1) activerecord (5.2.2.1) activemodel (= 5.2.2.1) activesupport (= 5.2.2.1) arel (>= 9.0) activestorage (5.2.2.1) actionpack (= 5.2.2.1) activerecord (= 5.2.2.1) marcel (~> 0.3.1) activesupport (5.2.2.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.6.0) public_suffix (>= 2.0.2, < 4.0) archive-zip (0.12.0) io-like (~> 0.3.0) arel (9.0.0) bindex (0.5.0) bootsnap (1.4.1) msgpack (~> 1.0) bson (4.4.2) builder (3.2.3) byebug (11.0.1) capybara (3.15.0) addressable mini_mime (>= 0.1.3) nokogiri (~> 1.8) rack (>= 1.6.0) rack-test (>= 0.6.3) regexp_parser (~> 1.2) xpath (~> 3.2) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) chromedriver-helper (2.1.0) archive-zip (~> 0.10) nokogiri (~> 1.8) coffee-rails (4.2.2) coffee-script (>= 2.2.0) railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.12.2) concurrent-ruby (1.1.5) crass (1.0.4) erubi (1.8.0) execjs (2.7.0) ffi (1.10.0) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.6.0) concurrent-ruby (~> 1.0) io-like (0.3.0) jbuilder (2.8.0) activesupport (>= 4.2.0) multi_json (>= 1.2) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) loofah (2.2.3) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) method_source (0.9.2) mimemagic (0.3.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) mongo (2.7.1) bson (>= 4.4.2, < 5.0.0) mongoid (7.0.2) activemodel (>= 5.1, < 6.0.0) mongo (>= 2.5.1, < 3.0.0) msgpack (1.2.9) multi_json (1.13.1) nio4r (2.3.1) nokogiri (1.10.1) mini_portile2 (~> 2.4.0) public_suffix (3.0.3) puma (3.12.1) rack (2.0.6) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.2.1) actioncable (= 5.2.2.1) actionmailer (= 5.2.2.1) actionpack (= 5.2.2.1) actionview (= 5.2.2.1) activejob (= 5.2.2.1) activemodel (= 5.2.2.1) activerecord (= 5.2.2.1) activestorage (= 5.2.2.1) activesupport (= 5.2.2.1) bundler (>= 1.3.0) railties (= 5.2.2.1) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) railties (5.2.2.1) actionpack (= 5.2.2.1) activesupport (= 5.2.2.1) method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rake (12.3.2) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) regexp_parser (1.3.0) ruby_dep (1.5.0) rubyzip (1.2.2) sass (3.7.3) sass-listen (~> 4.0.0) sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) selenium-webdriver (3.141.0) childprocess (~> 0.5) rubyzip (~> 1.2, >= 1.2.2) spring (2.0.2) activesupport (>= 4.2) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) thor (0.20.3) thread_safe (0.3.6) tilt (2.0.9) turbolinks (5.2.0) turbolinks-source (~> 5.2) turbolinks-source (5.2.0) tzinfo (1.2.5) thread_safe (~> 0.1) uglifier (4.1.20) execjs (>= 0.3.0, < 3) web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) websocket-driver (0.7.0) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS ruby DEPENDENCIES bootsnap (>= 1.1.0) byebug capybara (>= 2.15) chromedriver-helper coffee-rails (~> 4.2) jbuilder (~> 2.5) listen (>= 3.0.5, < 3.2) mongoid puma (~> 3.11) rails (~> 5.2.2, >= 5.2.2.1) sass-rails (~> 5.0) selenium-webdriver spring spring-watcher-listen (~> 2.0.0) turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) web-console (>= 3.3.0) RUBY VERSION ruby 2.5.1p57 BUNDLED WITH 1.17.3
Dockerfile
FROM ruby:2.5.1 RUN apt-get update -qq && apt-get install -y build-essential nodejs RUN mkdir /app WORKDIR /app COPY Gemfile /app/Gemfile COPY Gemfile.lock /app/Gemfile.lock RUN gem install bundler RUN bundle install COPY . /app
docker-compose.yml
version: '3' services: web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/app ports: - 3000:3000 depends_on: - db tty: true stdin_open: true db: image: mongo:4.0.6 volumes: - ./docker/mongodb/data:/data/db volumes: db-volume:
解決法
キャッシュで bundle install
が実行されない場合があるっぽい!
docker-compose build --no-cache
を実行する
https://www.udemy.com/rails-kj/learn/v4/questions/6375408
$ docker-compose build --no-cache db uses an image, skipping Building web Step 1/9 : FROM ruby:2.5.1 ---> 3c8181e703d2 Step 2/9 : RUN apt-get update -qq && apt-get install -y build-essential nodejs ---> Running in e9fe91dea4b5 Reading package lists... Building dependency tree... Reading state information... The following additional packages will be installed: dpkg-dev fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libfakeroot libuv1 Suggested packages: debian-keyring The following NEW packages will be installed: build-essential dpkg-dev fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libfakeroot libuv1 nodejs 0 upgraded, 9 newly installed, 0 to remove and 49 not upgraded. Need to get 5331 kB of archives. After this operation, 17.0 MB of additional disk space will be used. Get:1 http://deb.debian.org/debian stretch/main amd64 dpkg-dev all 1.18.25 [1595 kB] Get:2 http://deb.debian.org/debian stretch/main amd64 build-essential amd64 12.3 [7346 B] Get:3 http://deb.debian.org/debian stretch/main amd64 libfakeroot amd64 1.21-3.1 [45.7 kB] Get:4 http://deb.debian.org/debian stretch/main amd64 fakeroot amd64 1.21-3.1 [85.6 kB] Get:5 http://deb.debian.org/debian stretch/main amd64 libalgorithm-diff-perl all 1.19.03-1 [48.7 kB] Get:6 http://deb.debian.org/debian stretch/main amd64 libalgorithm-diff-xs-perl amd64 0.04-4+b2 [11.6 kB] Get:7 http://deb.debian.org/debian stretch/main amd64 libalgorithm-merge-perl all 0.08-3 [12.7 kB] Get:8 http://deb.debian.org/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB] Get:9 http://deb.debian.org/debian stretch/main amd64 nodejs amd64 4.8.2~dfsg-1 [3440 kB] debconf: delaying package configuration, since apt-utils is not installed Fetched 5331 kB in 3s (1774 kB/s) Selecting previously unselected package dpkg-dev. (Reading database ... 29376 files and directories currently installed.) Preparing to unpack .../0-dpkg-dev_1.18.25_all.deb ... Unpacking dpkg-dev (1.18.25) ... Selecting previously unselected package build-essential. Preparing to unpack .../1-build-essential_12.3_amd64.deb ... Unpacking build-essential (12.3) ... Selecting previously unselected package libfakeroot:amd64. Preparing to unpack .../2-libfakeroot_1.21-3.1_amd64.deb ... Unpacking libfakeroot:amd64 (1.21-3.1) ... Selecting previously unselected package fakeroot. Preparing to unpack .../3-fakeroot_1.21-3.1_amd64.deb ... Unpacking fakeroot (1.21-3.1) ... Selecting previously unselected package libalgorithm-diff-perl. Preparing to unpack .../4-libalgorithm-diff-perl_1.19.03-1_all.deb ... Unpacking libalgorithm-diff-perl (1.19.03-1) ... Selecting previously unselected package libalgorithm-diff-xs-perl. Preparing to unpack .../5-libalgorithm-diff-xs-perl_0.04-4+b2_amd64.deb ... Unpacking libalgorithm-diff-xs-perl (0.04-4+b2) ... Selecting previously unselected package libalgorithm-merge-perl. Preparing to unpack .../6-libalgorithm-merge-perl_0.08-3_all.deb ... Unpacking libalgorithm-merge-perl (0.08-3) ... Selecting previously unselected package libuv1:amd64. Preparing to unpack .../7-libuv1_1.9.1-3_amd64.deb ... Unpacking libuv1:amd64 (1.9.1-3) ... Selecting previously unselected package nodejs. Preparing to unpack .../8-nodejs_4.8.2~dfsg-1_amd64.deb ... Unpacking nodejs (4.8.2~dfsg-1) ... Setting up libuv1:amd64 (1.9.1-3) ... Setting up dpkg-dev (1.18.25) ... Processing triggers for libc-bin (2.24-11+deb9u3) ... Setting up libfakeroot:amd64 (1.21-3.1) ... Setting up libalgorithm-diff-perl (1.19.03-1) ... Setting up nodejs (4.8.2~dfsg-1) ... update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode Setting up build-essential (12.3) ... Setting up fakeroot (1.21-3.1) ... update-alternatives: using /usr/bin/fakeroot-sysv to provide /usr/bin/fakeroot (fakeroot) in auto mode Setting up libalgorithm-merge-perl (0.08-3) ... Setting up libalgorithm-diff-xs-perl (0.04-4+b2) ... Processing triggers for libc-bin (2.24-11+deb9u3) ... Removing intermediate container e9fe91dea4b5 ---> c3b444433c3f Step 3/9 : RUN mkdir /app ---> Running in d9a2a7303e84 Removing intermediate container d9a2a7303e84 ---> 56a13202e842 Step 4/9 : WORKDIR /app ---> Running in bbd450d4f4b4 Removing intermediate container bbd450d4f4b4 ---> 533f815a4d7f Step 5/9 : COPY Gemfile /app/Gemfile ---> 794f51b7f83d Step 6/9 : COPY Gemfile.lock /app/Gemfile.lock ---> b718ae62f2c7 Step 7/9 : RUN gem install bundler ---> Running in 3ea22bf774b7 Successfully installed bundler-2.0.1 1 gem installed Removing intermediate container 3ea22bf774b7 ---> 5f0ce919f381 Step 8/9 : RUN bundle install ---> Running in e854bd94d444 Warning: the running version of Bundler (1.16.6) is older than the version that created the lockfile (1.17.3). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`. The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`. Fetching gem metadata from https://rubygems.org/............ Fetching rake 12.3.2 Installing rake 12.3.2 Fetching concurrent-ruby 1.1.5 Installing concurrent-ruby 1.1.5 Fetching i18n 1.6.0 Installing i18n 1.6.0 Fetching minitest 5.11.3 Installing minitest 5.11.3 Fetching thread_safe 0.3.6 Installing thread_safe 0.3.6 Fetching tzinfo 1.2.5 Installing tzinfo 1.2.5 Fetching activesupport 5.2.2.1 Installing activesupport 5.2.2.1 Fetching builder 3.2.3 Installing builder 3.2.3 Fetching erubi 1.8.0 Installing erubi 1.8.0 Fetching mini_portile2 2.4.0 Installing mini_portile2 2.4.0 Fetching nokogiri 1.10.1 Installing nokogiri 1.10.1 with native extensions Fetching rails-dom-testing 2.0.3 Installing rails-dom-testing 2.0.3 Fetching crass 1.0.4 Installing crass 1.0.4 Fetching loofah 2.2.3 Installing loofah 2.2.3 Fetching rails-html-sanitizer 1.0.4 Installing rails-html-sanitizer 1.0.4 Fetching actionview 5.2.2.1 Installing actionview 5.2.2.1 Fetching rack 2.0.6 Installing rack 2.0.6 Fetching rack-test 1.1.0 Installing rack-test 1.1.0 Fetching actionpack 5.2.2.1 Installing actionpack 5.2.2.1 Fetching nio4r 2.3.1 Installing nio4r 2.3.1 with native extensions Fetching websocket-extensions 0.1.3 Installing websocket-extensions 0.1.3 Fetching websocket-driver 0.7.0 Installing websocket-driver 0.7.0 with native extensions Fetching actioncable 5.2.2.1 Installing actioncable 5.2.2.1 Fetching globalid 0.4.2 Installing globalid 0.4.2 Fetching activejob 5.2.2.1 Installing activejob 5.2.2.1 Fetching mini_mime 1.0.1 Installing mini_mime 1.0.1 Fetching mail 2.7.1 Installing mail 2.7.1 Fetching actionmailer 5.2.2.1 Installing actionmailer 5.2.2.1 Fetching activemodel 5.2.2.1 Installing activemodel 5.2.2.1 Fetching arel 9.0.0 Installing arel 9.0.0 Fetching activerecord 5.2.2.1 Installing activerecord 5.2.2.1 Fetching mimemagic 0.3.3 Installing mimemagic 0.3.3 Fetching marcel 0.3.3 Installing marcel 0.3.3 Fetching activestorage 5.2.2.1 Installing activestorage 5.2.2.1 Fetching public_suffix 3.0.3 Installing public_suffix 3.0.3 Fetching addressable 2.6.0 Installing addressable 2.6.0 Fetching io-like 0.3.0 Installing io-like 0.3.0 Fetching archive-zip 0.12.0 Installing archive-zip 0.12.0 Fetching bindex 0.5.0 Installing bindex 0.5.0 with native extensions Fetching msgpack 1.2.9 Installing msgpack 1.2.9 with native extensions Fetching bootsnap 1.4.1 Installing bootsnap 1.4.1 with native extensions Fetching bson 4.4.2 Installing bson 4.4.2 with native extensions Using bundler 1.16.6 Fetching byebug 11.0.1 Installing byebug 11.0.1 with native extensions Fetching regexp_parser 1.3.0 Installing regexp_parser 1.3.0 Fetching xpath 3.2.0 Installing xpath 3.2.0 Fetching capybara 3.15.0 Installing capybara 3.15.0 Fetching ffi 1.10.0 Installing ffi 1.10.0 with native extensions Fetching childprocess 0.9.0 Installing childprocess 0.9.0 Fetching chromedriver-helper 2.1.0 Installing chromedriver-helper 2.1.0 Fetching coffee-script-source 1.12.2 Installing coffee-script-source 1.12.2 Fetching execjs 2.7.0 Installing execjs 2.7.0 Fetching coffee-script 2.4.1 Installing coffee-script 2.4.1 Fetching method_source 0.9.2 Installing method_source 0.9.2 Fetching thor 0.20.3 Installing thor 0.20.3 Fetching railties 5.2.2.1 Installing railties 5.2.2.1 Fetching coffee-rails 4.2.2 Installing coffee-rails 4.2.2 Fetching multi_json 1.13.1 Installing multi_json 1.13.1 Fetching jbuilder 2.8.0 Installing jbuilder 2.8.0 Fetching rb-fsevent 0.10.3 Installing rb-fsevent 0.10.3 Fetching rb-inotify 0.10.0 Installing rb-inotify 0.10.0 Fetching ruby_dep 1.5.0 Installing ruby_dep 1.5.0 Fetching listen 3.1.5 Installing listen 3.1.5 Fetching mongo 2.7.1 Installing mongo 2.7.1 Fetching mongoid 7.0.2 Installing mongoid 7.0.2 Fetching puma 3.12.1 Installing puma 3.12.1 with native extensions Fetching sprockets 3.7.2 Installing sprockets 3.7.2 Fetching sprockets-rails 3.2.1 Installing sprockets-rails 3.2.1 Fetching rails 5.2.2.1 Installing rails 5.2.2.1 Fetching rubyzip 1.2.2 Installing rubyzip 1.2.2 Fetching sass-listen 4.0.0 Installing sass-listen 4.0.0 Fetching sass 3.7.3 Installing sass 3.7.3 Fetching tilt 2.0.9 Installing tilt 2.0.9 Fetching sass-rails 5.0.7 Installing sass-rails 5.0.7 Fetching selenium-webdriver 3.141.0 Installing selenium-webdriver 3.141.0 Fetching spring 2.0.2 Installing spring 2.0.2 Fetching spring-watcher-listen 2.0.1 Installing spring-watcher-listen 2.0.1 Fetching turbolinks-source 5.2.0 Installing turbolinks-source 5.2.0 Fetching turbolinks 5.2.0 Installing turbolinks 5.2.0 Fetching uglifier 4.1.20 Installing uglifier 4.1.20 Fetching web-console 3.7.0 Installing web-console 3.7.0 Bundle complete! 18 Gemfile dependencies, 81 gems now installed. Bundled gems are installed into `/usr/local/bundle` Post-install message from i18n: HEADS UP! i18n 1.1 changed fallbacks to exclude default locale. But that may break your application. Please check your Rails app for 'config.i18n.fallbacks = true'. If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be 'config.i18n.fallbacks = [I18n.default_locale]'. If not, fallbacks will be broken in your app by I18n 1.1.x. For more info see: https://github.com/svenfuchs/i18n/releases/tag/v1.1.0 Post-install message from sass: Ruby Sass is deprecated and will be unmaintained as of 26 March 2019. * If you use Sass as a command-line tool, we recommend using Dart Sass, the new primary implementation: https://sass-lang.com/install * If you use Sass as a plug-in for a Ruby web framework, we recommend using the sassc gem: https://github.com/sass/sassc-ruby#readme * For more details, please refer to the Sass blog: http://sass.logdown.com/posts/7081811 Removing intermediate container e854bd94d444 ---> 6c37b33e09a5 Step 9/9 : COPY . /app ---> cf7322d43f1f Successfully built cf7322d43f1f Successfully tagged mongoid_sample_web:latest
結果
無事に起動しました!
Docker上でRailsがMongoDBで動いていることも確認!
気付いたこと
今回のエラーは最初に docker-compose build
をせず、直接 docker-compose run web bundle install
を実行した状態でサーバーを起動しようとしたところ起こりました...
そもそも docker-compose.yml
に bundle install
コマンドを書いていたので、最初から docker-compose build
を一発実行すれば変なキャッシュが残ってインストール出来ない問題もなかったのでは?そんな風に思います。
他の解決法があるのか詳しくは分からないですが、こうしたキャッシュ絡みのインストールトラブルの際は docker-compose build --no-cache
でイケそうということがわかったのでひとまずよかった。