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?