2

I am using a docker container that contains a python script that should be able to interact with the local! Mysql server. Both are running on my Linux Server (debian 11).

When i start the container on the Linux Server I use

"docker run --add-host=host.docker.internal:host-gateway [chosen.container]"

in order to allow the script to use a gateway to connect to the mysql database on the host.

unfortunately this always produces the following error code

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host.docker.internal:3306' (111 Connection refused)

I'm sure that the Mysql database is configured to allow internal access, as I can connect to the database via the cli or a ssh tunnel over mysql workbench.

Does anyone know how to make the communication between the container and the database work?

The script that is running in the docker container is the following;

import mysql.connector

mydb = mysql.connector.connect(host="host.docker.internal", user="root", password="xxxxxxxxx", database='xxx-analysis-db', port=3306)

c = mydb.cursor()


query = "SELECT * FROM [chosen Table];"

c.execute(query)

result = c.fetchall()

print(result)

the dockerfile contains:

FROM python:3.10

ADD main.py .

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

CMD ["python","./main.py"]
  • better show exactly what values you use in `docker run` – furas Apr 28 '22 at 13:13
  • if you mean the run command; the run command is executed exactly as posted. At least as far as I know – PetersServer1 Apr 28 '22 at 13:53
  • do you use variables `host.docker.internal:host-gateway`? And what values you have assigned to these variables? You should check it. – furas Apr 28 '22 at 13:57
  • i do not use variables as there are no variables involved in this syntax to my understanding. Are you saying I should use variables? – PetersServer1 Apr 28 '22 at 14:00
  • normal execution is `docker run --add-host=domain.com:192.168.1.1 my_containter` and it seems `host.docker.internal` and `host-gateway` are specila variables to keep values. [What is linux equivalent of "host.docker.internal"](https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal). But now I'm wonder if you don't have to add some port redirections because `host.docker.internal:3306` can suggest it tries to connect to MySQL server inside docker, not outside docker. OR maybe your local database doesn't run - you should first if you can access it – furas Apr 28 '22 at 15:55

0 Answers0