如何在没有端口映射的情况下将 docker 容器的 ip 和端口暴露给外部 docker 主机? [英] How to expose docker container's ip and port to outside docker host without port mapping?

查看:53
本文介绍了如何在没有端口映射的情况下将 docker 容器的 ip 和端口暴露给外部 docker 主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在一个 docker 主机上为同一个 web 镜像启动两个 docker 容器时.

When i started two docker containers for a same web image on one docker host.

  • 两个 docker 容器监听同一个端口 5000
  • 两个容器的
  • 端口5000被映射到docker主机的不同端口:49155,49156
  • 从外部docker主机访问两个容器需要通过访问docker主机ip和端口4915549156
  • 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

是否有解决方案可以通过 docker 主机的 ip 和端口 x.x.x.x:5000 从 docker 主机外部访问 docker 容器,而无需端口映射?

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?

不同dock主机上的所有docker容器可以直接相互访问.

All docker containers on different dock hosts can access each other directly.

推荐答案

您可以通过主机上的 IP 别名来实现这一点.

You can accomplish this with IP aliasing on the host.

首先,在与主接口具有不同 IP 地址的主机上添加一个虚拟接口.我们将使用 IP 10.0.0.10 调用主接口 eth0,使用 IP 地址 10.0 调用虚拟接口 eth0:1.0.11.

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 

现在运行容器并将端口 5000 映射到相应的接口.例如:

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>

现在您可以在外部使用不同的 IP 地址访问端口 5000 上的每个容器.

Now you can access each container on port 5000 using different IP addresses externally.

这篇关于如何在没有端口映射的情况下将 docker 容器的 ip 和端口暴露给外部 docker 主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆