Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysql2::Error: Can't connect to MySQL server on 'db' (111) #4125

Closed
kyledecot opened this issue Nov 7, 2016 · 2 comments
Closed

Mysql2::Error: Can't connect to MySQL server on 'db' (111) #4125

kyledecot opened this issue Nov 7, 2016 · 2 comments

Comments

@kyledecot
Copy link

kyledecot commented Nov 7, 2016

Hi All,

I'm not sure if this is the right repo to post this to but I figured I'd start here and perhaps someone can at least point me in the right direction. I have a docker-compose.test.yml file which creates two services (web and db). Here is said file:

version: '2'
services:
  db:
    container_name: "rfc_test_db"
    image: mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
  web:
    container_name: "rfc_test_web"
    stdin_open: true
    tty: true
    environment:
      - RAILS_ENV=test
      - DATABASE_HOST=db
      - DATABASE=mysql
      - DATABASE_USERNAME=root
      - DATABASE_PASSWORD=
      - SECRET_KEY_BASE=3ad576ebf101d796769739f909b4a84d01a9f99852765d51008ec7d
    build: .
    volumes:
      - .:/rfc
    depends_on:
      - db

The problem that I'm running into is when I perform an up and then a run that attempts to connect to the db service I get the following error:

Mysql2::Error: Can't connect to MySQL server on 'db' (111)

What's odd is that if I run the up and then wait ~30 seconds and then run everything works as expected. It appears that my networking isn't getting linked up or something. Has anyone else run into this problem?

docker-compose version: docker-compose version 1.9.0-rc2, build a2da43b
docker version: Docker version 1.12.3, build 6b644ec, experimental

Doesn't Work

docker-compose -f docker-compose.test.yml -p rfc_test up --build --remove-orphans -d
# immediately after this completes call 
docker-compose -f docker-compose.test.yml -p rfc_test run --rm web rake db:schema:load
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
-- create_table("attachments", {:force=>:cascade})
rake aborted!
Mysql2::Error: Can't connect to MySQL server on 'db' (111)
...

Works

docker-compose -f docker-compose.test.yml -p rfc_test up --build --remove-orphans -d
# wait ~30 seconds
docker-compose -f docker-compose.test.yml -p rfc_test run --rm web rake db:schema:load
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
-- create_table("attachments", {:force=>:cascade})
   -> 0.1204s
...
@shin-
Copy link

shin- commented Nov 7, 2016

MySQL needs time to start up, and there's a latency until it starts answering on the allocated port. This has nothing to do with docker - you need to implement some sort of retry mechanism into your workflow to account for it.

HTH !

@wilsonsilva
Copy link

According to docker compose docs:

You can use a tool such as wait-for-it, dockerize or sh-compatible wait-for. These are small wrapper scripts which you can include in your application’s image and will poll a given host and port until it’s accepting TCP connections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants