无法找到gem mysql2,即使它在Gemfile中并且bundle install在Dockerfile中

时间:2016-06-23 11:56:48

标签: ruby-on-rails docker docker-compose

我试图在容器(Ruby图像)中创建和运行Rails应用程序。我使用docker-compose.exe run -d web rails new . --force --database=mysql --skip-bundle创建应用程序脚手架,并尝试使用docker-compose.exe up -d运行。

这是我的docker-compose.ymlrails.dockerfile

version: '2'

services:
  web:
    build: 
      context: .
      dockerfile: .docker/rails.dockerfile
    command: rails server -p 3000 -b '0.0.0.0'
    volumes: 
      - .:/var/www
    ports: 
      - "3000:3000"
    depends_on:
      - 'mysql'
    networks:
      - ddoc-network


  mysql:
    image: mysql
    environment: 
      MYSQL_ROOT_PASSWORD: 'SOMETHING'
    networks:
      - ddoc-network


networks:
  ddoc-network:
    driver: bridge

rails.dockerfile

FROM ruby:2.3.1

MAINTAINER Juliano Nunes

RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
RUN mkdir /var/www
WORKDIR /var/www

ADD Gemfile /var/www/Gemfile
ADD Gemfile.lock /var/www/Gemfile.lock
RUN bundle install
ADD . /var/www

CMD ['rails', 'server', '-b', '0.0.0.0']

然而,当我运行docker-compose up -d并检查日志时,我明白了:

[36mweb_1    |[0m /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/resolver.rb:356:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'mysql2 (< 0.5, >= 0.3.13)' in any of the gem sources listed in your Gemfile or available on this machine. (Bundler::GemNotFound)
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/resolver.rb:331:in `each'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/resolver.rb:331:in `verify_gemfile_dependencies_are_found!'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/resolver.rb:200:in `start'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/resolver.rb:184:in `resolve'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/definition.rb:200:in `resolve'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/definition.rb:140:in `specs'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/definition.rb:185:in `specs_for'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/definition.rb:174:in `requested_specs'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/environment.rb:19:in `requested_specs'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/runtime.rb:14:in `setup'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler.rb:95:in `setup'
[36mweb_1    |[0m     from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.4/lib/bundler/setup.rb:19:in `<top (required)>'
[36mweb_1    |[0m     from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `require'
[36mweb_1    |[0m     from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `rescue in require'
[36mweb_1    |[0m     from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
[36mweb_1    |[0m     from /usr/local/bundle/bin/rails:15:in `<main>'

为什么?

更新

这似乎是相同或类似的问题:https://github.com/docker/compose/issues/2103

我是从Windows主机运行的,而且我的主机上没有任何Ruby on Rails开发工具,因此在运行容器之前我无法在本地卷上运行bundler。

1 个答案:

答案 0 :(得分:0)

我必须做两件事来解决这个问题。

首先,运行docker-compose build(我不知道我需要在docker-compose.ymldockerfile上进行一些更改,因此可以执行bundle install我还必须配置http代理,因为我在公司代理后面。