{
"$type": "site.standard.document",
"description": "I was having the hardest time getting applications in docker containers to connect to the instance of mysql runnning on the host without using host networking mode in docker. So I figured I'd share my solution here for others.",
"path": "/blog/docker-localhost-mysql",
"publishedAt": "2019-12-08T00:00:00.000Z",
"site": "at://did:plc:nmwj6xne7mdpkry5pzvfendv/site.standard.publication/ndo-dev",
"tags": [
"servers",
"linux",
"mysql",
"docker"
],
"textContent": "I was having the hardest time getting applications in docker containers to connect to the instance of mysql runnning on the host without using host networking mode in docker. So I figured I'd share my solution here for others. The idea is to simply enable some local routing rules and use iptables to route traffic from Docker's subnet to the localhost's port 3306 for MySQL. 1. Enable routelocalnet for docker0 interface: 2. At this point, you'll want to double check and see which subnet is being used by your container for its internal docker network. The default network is 172.17.0.0/24, however if you have multiple containers / docker networks this network may be slightly different. You can check easily by either entering an interactive shell in your container and running ifconfig or ip addr to see which network your container is on. Alternatively, you can view the docker inspect info with the command docker inspect <container name> and look for the \"Gateway\" address in the network config towards the bottom of the inspect information. Finally, once you've confirmed the network your container is on, add these rules to iptables: At this point, we should be able to route on our local machine on port 3306. That is, from the docker subnet's gateway (172.17.0.1 aka the localhost) to our host's loopback address where the MySQL instance is listening (127.0.0.1:3306). 3. Make sure you have a mysql user in your local instance who is allowed to connect from outside of 'localhost', i.e. from '%'. If you're having trouble with authentication still, you may need to add a user who is explicitly allowed to connect from the Docker subnet on your computer. For example, with wide open privileges, allowing the user username to connect from any host: Alternatively, you can lock it down a bit more and only allow username to connect from the Docker subnet: 4. Finally, in your docker container setup, whether that's in a config value in the docker-compose.yml file or elsewhere in your dockerized application, make sure to change the mysql server address to 172.17.0.1. This is the default host's IP address from the view of the container.",
"title": "Docker + Localhost MySQL"
}