i can not connect my flask app with mysql /sqlalchemy via docker-compose

Viewed 16

i can not connect my flask app with mysql /sqlalchemy via docker-compose My docker-compose is as follows:

version: "3.7"
services:
  myapp-backend:
    build: ./myapp-backend
    container_name: myapp-backend
    depends_on:
      - mysql
    links:
      - mysql
    environment:
      DB_HOST: mysql
      DB_NAME: survey
      DB_USER: root
      DB_PASSWORD: 'blabla'
    volumes:
      - ./db:/app/db
    restart: always
    expose:
      - 5000
  mysql:
    image: mysql:8.0.22
    container_name: mysql
    restart: always
    environment:
      MYSQL_DATABASE: 'survey'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'blabla'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'blabla'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'
      # Where our data will be persisted
    volumes:
      - ./data/db:/var/lib/mysql

my config.py file looks like this:

import os

class BaseConfig(object):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'mysql://{}:{}@{}/{}'.format(
        os.getenv('DB_USER', 'root'),
        os.getenv('DB_PASSWORD', 'blabla'),
        os.getenv('DB_HOST', 'mysql'),
        os.getenv('DB_NAME', 'survey')
    )

and the flask requirements file:

Flask
Flask_cors==3.0.9
PyJWT==1.7.1
pysqlite3
flask_sqlalchemy
flask_script
flask_migrate==2.5.3
SQLAlchemy==1.3.19
Flask-Mail
uWSGI
human-id
mysqlclient==2.1.1

i can access mysql container (with docker exec), get connected to mysql and see the db. But when i execute docker-compose, i get the message :

sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (2005, "Unknown MySQL server host 'mysql' (-3)")

It is really strange because i refer to mysql by its container name (mysql). Why flask can not see correctly the mysql container?

Hope i gave you all the necessar info!

Thank you very much for your help!

0 Answers
Related