[英]Multiple docker containers with same container port connected to the same network
I am having an app that depends on multiple docker containers. 我有一个依赖多个Docker容器的应用程序。 I use docker compose so that all of them are in the same network for inter-container communication.
我使用docker compose,使它们都在同一网络中以进行容器间通信。 But, two of my containers are listening to the same port 8080 inside their respective containers but, are mapped to different ports on the host: 8072,8073.
但是,我的两个容器正在侦听它们各自容器内的相同端口8080,但被映射到主机上的不同端口:8072,8073。 For inter-container communication since we use the container's port will this cause problems?
对于容器间的通信,因为我们使用容器的端口,这会引起问题吗?
Constraints: 约束:
Am new to docker and I am not sure how to solve this. 我是docker的新手,我不确定该如何解决。
Thanks 谢谢
IIUC see the documentation here: https://docs.docker.com/compose/networking IIUC在此处查看文档: https ://docs.docker.com/compose/networking
You need not expose each of the service's ports on the host unless you wish to access them from the host, ie outside of the docker-compose created network. 除非您希望从主机访问它们,即在docker-compose创建的网络之外,否则无需在主机上公开每个服务的端口。
Ports must be unique per host but each service in your docker-compose created network can use the same port with impunity and is referenced by :. 每个主机的端口必须唯一, 但是由 docker-compose创建的网络中的每个服务都可以使用不受干扰的同一端口,并由:引用。
In the Docker example, there could be 2 Postgres services. 在Docker示例中,可能有2个Postgres服务。 Each would need a unique name:
db1
; 每个都需要一个唯一的名称:
db1
; db2
but both could use the same port - "5432" and be uniquely addressable from the service called web
(and each other) as db1:8432
and db2:8432
. db2
但是两者都可以使用相同的端口“ 5432”,并且可以通过名为db1:8432
和db2:8432
web
服务(以及彼此)进行唯一寻址。
Each service corresponds effectively to a different host. 每个服务有效地对应于不同的主机。 So, as long as the ports are unique for each service|host, you're good.
因此,只要每个主机的端口都是唯一的,那就好。 And, as long as any ports you expose on the host are unique, you're good too....
而且,只要您在主机上公开的任何端口都是唯一的,那么您就很好。
Extending the example, db1
could expose port 9432:8432
but then db2
would need to find a different host port to use, perhaps 9433:8432
. 扩展示例,
db1
可以公开端口9432:8432
但是db2
将需要找到另一个主机端口来使用,也许是9433:8432
。
Within the docker-compose created network, you would access db1
as db1:8432
and db2
as db2:8432
. 在泊坞窗,撰写创建的网络,你将访问
db1
为db1:8432
和db2
作为db2:8432
。
From the host (outside the docker-compose create network), you would access db1
as localhost:9432
and db2
as localhost:9433
. 从主机(在docker-compose create网络外部),您将以
localhost:9432
身份访问db1
,以localhost:9433
身份访问db2
。
NB It's likely a good practice to only expose service ports to the host when those service's must be accessible from outside (eg web
probably must be exposed but dbX
probably need not be exposed). 注意:只有在必须可以从外部访问服务端口的情况下,才将服务端口暴露给主机(例如,可能必须公开
web
而dbX
公开dbX
是一个好习惯。 You may wish to be more liberal in exposing service ports while debugging. 您可能希望在调试时公开服务端口时更加自由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.