启用从 docker 容器通过 ubuntu 防火墙访问主机服务

Pay*_*ian 7 firewall ufw ubuntu docker

我有一个服务在端口 的主机上运行8545。我有几个需要访问主机上的此服务的 docker 容器。主机运行的是ubuntu。我已经配置成功了

extra_hosts:
- "host.docker.internal:host-gateway"
Run Code Online (Sandbox Code Playgroud)

在 docker-compose 文件中,我用它来启动我的 docker 容器。但是,我发现容器无法访问host.docker.internal:8545,除非我使用以下命令打开主机上的该端口

ufw allow 8545
Run Code Online (Sandbox Code Playgroud)

然而,这会向任何人开放该端口,这是不希望的。

如何向主机上运行的 docker 容器开放此端口?

编辑:我已经看到该docker0接口的 IP 为172.17.0.1. 我尝试运行sudo ufw allow from 172.17.0.1,但这并没有使我的容器能够访问8545主机上的端口。

root@localhost:~/code/metis/ops# ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Anywhere                   ALLOW       172.17.0.1
22/tcp (v6)                ALLOW       Anywhere (v6)

root@localhost:~/code/metis/ops# docker exec -it ops_l2geth-mainnet_1 /bin/sh
/ # geth attach http://host.docker.internal:8545
Fatal: Failed to start the JavaScript console: api modules: Post "http://host.docker.internal:8545": context deadline exceeded
Run Code Online (Sandbox Code Playgroud)

编辑2:我还尝试了这里的另一个建议,但也不起作用:

root@localhost:~/code/metis/ops# ufw allow out on docker0 from 172.17.0.0/16
Rule added
root@localhost:~/code/metis/ops# docker exec -it ops_l2geth-mainnet_1 /bin/sh
/ # geth attach http://host.docker.internal:8545
Fatal: Failed to start the JavaScript console: api modules: Post "http://host.docker.internal:8545": context deadline exceeded
Run Code Online (Sandbox Code Playgroud)

编辑 3:我忘了提及我正在使用 docker-compose 运行这些容器。据我了解,docker-compose 使用自定义网络,这可能解释了为什么上述ufw allow命令没有帮助。

Pay*_*ian 13

弄清楚了!虽然我不确定这是否是一个通用的解决方案。

\n

事实证明,因为我使用docker-compose带有 IP 的默认 docker0 接口启动容器172.17.0.1,所以容器与主机的通信方式并不是这样。就我而言,docker-compose创建了一个名为的新网络ops_default

\n
 \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf docker network ls\nNETWORK ID     NAME          DRIVER    SCOPE\n2774ed101a84   bridge        bridge    local\na6176c796a29   host          host      local\ndfcd1606b19d   none          null      local\n7415a4410daf   ops_default   bridge    local\n
Run Code Online (Sandbox Code Playgroud)\n

检查ops_default产生以下结果

\n
 \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf docker network inspect ops_default\n[\n    {\n        "Name": "ops_default",\n        "Id": "7415a4410daf3df718ce957787abd1b9842e4e914fd1b2ff549c80e56d032265",\n        "Created": "2022-03-10T16:14:13.789181757Z",\n        "Scope": "local",\n        "Driver": "bridge",\n        "EnableIPv6": false,\n        "IPAM": {\n            "Driver": "default",\n            "Options": null,\n            "Config": [\n                {\n                    "Subnet": "172.22.0.0/16",\n                    "Gateway": "172.22.0.1"\n                }\n            ]\n        },\n        "Internal": false,\n        "Attachable": true,\n        "Ingress": false,\n        "ConfigFrom": {\n            "Network": ""\n        },\n        "ConfigOnly": false,\n        "Containers": {\n        }\n    }\n]\n
Run Code Online (Sandbox Code Playgroud)\n

看来这个网络在subnet上运行172.22.0.0/16。跑步ufw allow from 172.22.0.0/16解决了我的问题!

\n
root@localhost:~/code/metis/ops# ufw status\nStatus: active\n\nTo                         Action      From\n--                         ------      ----\n22/tcp                     ALLOW       Anywhere\nAnywhere                   ALLOW       172.22.0.0/16\n22/tcp (v6)                ALLOW       Anywhere (v6)\n\nroot@localhost:~/code/metis/ops# docker exec -it ops_l2geth-mainnet_1 /bin/sh\n/ # geth attach http://host.docker.internal:8545\nWelcome to the Geth JavaScript console!\n\ninstance: Geth/v1.10.17-unstable-19c2c60b-20220308/linux-amd64/go1.17.8\nat block: 14360238 (Thu, 10 Mar 2022 16:44:29 UTC)\n modules: eth:1.0 net:1.0 rpc:1.0 web3:1.0\n\n> \n
Run Code Online (Sandbox Code Playgroud)\n