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’ on the IDE

def authenticate_user(username, password):
    db_connection = mysql.connector.connect(
        user= xxxxxxx,
        password= xxxxxxx,
    cursor = db_connection.cursor()

    query = f"SELECT * FROM login_info WHERE login_id = '{username}' AND login_password = '{password}'"
    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'"
        superior_employee = cursor.fetchone()

        if superior_employee:
            user_data['isLeader'] = True
            user_data['isLeader'] = False
        return user_data
        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


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

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

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 ‘’, 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?