简体   繁体   English

无法从 docker 容器连接到主机

[英]Unable to connect to host from docker container

I am having trouble accessing my host machine from a docker container, getting Connection refused error.我无法从 docker 容器访问我的主机,出现连接被拒绝错误。 I tried to solve it following this SO question: From inside of a Docker container, how do I connect to the localhost of the machine?我试图在这个 SO 问题之后解决它: 从 Docker 容器内部,如何连接到机器的本地主机?

Docker version is 20.10.18, build b40c2f6 Host is Ubuntu 22.04 LTS Docker 版本为20.10.18,构建 b40c2f6主机为 Ubuntu 22.04 LTS

What I am doing:我在做什么:

  • Started a server on port 3000 on the host machine在主机的 3000 端口上启动了一个服务器
  • server is accessible with curl localhost:3000服务器可通过curl localhost:3000访问

Then I tried the following, using curl docker image for testing, all throwing Connection refused error然后我尝试了以下,使用 curl docker 图像进行测试,所有抛出连接拒绝错误

# using host.docker.internal
docker run --add-host host.docker.internal:host-gateway --rm curlimages/curl:7.85.0 host.docker.internal:3000
# using IP from docker0 network 172.17.0.1
docker run --rm curlimages/curl:7.85.0 172.17.0.1:3000
# using host network
docker run --network=host --rm curlimages/curl:7.85.0 localhost:3000

What am I missing?我错过了什么?

My issue was running docker on rootless mode.我的问题是在无根模式下运行 docker。 https://docs.docker.com/engine/security/rootless/ https://docs.docker.com/engine/security/rootless/

After returning to the default context with返回到默认上下文后

 docker context use default

all the above solutions worked!以上所有解决方案都有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM