Mac上的Docker Beta:无法使用ip访问nginx容器 [英] Docker Beta on Mac : Cannot use ip to access nginx container
问题描述
我为osx安装了docker-beata( https://beta.docker.com/ )。
接下来,我创建了一个包含此文件的文件夹 docker-compose.yml
:
web:
image:nginx:最新
ports:
- 8080:80
之后,我使用这个命令: docker-compose up
。
容器从成功开始。
但问题是在我的容器中访问。我不知道什么ip使用。
我尝试用 docker ps
和 docker inspect ...
找到ip:
网络:{
/ pre>
bridge:{
IPAMConfig:null,
链接 :
别名:null,
NetworkID:6342cefc977f260f0ac65cab01c223985c6a3e5d68184e98f0c2ba546cc602f9,
EndpointID:8bc7334eff91d159f595b7a7966a2b0659b0fe512c36ee9271b9d5a1ad39c251,
网关:172.17.0.1 ,
IPAddress:172.17.0.2,
IPPrefixLen:16,
IPv6Gateway:,
GlobalIPv6Address:,
GlobalIPv6PrefixLen:0,
MacAddress:02:42:ac:11:00:02
}
}
所以我尝试使用
http://172.17.0.2:8080/
访问,但我有一个ERR_CONNECTION_TIMED_OUT
错误。
但是,如果我使用http:// localhost:8080 /
,我可以访问我的容器!
(但是我的本地主机已经在我的mac上由我的native配置使用,所以如果我想使用localhost,我必须停止我的本机apache)。
为什么不能使用ip?
解决方案正如@ Javier-Segura所说,在Linux上的本机Docker应该可以通过它的IP和端口打到容器,所以在你的情况下
http://172.17.0.2:80
- 8080端口将在主机IP上。
使用Docker for Mac Beta,对于容器来说,似乎没有一样的方式。它每个版本都有所改变,但是现在看来,你无法通过常规方式通过ip访问容器。
不幸的是,由于在OSX中的限制,我们无法将流量
路由到容器,并将容器从容器路由到主机。
您的最好的办法是使用不同的非冲突端口。您可以为不同的环境使用不同的Compose配置文件,如上例所示,使用8081进行开发,8080用于生产,如果这是愿望的话。您将通过类似
docker-compose -f docker-compose.yml -f production.yml up -d
的生产开始撰写,其中production.yml具有该环境的覆盖。I installed the docker-beata (https://beta.docker.com/) for osx. Next, I created a folder with this file
docker-compose.yml
:web: image: nginx:latest ports: - "8080:80"
After, I used this command :
docker-compose up
. Container start with success.But the problem is to access in my container. I don't know what ip use. I try to find ip with
docker ps
anddocker inspect ...
:"Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "6342cefc977f260f0ac65cab01c223985c6a3e5d68184e98f0c2ba546cc602f9", "EndpointID": "8bc7334eff91d159f595b7a7966a2b0659b0fe512c36ee9271b9d5a1ad39c251", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } }
So I try to use
http://172.17.0.2:8080/
to access, but I have aERR_CONNECTION_TIMED_OUT
error. But, if I usehttp://localhost:8080/
, I can access to my container ! (But my localhost is already use by my native config on my mac, so if I want use localhost I must stop my native apache).Why it's doesn't work with the ip ?
解决方案As @Javier-Segura mentioned, on with native Docker on Linux you should be able to hit the container via it's IP and port, so in your case
http://172.17.0.2:80
- the 8080 port would be on the host IP.With Docker for Mac Beta it does not appear to work the same way for the container. It changes a bit with every release but right now it appears you can not reach a container by ip via conventional means.
Unfortunately, due to limtations in OSX, we’re unable to route traffic to containers, and from containers back to the host.
Your best bet is to use a different non-conflicting port as mentioned. You can use different Compose config files for different environments, so as in the example above, use 8081 for development and 8080 for production, if that is the desire. You would start Compose in production via something like
docker-compose -f docker-compose.yml -f production.yml up -d
where production.yml has the overrides for that environment.这篇关于Mac上的Docker Beta:无法使用ip访问nginx容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!