如何在rails应用程序中使用docker连接mysql?

时间:2015-11-05 10:13:43

标签: mysql ruby-on-rails docker

我创建了一个rails应用程序:testapp。

然后在其下添加了Dockerfiledocker-compose.yml个文件。

Dockerfile

FROM ruby:2.2.0
RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
RUN mkdir /testapp
WORKDIR /testapp
ADD Gemfile /testapp/Gemfile
ADD Gemfile.lock /testapp/Gemfile.lock
RUN bundle install
ADD . /testapp

搬运工-compose.yml

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: 1234
web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/testapp
  ports:
    - "3000:3000"
  links:
    - db

我的数据库设置文件:

配置/ database.yml的

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: 1234
  host: db
  port: 3306

development:
  <<: *default
  database: testapp_development

test:
  <<: *default
  database: testapp_test

production:
  <<: *default
  database: testapp_production

当我从docker机器启动容器时:docker-compose up,并从浏览器访问http://192.168.99.104:3000/,它给了我错误:

Mysql2::Error
Access denied for user 'root'@'172.17.0.7' (using password: YES)

控制台中的错误:

web_1 | /usr/local/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
web_1 |   error_class: Mysql2::Error
web_1 |   environment_name: development
web_1 |   cgi_data: {"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"192.168.99.1", "REMOTE_HOST"=>"192.168.99.1", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"http://192.168.99.104:3000/", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"192.168.99.104", "SERVER_PORT"=>"3000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)", "HTTP_HOST"=>"192.168.99.104:3000", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0", "HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "HTTP_ACCEPT_LANGUAGE"=>"ja,en-US;q=0.7,en;q=0.3", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_CONNECTION"=>"keep-alive", "HTTP_CACHE_CONTROL"=>"max-age=0", "rack.version"=>["1", "3"], "rack.multithread"=>"false", "rack.multiprocess"=>"false", "rack.run_once"=>"false", "rack.url_scheme"=>"http", "rack.hijack?"=>"true", "rack.hijack"=>"#<Proc:0x007fd6b4016c28@/usr/local/bundle/gems/rack-1.6.4/lib/rack/handler/webrick.rb:76 (lambda)>", "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/", "ORIGINAL_FULLPATH"=>"/", "ORIGINAL_SCRIPT_NAME"=>"", "action_dispatch.parameter_filter"=>["password"], "action_dispatch.redirect_filter"=>[], "action_dispatch.show_exceptions"=>"true", "action_dispatch.show_detailed_exceptions"=>"true", "action_dispatch.cookies_serializer"=>"json", "action_dispatch.cookies_digest"=>nil, "ROUTES_47215567452160_SCRIPT_NAME"=>"", "action_dispatch.request_id"=>"4048a5f6-974e-484a-9c16-fc4e1ca1c1dc", "action_dispatch.remote_ip"=>"192.168.99.1", "rack.request.query_string"=>"", "rack.request.query_hash"=>{}}
web_1 |   error_message: Mysql2::Error: Access denied for user 'root'@'172.17.0.7' (using password: YES)

如何设置config/database.yml文件?还有别的错吗?例如,mysql图像。

0 个答案:

没有答案