Docker 容器无法连接到主机:没有到主机的路由 [英] Docker container cannot connect to host machine: No route to host
问题描述
我一直在尝试使用 docker compose 设置 docker 环境.让我难倒的一个问题是我的 docker 容器无法到达我的主机.
I've been trying to setup a docker environment using docker compose. One issue that has me stumped is that my docker containers cannot reach my host machine.
我使用以下撰写文件设置了一个容器:
I setup a container using the following compose file:
version: '3'
services:
webapp:
image: ...
ports:
- "8080:8080"
当我进入容器时,我可以ping我的主机:
When I enter the container, I am able to ping my host machine:
ping ${dockerHostIP}
但是,当我尝试在容器内使用 curl 检索主页时:
However when I try and retrieve the home page using curl inside the container:
curl http://${dockerHostIP}:8080
我明白了:
curl: (7) Failed connect to ${dockerHostIP}:8080; No route to host
我不知道应该怎么做才能解决这个 No route to host 错误.不幸的是,我需要能够做到这一点,因为 Web 应用程序在内部使用其主机名发出请求.
I cannot figure out what should be done to resolve this No route to host error. Unfortunately I need to be able to do this as the web application makes requests using its hostname internally.
跟踪路由结果:
traceroute ${dockerHostIP}
traceroute to ${dockerHostIP} (${dockerHostIP}), 30 hops max, 60 byte packets
1 ${dockerHostName} (${dockerHostIP}) 0.039 ms !X 0.012 ms !X 0.007 ms !X
推荐答案
令我惊讶的是,这实际上是我的防火墙问题.
To my surprise, it was actually a firewall issue in my case.
更新/启用 docker 主机上的 tcp 端口 [2375] 修复了该问题.
Update/Enable the tcp port [2375] on the docker host machine fixes the issue.
sudo firewall-cmd --zone=public --add-port=2375/tcp --permanent
sudo firewall-cmd --reload
有用的链接:1. 在此处输入链接描述2. 在此处输入链接描述
useful links: 1. enter link description here 2. enter link description here
这篇关于Docker 容器无法连接到主机:没有到主机的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!