Reputation: 107
I'm new to the world of DevOps, I'm running a simple rails app on docker-compose. 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.
Below is my 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
Below is my docker-compose.yaml file.
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'
Upvotes: 2
Views: 1291
Reputation: 148
Firstly, you're referencing database.yml file to config.yml. You don't need to create custom config file instead you can use 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. 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:
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:
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. 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.
Upvotes: 2