laravel 链接到容器时如何使用主机网络?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35650395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use host network while linking to a container?
提问by simo
In my docker-compose:
在我的 docker-compose 中:
laravel:
image: trackware
links:
- postgis:postgis
ports:
- "80:80"
- "3306:3306"
- "443:443"
- "220:22"
- "8000:8000"
net: "host"
restart: always
volumes:
- C:/H/repositories/pubnub:/share
container_name: laravel
postgis:
image: mdillon/postgis
env_file: .postgis_env
ports:
- "9090:9000"
- "54320:5432"
container_name: postgis
if I run docker-compose up -d
I get this error:
如果我运行,docker-compose up -d
我会收到此错误:
Conflicting options: host type networking can't be used with links. This would result in undefined behavior
So, how would I use net: "host"
while linking to postgis container?
laravel container needs to run pubnub client, which will need high-performance networking for real time messages handling, and also it needs to link to postgis
container to access db.
那么,net: "host"
在链接到 postgis 容器时我将如何使用?laravel 容器需要运行 pubnub 客户端,它需要高性能网络来实时处理消息,还需要链接到postgis
容器来访问 db。
So, any advice? I am using docker 1.10.2
那么,有什么建议吗?我正在使用 docker 1.10.2
采纳答案by rm -rf
Since you expose postgis ports to host, you can skip linking and connect to it through localhost:9000
. I believe this will work since the Laravel application resides on the host network and they will share those ports.
由于您将 postgis 端口暴露给主机,您可以跳过链接并通过localhost:9000
. 我相信这会起作用,因为 Laravel 应用程序驻留在主机网络上并且它们将共享这些端口。
回答by SLY
I dunno reason but... You shouldn't use "host" driver and port mapping, at least you wouldn't get expected result. In case like this "220:22" you'll get 22 port mapped to the host machine.
我不知道为什么......你不应该使用“主机”驱动程序和端口映射,至少你不会得到预期的结果。在这种“220:22”的情况下,您将获得映射到主机的 22 端口。
"Net" is outdated as far as I know, use "network_mode" instead. Also I would recommend you to update docker-compose to the latest version, now is 1.6.2. Previous versions had some networking problems.
据我所知,“Net”已经过时,请改用“ network_mode”。另外我建议您将 docker-compose 更新到最新版本,现在是 1.6.2。以前的版本有一些网络问题。
May be you can use "bridge" driver? In your case, I can't see problems which it couldn't resolve.
可能是你可以使用“桥”驱动程序吗?在您的情况下,我看不到它无法解决的问题。
回答by Rohit Salecha
The reason why we use links keyword is so that docker can internally make hostname resolution so that two disparate containers can communicate with each other. In your case if you were not using host network and using the link keyword then docker would have created a hostname with each container names internally so both containers can communicate with each others. When you are using "host" mode network means you are telling docker that i shall be using the "hosts" underlying network and hence by simply exposing the ports on the localhost my containers can communicate with each other.
我们使用 links 关键字的原因是为了让 docker 可以在内部进行主机名解析,以便两个不同的容器可以相互通信。在您的情况下,如果您没有使用主机网络并使用 link 关键字,那么 docker 将在内部为每个容器名称创建一个主机名,以便两个容器可以相互通信。当您使用“主机”模式网络时,意味着您告诉 docker 我将使用“主机”底层网络,因此通过简单地公开本地主机上的端口,我的容器可以相互通信。