简体   繁体   English

具有相同容器端口连接到同一网络的多个Docker容器

[英]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: 约束:

  1. I need both the containers for my app to run. 我需要两个容器才能运行我的应用程序。 Thus I cannot isolate the other container with same internal port to a different network 因此,我无法将具有相同内部端口的其他容器隔离到其他网络
  2. All containers should run on the same host. 所有容器应在同一主机上运行。

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:8432db2: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 . 在泊坞窗,撰写创建的网络,你将访问db1db1:8432db2作为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). 注意:只有在必须可以从外部访问服务端口的情况下,才将服务端口暴露给主机(例如,可能必须公开webdbX公开dbX是一个好习惯。 You may wish to be more liberal in exposing service ports while debugging. 您可能希望在调试时公开服务端口时更加自由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 重定向容器内的端口或具有相同端口和network_mode的多个容器 - Docker redirect port inside container or multiple containers with same port and network_mode 通过多个 Docker 容器监听同一个端口 - Listen to a same port via multiple Docker containers 如何在同一网络中使用localhost访问其他docker容器中的一个容器的端口 - How to access port of one container in other docker containers with localhost in the same network 如何从同一网络中的 docker 容器中扫描 docker 容器 IP? - How to scan docker containers IPs from a docker container that is in the same network? Docker 编写:将容器端口重新映射为在网络内部和主机上相同 - Docker Compose: Remap a containers port to be the same inside the network and on the host 将 Docker 容器连接到与 Azure 管道中的当前容器相同的网络 - Connect Docker containers to same network as current Container in Azure Pipelines 如何使用 docker 容器作为同一网络中其他容器的路由器 - How to use docker container as a router for other containers in the same network docker容器映射到同一端口 - docker containers mapped to same port 同一港口有更多 Docker 个集装箱 - More Docker containers on same port 运行多个 docker 容器在同一个端口上侦听 RabbitMQ 消息 - Running multiple docker containers listening on same port for RabbitMQ messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM