Docker Community Forums

Share and learn in the Docker community.

Help establishing connection between mysql db container and ubuntu/flask

Hi,

I am trying to run a docker-compose to get multiple containers talking to each other. I am a complete newbie and read that using compose is the correct route to take. I have a mysql database, a flask app and gunicorn. Eventually I want to push it to heroku. They require the gunicorn. One of the packages is not a standard pip package, so I had to use conda on the ubuntu image. That is why I didn’t go straight to a python image. I don’t know how to establish connection between the flask app and mysql server. Everything builds fine when I run docker-compose up; when I enter the bash shell of the ubuntu image, I try running python run.py or gunicorn, I am getting an error:

2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name or service not known

Here is my .yml file:

version: '3.7'
services:
  app:
    build: . 
    links:
      - db
    ports:
      - "5000:5000"
  db:
    image: mysql:latest
    restart: on-failure:3 
    environment:
      MYSQL_DATABASE: 'db'
      MYSQL_USER: ${MYSQL_USER} 
      MYSQL_PASSWORD: ${MYSQL_PASSWORD} 
      MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} 
    ports:
      - "3306"
    volumes:
      - /chemITry/app/protected/chemitrycmpds.sql:/docker-entrypoint-initdb.d/chemitrycmpds.sql 
    entrypoint:
      - /chemITry/startup.sh

startup.sh:

#!/bin/bash
gunicorn --bind 0.0.0.0:5000 run:app 

I read some posts that made me think that the docker-entrypoint-initdb.d may be the issue? I don’t have enough knowledge about this aspect of the docker-compose; not sure what I need to do. Would greatly appreciate some help. I’ve been trying to figure this out for more than a week. Thank you.