大网数据广告活动
服务器运维问答

Can't ping / access docker host on 172.17.0.1 from inside a container

· 52CE 我爱测

My docker host listens on 172.17.0.1. I can curl it from the host machine, but when I curl the same ip/port from within the container I get timeout. I can ping anything from withing the container but I can't access the host. What…

问题原文

原帖链接 · Dani · CC BY-SA 4.0

My docker host listens on 172.17.0.1.

I can curl it from the host machine, but when I curl the same ip/port from within the container I get timeout.

I can ping anything from withing the container but I can't access the host.

What am I missing ?

Container is Jenkins image run by this command:

docker run -d --name jenkins -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:2.222.3

Running curl on the host:

curl http://172.17.0.1:2375 {"message":"page not found"}

Running curl from withing the docker will time out.

curl http://172.17.0.1:2375 curl: (7) Failed to connect to 172.17.0.1 port 2375: Connection timed out

已采纳解答

原帖链接 · BMitch · CC BY-SA 4.0

That's typically because iptables on the host is blocking access from the docker networks. You can look at iptables -S or iptables -nvL to see your current rules.


That said, if you do open up the docker API, you need to be careful about who can access that API. Using port 2375 typically means you have not configured mTLS, see docker's guide for setting up mTLS. This means anyone with access to the port can submit API calls, which means an unprivileged local user, or any other container, has the ability to get root on your host. In your case, that's as easy as:

docker -H tcp://172.17.0.1:2375 run -it --rm --privileged --pid host debian nsenter -t 1 -m -u -n -i bash

The recommended solution is to either use DinD to run the docker engine inside of a container, or to share the docker socket as a file/volume mount with the appropriate UID/GID access to the file. That ensures only that container has access to the docker engine rather than all users and any container running on the host. To handle the UID/GID access issues with files mounted in a volume, I've done this specifically for Jenkins images in my jenkins-docker repo, and there's a more generic solution in my fix-perms script in my docker-base repo.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。