Mo Morsi
Mo Morsi

Reputation: 111

Cannot connect to mysql running in docker container from another

I know this seems like a common situation but nothing which I've found helps.

I'm following the docker 'getting-started' guide verbatim, literally copy and pasting the commands. But when I get to this step:

docker run -dp 3000:3000 \
   -w /app -v "$(pwd):/app" \
   --network todo-app \
   -e MYSQL_HOST=mysql \
   -e MYSQL_USER=root \
   -e MYSQL_PASSWORD=secret \
   -e MYSQL_DB=todos \
   node:12-alpine \
   sh -c "yarn install && yarn run dev"

The log output is as follows:

{ Error: connect EHOSTUNREACH 172.18.0.2:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
  errno: 'EHOSTUNREACH',
  code: 'EHOSTUNREACH',
  syscall: 'connect',
  address: '172.18.0.2',
  port: 3306 }

Here is the catch, I can successfully connect to mysql running in the container from the host system:

$ mysql -h172.18.0.2 -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>

Furthermore from the 'app' container, I can successfully ping the 'mysql' host. 'nc' is unable to connect though:

[mmorsi@localhost app]$ docker exec e8df2d36914 ping -c 3 mysql
PING mysql (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.071 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.173 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.176 ms

--- mysql ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.071/0.140/0.176 ms
[mmorsi@localhost app]$ docker exec e8df2d36914 nc -z -v mysql 3306
nc: mysql (172.18.0.2:3306): Host is unreachable

Other things I've tried:

My host system is Fedora 33. On the host, the docker0 interface is running in the 'trusted' zone (target: ACCEPT):

[mmorsi@localhost app]$ sudo firewall-cmd --get-active-zones
FedoraWorkstation
  interfaces: enp0s31f6
trusted
  interfaces: docker0
[mmorsi@localhost app]$ man firewall-cmd
[mmorsi@localhost app]$ sudo firewall-cmd --info-zone=trusted
trusted (active)
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: docker0
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

I'm all out of ideas. Any help or insights would be highly appreciated.

Upvotes: 0

Views: 610

Answers (1)

Mo Morsi
Mo Morsi

Reputation: 111

Figured it out!

(Courtesy of https://fedoramagazine.org/docker-and-fedora-32/)

A simple:

sudo firewall-cmd --permanent --zone=FedoraWorkstation --add-masquerade

So as to allow docker to make local connections was all that was needed! (in addition to the other steps listed there which I had previously run).

[Proceeds to pat self on back]

Upvotes: 1

Related Questions