[英]Not able to connect to the db in rails app when running on docker compose
I'm new to the world of DevOps, I'm running a simple rails app on docker-compose.我是 DevOps 领域的新手,我正在 docker-compose 上运行一个简单的 rails 应用程序。 When I hardcode the DB credentials in the config.yml app works fine, but when I refer to the ENV variables declared in the docker-compose.yaml my app container isn't establishing a connection with DB.当我在 config.yml 应用程序中硬编码 DB 凭据时工作正常,但是当我参考 docker-compose.yaml 中声明的 ENV 变量时,我的应用程序容器没有与 DB 建立连接。
Below is my config.yaml下面是我的配置。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
Below is my docker-compose.yaml file.下面是我的 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'
I get below error我得到以下错误
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'
Firstly, you're referencing database.yml file to config.yml.首先,您将 database.yml 文件引用到 config.yml。 You don't need to create custom config file instead you can use database.yml.您不需要创建自定义配置文件,而是可以使用 database.yml。
Error message explains that database is accessible but password is not correct.错误消息说明数据库可访问但密码不正确。 This may due to the typo mistake in config.yml (database.yml) at line "password: <%= ['DB_PASSWORD'] %>" but this should be "password: <%= ENV['DB_PASSWORD'] %>" for default, development environment.这可能是由于“密码:<%= ['DB_PASSWORD'] %>”行的 config.yml (database.yml) 中的拼写错误,但这应该是“密码:<%= ENV['DB_PASSWORD'] %> " 默认为开发环境。 Please verify the same for production environment as well.请对生产环境也进行相同的验证。
I took your files modified it and it works for me for all environments.我对你的文件进行了修改,它适用于所有环境。 The files are as follows below.文件如下。
The update database.yml file:更新 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
The updated docker-compose.yml file:更新后的 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"
The ports and expose key in 'docker-compose.yml' is not require if you will not be accessing the database from your host machine.如果您不从主机访问数据库,则不需要“docker-compose.yml”中的端口和公开密钥。 Normally the value of the port should be "3306:3306" except if you won't access the database from different port from your host machine.通常端口的值应该是“3306:3306”,除非您不会从主机的不同端口访问数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.