Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose rails with mysql

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

like image 848
Juri Bojka Avatar asked Mar 10 '23 23:03

Juri Bojka


1 Answers

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.

like image 104
Marcs Avatar answered Apr 06 '23 15:04

Marcs