0

我知道这似乎是一种常见情况,但我发现没有任何帮助。

我正在逐字遵循 docker ' Getting-started ' 指南,从字面上复制和粘贴命令。但是当我到达这一步时:

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"

日志输出如下:

{ 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 }

这是一个问题,我可以成功地从主机系统连接到容器中运行的 mysql:

$ 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)]>

此外,从“应用程序”容器中,我可以成功 ping 到“mysql”主机。'nc' 无法连接:

[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

我尝试过的其他事情:

  • 运行最新的 mysql docker 镜像
  • 在 centos:latest 映像上运行“应用程序”(带有安装 deps 的必要步骤)

我的主机系统是 Fedora 33。在主机上,docker0 接口正在“受信任”区域中运行(目标: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: 

我完全没主意了。任何帮助或见解将不胜感激。

4

1 回答 1

0

弄清楚了!

(由https://fedoramagazine.org/docker-and-fedora-32/提供)

一个简单的:

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

只需要让 docker 建立本地连接!(除了我之前运行的那里列出的其他步骤)。

[继续拍拍自己的背]

于 2021-12-24T02:10:19.293 回答