[英]How can I map a Docker containers port to a different number - but not on the host
For testing purposes I would like to run a cluster of three containers, each running the same service on port 7600. Those containers should reside in one network and could theoretically access each other as host1:7600
, host2:7600
and host3:7600
. 为了进行测试,我想运行一个由三个容器组成的群集,每个容器在端口7600上运行相同的服务。这些容器应该驻留在一个网络中,并且理论上可以作为
host1:7600
, host2:7600
和host3:7600
。
However I want to 'emulate' an external port mapping, such that the service of each container is still bound to port 7600 but that the services can acess each other by maped (different) ports like host1:8881
, host2:8882
and host3:8883
. 但是,我想“模拟”外部端口映射,以便每个容器的服务仍绑定到端口7600,但服务可以通过映射(不同)的端口(例如
host1:8881
, host2:8882
和host3:8883
。
How can I do that as easily as possible - preferred within a Docker Compose setup. 我如何尽可能轻松地做到这一点-在Docker Compose设置中首选。
The reasoning is that I want to test how the service will behave with a configuration of three physical hosts running that service and mapped its port to an arbitrary external port. 原因是我想测试该服务在运行该服务的三个物理主机的配置下的行为,并将其端口映射到任意外部端口。
Following some edits to clarify the task, after the first comments won't met the requirements (however thank you for every answer). 经过一些编辑以澄清任务后,第一条评论将不符合要求(但是,谢谢您的每一个回答)。
After further investigation I found a working solution for me. 经过进一步调查,我找到了可行的解决方案。
The following Docker Compose file shows an example of the solution. 以下Docker Compose文件显示了解决方案的示例。 It shows how to make two services accessible by an external IP and external port.
它显示了如何使两个服务可以通过外部IP和外部端口访问。 The example works completely in Docker without the need to run the containers in two separate virtual machines.
该示例完全可以在Docker中运行,而无需在两个单独的虚拟机中运行容器。
The two services are by example two Nginx instances. 例如,这两个服务是两个Nginx实例。 Imagine both services should access each other by their external IP and port to form a cluster.
想象一下,两个服务都应该通过其外部IP和端口相互访问以形成集群。 The external IP and port are emulated by two separate busybox containers mapping the ports of the service containers to their own IP.
外部IP和端口由两个单独的busybox容器模拟,这些容器将服务容器的端口映射到它们自己的IP。
version: '3'
services:
service1:
image: nginx:latest
service2:
image: nginx:latest
proxy1:
image: busybox:latest
command: nc -lk -p 8081 -e /bin/nc service1 80
expose:
- "8081"
proxy2:
image: busybox:latest
command: nc -lk -p 8082 -e /bin/nc service2 80
expose:
- "8082"
The services service1:80
and service2:80
can access each other by their external representations proxy1:8081
and proxy2:8082
服务
service1:80
和service2:80
可以通过它们的外部表示proxy1:8081
访问proxy1:8081
和proxy2:8082
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.