Allow Docker Container to connect Local Database
1. Implement Docker Interface in Firewall:
sudo firewall-cmd --zone=docker --change-interface=docker0
sudo firewall-cmd --permanent --zone=docker --change-interface=docker0
sudo systemctl restart firewalld
Check the Changes
a. sudo ip addr show docker0
docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:72:c5:a8:50 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
b. firewall-cmd --get-active-zones
docker
interfaces: docker0
public
interfaces: enp3s0
2. Add Database Port Number in Firewall
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --list-all
sudo service firewalld restart
Check the Changes
a. sudo netstat -tnlp | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 10275/mysqld
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN 10275/mysqld
b. sudo firewall-cmd --list-all
public (active)
ports: 80/tcp 8080/tcp 7070/tcp 3306/tcp
3. Change bind-address in /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
sudo service mysql restart
4. Add a Database User as @ %
ALTER USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'P@ssword123';
ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'P@ssword123';
FLUSH PRIVILEGES;
sudo service mysql restart
5. Create a Docker as --network="bridge"
sudo docker run -d -it --restart=always --network="bridge" --name webapp -v /srv/code:/opt/code/ -p 81:80 webapp zsh
Now you can access local Database from webapp Docker Container by following credential -
host: 172.17.0.1
adapter: mysql
database: database_name
port: 3306
username: admin
password: P@ssword123
Allow local system to connect Docker Database (Alternative)
In Ubuntu:
First You have to check that is the Docker Database port is Available in your system by following command -
sudo iptables -L -n
Sample OUTPUT:
Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22
Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
Now, You can easily access the Docker Database from your local system by following configuration
host: 172.17.0.2
adapter: mysql
database: DATABASE_NAME
port: 3307
username: DATABASE_USER
password: DATABASE_PASSWORD
encoding: utf8
In CentOS:
First You have to check that is the Docker Database port is Available in your firewall by following command -
sudo firewall-cmd --list-all
Sample OUTPUT:
target: default
icmp-block-inversion: no
interfaces: eno79841677
sources:
services: dhcpv6-client ssh
**ports: 3307/tcp**
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -
sudo firewall-cmd --zone=public --add-port=3307/tcp
In server, You can add the port permanently
sudo firewall-cmd --permanent --add-port=3307/tcp
sudo firewall-cmd --reload
Now, You can easily access the Docker Database from your local system by the above configuration.