0

I am trying to connect to mysql db using a python program. When run locally it works.

But while dockerizing the application I created, one container is for the python code and the other for the mysql db, when ran i this manner it fails to connect.

Python_code:

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

docker-compose:

version: "3"
  services:
    app:
      image: app:latest
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And the docker-compose up fails with the eroor:

pony.orm.dbapiprovider.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")

Where am I going wrong? Please advise!

3 Answers 3

1

I would recommend you to exit your application in case it cannot connect to MySQL and set the restart policy to always, because depends_on does not guarantee that MySQL will be totally up when app starts but it is good to have it there.

version: "3"
  services:
    app:
      image: app:latest
      restart: always
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db
      depends_on:
        - db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And your application code should be something like:

try:
    db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)
except:
    # write some logs
    exit(1)
Sign up to request clarification or add additional context in comments.

4 Comments

Can you tell me exactly what happens when we use depends on?
depends_on does not wait --in your case-- for db to be ready, it only makes sure that service db is started before app but it does not guarantee that db will be ready. Check this docs.docker.com/compose/compose-file and check the note that says: > "There are several things to be aware of when using depends_on"
An example would be that container for db starts, then app will start but db startup process might take some time e.g. 3 seconds, so app will try to connect to something that is still starting
If you run docker-compose up app, it will also start db, but that’s about all it does. It will start starting db before it starts starting app but as others have noted the MySQL startup can take a while.
1

Try using container port 3306 -

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

Also, add depends_on attribute, you can remove the links attribute -

 depends_on:
    - db

3 Comments

still the same error. I have edited the question - according to this answer,
Try adding depends_on as well & then run the compose again.
even that didn't help :(
1

YOu can use depends_on flag as mentioned in accepted answer. If it does not solve your problem them use this approach.

After starting the container, your server will try to connect to the database server. sometimes database server may take some time to boot up and in this window, if the server tries to connect to database server it will face problems. Try adding logic to reconnect database server after few seconds if the connection fails.

try{
   connectToDatabase();
   }catch(error){
   waitForHalfMinute();
   connectToDatabase();
}

4 Comments

Is this the same logic we are using while doing depends_on in the compose file? @vivekyadv4?
depend_on works partially, once you docker instance of database started, server will start. it will not check whether the port is open or not i.e. database is actually up or not. If your database instance takes time to boot up only depend_on flag will not work
Do we have any way to handle this in the compose file?
Not sure about this but probably no, cause I faced this problem and digged a lot through docker documentation for it

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.