Using docker-compose Mysql + App

I am trying to bundle two containers using dockers.

MySQL Dockerfile:

...
EXPOSE 3306
CMD ["/usr/sbin/mysqld"]

Dockerfile application file:

...
ADD . /services
CMD ["python", "-u", "services/run_tests.py"]

In run_tests.py I used

self.db = MySQLdb.connect(host="mysql", user="XYZ", passwd="XYZ", db="TEST_DB")

In my docker-compose.yml:

app:
   build: .
   links:
      - mysql
mysql:
   image: XYZ/KJM

When I started docker-compose up, I was unable to connect to the mysql container.

OperationalError: (2003, "Cannot connect to MySQL server on" rds "(111)")

EDIT: I don't know if I need to wait a bit to launch the docking application. I assume that MySQL does not support an application that is trying to connect.

+4
source share
3 answers

.

, MySql , , , , MySql, . , , , . -

while(numberOfRetries > 0) {

try {

  // Try to connect to MySql
} catch(Exception e) {

     numberOfRetries--;

     if(numberOfRetries == 0)
       // log the exception 
}

, .

+2

, , . .

0

Docker 1.13, . HEALTHCHECK MySQL Docker:

  • script, exit 1, : select 1+1 from <table> - . health.sh.

  • Docker health.sh .

  • HEALTHCHECK CMD health.sh script.
  • In your build file, change the definition app:

    version: 2.1
    
    app:
      build:.
      links:
        - mysql
      depends_on:
        mysql:
          condition: service_healthy
    
0
source

Source: https://habr.com/ru/post/1613680/


All Articles