Docker + Localhost Mysql

08.12.20191 Min Read — InLinux

I was having the hardest time getting docker containers to connect to the hosts instance of mysql.

I finally found a great solution, so I wanted to post it here for myself and others in the future:

Enable route_localnet for docker0 interface:

$ sysctl -w net.ipv4.conf.docker0.route_localnet=1

Add this rules to iptables:

$ iptables -t nat -I PREROUTING -i docker0 -d -p tcp --dport 3306 -j DNAT --to

$ iptables -t filter -I INPUT -i docker0 -d -p tcp --dport 3306 -j ACCEPT

Create mysql user with access from '%' that mean - from anyone, excluding localhost:

CREATE USER 'user'@'%' IDENTIFIED BY 'password';

Change in your docker run command or docker-compose, etc the mysql-server address to