从Docker容器沟通Docker主机 [英] Communicate to Docker host from Docker container
问题描述
我正在尝试停靠码头网络,我似乎找不到一种方法来完成以下操作:
I’m experimenting with docker networking, and I can’t seem to find a way to accomplish the following:
在主机中启动一个简单的netcat服务器:
Start a simple netcat server in the host:
nc -l -p 5555
,然后从一个码头容器内的与该服务器通信,例如
and then communicate to this server from within a docker container, e.g.
# grab the docker network interface ip
hostip=$(ip route | awk '/docker0/ { print $NF }')
# pass in the docker network interface as a host and try a curl command
docker run --add-host=docker:"${hostip}" --rm -it hiromasaono/curl curl docker:5555
卷曲请求刚挂起,主机netcat服务器没有收到请求。
The curl request just hangs and the host netcat server does not receive a request.
如果我改为启动docker - net = host
选项,它的作用是:
If I instead start docker with --net=host
option, it works:
docker run --net=host --rm -it hiromasaono/curl curl 127.0.0.1:5555
和netcat服务器收到
and the netcat server receives
GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: 127.0.0.1:5555
Accept: */*
如何在Docker容器内与简单的主机netcat服务器进行通信,而不使用 - net = host
(默认值为 - net = bridge
)?
How can I communicate to the simple host netcat server from within the docker container without using --net=host
(the default is --net=bridge
)?
(fyi:我正在运行docker服务器/客户端1.11.2)
(fyi: I'm running docker server/client 1.11.2)
我已经学习了相关资源,以寻找答案:
- Document how to connect to docker host from container (github issue)
- How to connect to Docker host from container (github issue)
- Allow Docker Container to Connect to a Local Postgres DB
- Docker Container Networking Documentation
推荐答案
据我所知,两个评论 [1] [2] ,截至今天,以下非官方的黑客似乎在为我工作(2016.10.27):
As far as I understand, based on two comments[1][2], the following unofficial hack seems to be working for me as of today (2016.10.27):
在需要连接到Docker主机的应用程序的 Dockerfile
中,我添加了以下行: p>
In the Dockerfile
for the app which needs to connect to the Docker host, I added the following lines:
...
# netstat
RUN apt-get update && apt-get install net-tools -y
...
CMD (netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2" dockerhost"}' >> /etc/hosts) && \
...old CMD line...
...
这似乎使Docker主机从容器内的可用
This seems to make the Docker host available to me from within the container as dockerhost
.
未来读者注意:另请参见 https://github.com/docker/docker/issues/23177 —作为写作,这个问题仍然是开放的,所以在未来的某个时候可能会成为官方的非零机会,并取代任何解决办法;尽管它也可能像其各种前辈一样被关闭和被解雇。
Note to future readers: see also https://github.com/docker/docker/issues/23177 — as of writing, this issue is still open, so there's a non-zero chance it may become official at some point in future and supersede any workarounds; though it may also become closed and dismissed as happened to its various predecessors.
这篇关于从Docker容器沟通Docker主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!