Docker & MySQL

Iā€™m new to streamlit and docker, so my question may be dumbā€¦
Iā€™ve used mysql.connector to connect to my local mysql database and I tried to make a docker image of my app.
Iā€™ve followed the docs to make the docker image and the app can be opened, but here comes the error ā€œDatabaseError: 2003 (HY000): Canā€™t connect to MySQL server on ā€˜localhost:3306ā€™ (99)ā€
The code works fine if I use the standard ā€˜streamlit run app.pyā€™ on the IDE

def authenticate_user(username, password):
    db_connection = mysql.connector.connect(
        host='localhost',
        user= xxxxxxx,
        password= xxxxxxx,
        database="my_db"
    )
    cursor = db_connection.cursor()


    query = f"SELECT * FROM login_info WHERE login_id = '{username}' AND login_password = '{password}'"
    cursor.execute(query)
    result = cursor.fetchone()

    if result:
        user_data = {
            "name": result[1],
            "department": result[2],
            "id": result[4],
            "permission": bool(result[5]),
        }
        is_superior_query = f"SELECT name1 FROM relationship WHERE name1 = '{result[1]}' AND relationship='up'"
        cursor.execute(is_superior_query)
        superior_employee = cursor.fetchone()

        if superior_employee:
            user_data['isLeader'] = True
        else:
            user_data['isLeader'] = False
        cursor.close()
        db_connection.close()
        return user_data
    else:
        cursor.close()
        db_connection.close()
        return None

What do I need to do to make sure the mysql db can be correctly reached? And Iā€™d love to know the answer for both the local db and the non-local ones bc eventually Iā€™ll switch to the company db and company network and thatā€™s why I need a docker to deploy the app rather than use the streamlit cloud services

Thanks in advance

If you run the streamlit app in a docker container, localhost is this container itself (not localhost of the host computer), so this wonā€™t work. You have to use the hostname of the database container instead.
But we have to see your Dockerfile and docker-compose.yml file to give further advice.

# app/Dockerfile

FROM python:3.11

WORKDIR /Performance

RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    software-properties-common \
    git \
    && rm -rf /var/lib/apt/lists/*

COPY . /Performance

RUN pip3 install -r requirements.txt

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

I have my Dockerfile here and I think I sticked to the doc pretty well. I guess the problem is indeed container difference, so do I just need to adjust my secret.toml and change host into the new database, maybe sth like ā€˜198.1.2.3ā€™, to reach the localhost of host computer, or is there more to be done?

The more important question would be: Where does the database run? In another container? Native on the host computer? Somewhere else?