mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Mysql within DIND gets Connection Refused, but works on my Mac

Open trimbleAdam opened this issue 4 years ago • 4 comments

Hi there,

Been googling around all morning trying to find out what the heck is going on when I kick off mysql docker container within a k8s pod (DIND).

Locally this works: docker run --rm -d --name=mysql
-e MYSQL_DATABASE=test-db
-e MYSQL_USER=test-user
-e MYSQL_PASSWORD=*********
-e MYSQL_ROOT_PASSWORD=*********
-p 6603:3306 mysql mysql -u test-user -p***** -P 6603 -e 'show tables;'

...but from the console of the pod (via kubectl exec, etc) the same mysql command invariably returns ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:6603' (111).

Is there something particular to Ubuntu 20.04 (the OS of the DIND pod), or just DIND with regard to the dockerized mysql that I am missing?

Any assistance is greatly appreciated

trimbleAdam avatar Mar 03 '22 19:03 trimbleAdam

Can't connect to MySQL server on '127.0.0.1:6603

Without changing the MySQL configuration, MySQL is listening on 3306, not 6603. The reason that this works from your host to a container locally is the proxying that docker is doing with -p 6603:3306. That proxies the host 6603 to the container 3306. But if you are connecting from inside the container (localhost via docker exec or kubectl exec) or from another container it is only available at 3306.

yosifkit avatar Mar 03 '22 21:03 yosifkit

Thanks for the response!

To be clear, the mysql container is being launched inside the DIND-enabled CI/CD agent container, and then a python script expecting to make the connection is getting the Connection Refused when trying to connect to that running container over 6603. (I have also tried 3306, as you mentioned, with the same outcome)

The pipeline kicks off the docker container with docker run (including the -p 6603:3306 proxying), then performs some potentially destructive things using the throwaway container.

This is simple diagram of what I'm trying to do. DIND

An interesting thing to note is that from within the build agent pod netstat doesn't show anything listening on 6603, where I do see it in netstat locally (on my Mac).

Mac:

netstat -an|grep 6603

tcp4       0      0  127.0.0.1.6603         127.0.0.1.56955        ESTABLISHED

Pod:

netstat -an|grep 6603
# returns nothing

Thanks again.

trimbleAdam avatar Mar 03 '22 22:03 trimbleAdam

How long is the python script waiting for the mysql container? On first start the container will take a little bit since it has to initialize the data directory and setup the initial user/database before it then starts listening with network available connectivity. There is nothing else special about the mysql container that would prevent it from running inside Docker-in-Docker.

Can you share the relevant parts of the kubernetes yaml files to give us a better idea of how it is all running. Without knowing the setup we can only guess where the networking could be behaving unexpectedly.

Perhaps the dind container is in its own network namespace?


(for kubernetes specific troubles, you might have better luck on a forum or site dedicated to such. The official-images issues are meant for finding issues with the containerization of the images and we don't usually have the bandwidth to debug problems arising from specific environments).

yosifkit avatar Mar 03 '22 23:03 yosifkit

The script waits until it sees [Server] /usr/sbin/mysqld: ready for connections in the logs before moving on to the next stage of the pipeline. Even hours after the container is running, the problem persists for connection via python/command line/etc.

I am betting that the docker network within the k8s pod is set up in such a way that it is preventing, as the exact same process is being run on native Docker on Mac, and native Docker on Ubuntu (docker.sock mounted as volume, etc).

Thanks for your assistance @yosifkit.

trimbleAdam avatar Mar 04 '22 15:03 trimbleAdam