4

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

1
  • what is more when i try to access from my local machine ( rails s -p 9999 -> localhost:9999 ) everything is ok Commented Oct 6, 2016 at 23:46

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. You saved my day. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.