如何解决docker宿主机无法访问容器中的服务?
PHP中文网
PHP中文网 2017-04-27 09:03:17
0
2
923

1、在容器中访问容器中的服务有响应,在宿主机访问容器服务无响应,但是宿主机可以访问到容器nginx服务的欢迎界面,截图如下:

在容器中访问容器中的服务有响应:

curl 172.17.0.2:8080

在宿主机访问容器中的服务无响应:

curl 172.17.0.2:8080


不加端口直接访问,会得到nginx服务的欢迎界面代码:

curl 172.17.0.2

2、由于容器里可以访问该服务,所以服务是启动了的;在宿主机可以访问容器中的nginx欢迎界面,说明宿主机可以访问容器中80端口的服务,问题到底出在什么地方呢?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
世界只因有你

Each image defines an interface that can be provided to the outside world. The Nginx image only provides ports 80 and 443 by default. Naturally, you cannot access the 8080 port in the container.

You only need to set the docker create或者docker run创建容器时携带--exposeparameter to open the specified port.

--expose Expose a port or a range of ports

Peter_Zhu

You need to expose the specified port to the host
docker run -p 8080:80 your image name

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!