I face an issue while trying to make docker-compose contains Rails API and MySQL and others.
After docker-compose up and send a request to the Rails API I get this error
"#<Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'database' (111 \"Connection refused\")>"
My docker-compose
services:
database:
image: mysql:8.0.23
restart: always
volumes:
- 'db_data:/var/lib/mysql'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: root
MYSQL_PASSWORD: root
app:
build: .
restart: always
depends_on:
- 'database'
- 'redis'
ports:
- '3000:3000'
volumes:
- '.:/app'
- 'gem_cache:/usr/local/bundle/gems'
env_file: .env
environment:
RAILS_ENV: development
database.yml
default: &default
adapter: mysql2
encoding: utf8
host: <%= ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'] || '5432' %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: <%= ENV['DATABASE_NAME_DEV'] %>
...
.env
...
DATABASE_HOST=database
...
What's the problem here?