Can't connect dockerized python app to my local mysql

So i have Dockerized a python app that writes data to mysql database, but it seems that everytime i start the app inside the container with this command:

python main.py -d database -l user -p password

get (2002, “Can’t connect to local MySQL server through socket ‘/run/mysqld/mysqld.sock’ (111)”)
i build the container with:
docker build -t name_of_image:tag .

and run it with network host so it uses the same network as the localhost:

docker run -td --network="host" name_of_imae:tag

I tried multiple approach: executing it into micro-services with docker-compose and connecting my app with mysql/mariadb image, and same network. doesn’t work.
really frustrating…
i share with you my requirments.txt and my Dockerfile:

FROM python:3.8

WORKDIR /usr/src/app/

# Installing Python and all the dependencies

COPY requirements.txt ./

RUN pip3 install --no-cache-dir -r requirements.txt

COPY . .
# Dependencies for Copilot XMPP

twisted==21.7.0

defer==1.0.4

redis

lxml==4.7.1

spyne==2.13.16

PyMySQL==1.0.2

mysql-connector==2.2.9

mysqlclient==2.1.0

requests==2.27.1

#''' Twisted TLS Extra '''

pyOpenSSL==21.0.0

service-identity==21.1.0

idna==2.8

It uses multple service but it doesn’t concern me as much as the mysql writing data hope somone can help.

Good evening,
I guess your MySQL-database is not in the same image/container as your Python-app (so it is in another container or on the docker-host or even on another host).
But it seems that your Python-app wants to connect to localhost through a socket. This is the default for the mysql-command on the linux-shell, too.
So check if there is a setting within your Python-app where you can change the MySQL-server’s hostname/ip-address. Use the MySQL-server’s container-name as DNS-name or (if you have forwarded the MySQL-port to the docker-host) use 172.17.0.1 (the docker-host’s ip-address on Docker running on Linux) or host.docker.internal (if you are running Docker on Windows).

1 Like

Good morning, Mathias,
Thanks for your answer i made the change as you said and it worked perfectly.
I have made:
Host=<container_ip>
In the python-app.
For anyone who’s have problem on that:
i’am working with Python:3.8 and Debian 11.