如何将docker容器的ip和port暴露给docker主机,而不需要端口映射? [英] How to expose docker container's ip and port to outside docker host without port mapping?
问题描述
- 在同一个端口5000上收听两个码头容器两个容器的端口5000映射到码头主机的不同端口:49155,49156
- 从docker主机需要的外部端口访问两个容器通过访问docker主机ip和端口49155或49156
有没有解决方案可以从Docker主机之外访问docker容器它的ip和port,xxxx:5000,没有端口映射?
不同终端主机上的所有docker容器可以直接访问。
您可以使用主机上的IP别名来实现此目的。
首先,在主机上添加与主界面不同的IP地址的虚拟接口。我们将调用IP 10.0.0.10
的主界面 eth0
,虚拟接口 eth0:1
,IP地址为 10.0.0.11
。
ifconfig eth0:1 10.0.0.11 netmask 255.255.255.0 up
现在运行容器并将端口5000映射到相应的接口。例如:
docker run -p 10.0.0.10:5000:5000 -name container1< someimage> < somecommand>
docker run -p 10.0.0.11:5000:5000 -name container2< someimage> < somecommand>
现在,您可以使用不同的IP地址从外部访问端口5000上的每个容器。
When i started two docker containers for a same web image on one docker host.
- two docker containers listened on the same port 5000
- port 5000 of the two containers were mapped to different ports of docker host: 49155, 49156
- to access the two containers from outside docker host need to be by accessing the docker host ip and port 49155 or 49156
Is there a solution to access a docker container from outside docker host by its ip and port, x.x.x.x:5000, without port mapping?
All docker containers on different dock hosts can access each other directly.
You can accomplish this with IP aliasing on the host.
First, add a virtual interface on the host that has a different IP address than the primary interface. We'll call the primary interface eth0
with IP 10.0.0.10
, and the virtual interface eth0:1
with IP address 10.0.0.11
.
ifconfig eth0:1 10.0.0.11 netmask 255.255.255.0 up
Now run the containers and map port 5000 to the corresponding interface. For example:
docker run -p 10.0.0.10:5000:5000 -name container1 <someimage> <somecommand>
docker run -p 10.0.0.11:5000:5000 -name container2 <someimage> <somecommand>
Now you can access each container on port 5000 using different IP addresses externally.
这篇关于如何将docker容器的ip和port暴露给docker主机,而不需要端口映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!