JVG
JVG

Reputation: 21150

Docker MySQL: Connecting a Rails app to MySQL

I have a rails app that I'm running in Docker. I want to run MySQL in a separate container and connect the two.

My docker-compose.yml file looks like this:

# docker-compose.yml
db:
  image: "mysql:5.6"
  ports: 
    - 3306
  environment:
    MYSQL_ROOT_PASSWORD: pass
    MYSQL_DATABASE: dbname
    MYSQL_USER: user
    MYSQL_PASSWORD: pass
web:
  build: .
  ports:
    - "80:80"
  env_file:
    - .env.development
  links:
    - db
  volumes:
    - "/webapp:/home/app/webapp"

When I run docker-machine ip default I get 192.168.99.100.

When I run docker ps I see mysql is running on PORT: 3306/tcp, 0.0.0.0:32782->32781/tcp Edit: After removing the mysql container and restarting it, the port is actually 0.0.0.0:32784->3306/tcp

I'm using the Sequel gem, and using the following params to connect to my db:

 Sequel::Model.db = Sequel.connect(adapter:  'mysql2',
                              database: 'dbname',
                              user:     'user',
                              password: 'pass',
                              host:     '192.168.99.100',
                              port:     '3306',
                              loggers: [logger] )

When I run my app, I get:

rake aborted!
Sequel::DatabaseConnectionError: Mysql2::Error: Can't connect to MySQL server on '192.168.99.100' (111)
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `new'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `connect'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/connection_pool.rb:101:in `make_new'
// Lots more traces

Any ideas what I'm doing wrong here?

Upvotes: 1

Views: 1077

Answers (1)

Magnus Skog
Magnus Skog

Reputation: 146

Try using the link name as "host" in your connection settings. Change the IP address to "db".

Upvotes: 3

Related Questions