Hello i have huge problem. I'm trying to run rails and mysql on separate docker containers. in docker-compose.yml i have :
version: '2'
services:
db:
image: mysql
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: zzz
MYSQL_USER: root
MYSQL_PASSWORD: zzz
MYSQL_DATABASE: zzz
web:
build: .
command: bundle exec rails s -p 3000
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
and in my rails database.yml
development:
adapter: mysql2
encoding: utf8
database: zzz
pool: 5
username: root
password: zzz
host: database.dev //this is host for container with mysql
port: 3306
end when i run application i got error :
Access denied for user 'root'@'xxx' (using password: YES)
where xxx is ip my machine not container. Please help i dont know what to do
You have to link the containers, this should work:
version: '2'
services:
db:
image: mysql
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: zzz
MYSQL_USER: root
MYSQL_PASSWORD: zzz
MYSQL_DATABASE: zzz
web:
build: .
command: bundle exec rails s -p 3000
volumes:
- .:/app
ports:
- "3000:3000"
links:
- db:sql_srv
depends_on:
- db
Now the web
container is linked to db
and sql_srv
is an alias for the db
container. You should reach the db
container from the web
container using this address: sql_srv:3306
You don't actually need to specify the ports
in the db
container if you just want to connect your db
container to the web
container. The attribute ports
is only used for reaching the container from the host.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With