在docker compose上运行时无法连接到Rails应用程序中的数据库

时间:2020-07-21 07:50:09

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

我是DevOps的新手,我正在docker-compose上运行一个简单的Rails应用。当我对config.yml应用程序中的数据库凭据进行硬编码时,工作正常,但是当我引用docker-compose.yaml中声明的ENV变量时,我的应用程序容器未与数据库建立连接。

下面是我的config.yaml

default: &default
  adapter: mysql2
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  database: <%= ENV['DB_DATABASE']%>
  host: <%= ENV['DB_HOST'] %>
  user: <%= ENV['DB_USERNAME'] %>
  password: <%= ['DB_PASSWORD'] %>
  timeout: 5000
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  host: <%= ENV['DB_HOST'] %>
  user: <%= ENV['DB_USERNAME'] %>
  password: <%= ['DB_PASSWORD'] %>
  socket: /var/run/mysqld/mysqld.sock
  database: <%= ENV['DB_DATABASE']%>


production:
  <<: *default
  database: <%= ENV['DB_DATABASE']%>
  host: <%= ENV['DB_HOST'] %>
  user: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  socket: /var/run/mysqld/mysqld.sock

下面是我的docker-compose.yaml文件。

version: '3'
services:
  webapp:
    build: .
    command: bash -c "bundle exec rails s -p 3001 -b '0.0.0.0'"
    ports:
      - '3001:3001'
    volumes:
      - '.:/data/checklist'
    depends_on:
      - db
    environment:
      DB_USERNAME: "root"
      DB_PASSWORD: "Mission2019"
      DB_DATABASE: "list"
      DB_PORT: 3306
      DB_HOST: db
      RAILS_ENV: production
      RAILS_MAX_THREADS: 5  
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: "list"
      MYSQL_ROOT_PASSWORD: "Mission2019"
      MYSQL_USERNAME: "root"
      MYSQL_PASSWORD: "Mission2019"
    ports:
      - '3307:3306'
    expose:
      - '3306'

我遇到错误

webapp_1  | ============= END WARNING FROM mysql2 =========
db_1      | 2020-07-21T07:44:59.886096Z 2 [Note] Access denied for user 'root'@'172.21.0.3' (using password: YES)
webapp_1  | I, [2020-07-21T07:44:59.887550 #1]  INFO -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] Completed 500 Internal Server Error in 40ms
webapp_1  | F, [2020-07-21T07:44:59.889463 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b]   
webapp_1  | F, [2020-07-21T07:44:59.889559 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] Mysql2::Error (Access denied for user 'root'@'172.21.0.3' (using password: YES)):
webapp_1  | F, [2020-07-21T07:44:59.889600 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b]   
webapp_1  | F, [2020-07-21T07:44:59.889658 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] mysql2 (0.4.10) lib/mysql2/client.rb:89:in `connect'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] mysql2 (0.4.10) lib/mysql2/client.rb:89:in `initialize'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
webapp_1  | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'

1 个答案:

答案 0 :(得分:0)

首先,您将database.yml文件引用为config.yml。您无需创建自定义配置文件,而可以使用database.yml。

错误消息说明可访问数据库,但密码不正确。这可能是由于config.yml(database.yml)在“密码:<%= ['DB_PASSWORD']%>”行中的拼写错误,但这应该是“密码:<%= ENV ['DB_PASSWORD']%>” ”作为默认的开发环境。请同时针对生产环境进行验证。

我对您的文件进行了修改,它适用于所有环境。文件如下。

更新database.yml文件:

default: &default
  adapter: mysql2
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  database: <%= ENV['DB_DATABASE']%>
  host: <%= ENV['DB_HOST'] %>
  port: <%= ENV["DB_PORT"] %>
  user: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  timeout: 5000
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default


production:
  <<: *default

更新的docker-compose.yml文件:

version: '3'
services:
  webapp:
    build: .
    command: bash -c "bundle exec rake db:migrate && bundle exec rails s -p 3001 -b '0.0.0.0'"
    ports:
      - '3001:3001'
    volumes:
      - '.:/data/checklist'
    depends_on:
      - db
    environment:
      DB_USERNAME: "root"
      DB_PASSWORD: "Mission2019"
      DB_DATABASE: "list"
      DB_PORT: 3306
      DB_HOST: db
      RAILS_ENV: production 
      RAILS_MAX_THREADS: 5  
    volumes:
      - .:/code
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: "list"
      MYSQL_ROOT_PASSWORD: "Mission2019"
      MYSQL_USERNAME: "root"
      MYSQL_PASSWORD: "Mission2019"

如果您不会从主机访问数据库,则不需要'docker-compose.yml'中的端口和暴露密钥。通常,端口的值应为“ 3306:3306”,除非您不会从主机的其他端口访问数据库。