简体   繁体   English

多个Docker容器可以使用相同的主机/端口运行吗?

[英]Can multiple Docker containers run using the same host/port?

Been looking into using Docker for a REST service project. 一直在考虑使用Docker进行REST服务项目。 One question I have is whether we could use Docker to run multiple versions of the service on the same host/port. 我有一个问题是我们是否可以使用Docker在同一主机/端口上运行多个版本的服务。

For example, I want to have an endpoint at {myserver}:8080/v1/ and another at {myserver}:8080/v2/. 例如,我想在{myserver}:8080 / v1 /和另一个{myserver}:8080 / v2 /。

If it's relevant at all, these would be Java:8 based Docker images constructed with a java jar on the Spring Boot REST framework. 如果它完全相关,那么这些将是基于Java:8的Docker镜像,在Spring Boot REST框架上使用java jar构建。

Is this possible with Docker containers? Docker容器可以实现吗?

您可以使用不同的主机端口运行这两个容器,并使用haproxy / nginx / varnish(本机或内部另一个容器)监听主机端口,并根据URL重定向到正确的容器。

This is as much a question about the way tcp ports work as the way docker works. 这是关于tcp端口作为docker工作方式的方式的问题。 In the same way that two applications can't bind to the same tcp port, neither can two docker containers. 与两个应用程序无法绑定到同一个tcp端口的方式相同,两个docker容器也不能。

As @Sergei Rodionov points out SO_REUSEPORT can be used to allow multiple processes to share the same tcp port (and this can be specified when launching your java application). 正如@Sergei Rodionov所指出的,SO_REUSEPORT可用于允许多个进程共享相同的tcp端口(这可以在启动java应用程序时指定)。 I don't think this will work across containers. 我认为这不会适用于容器。

Yes, this is possible as long as you are using different network addresses for each duplicate port you are listening on. 是的,只要您为正在侦听的每个重复端口使用不同的网络地址,就可以这样做。

For example, your host has the following IPs assigned to it: 192.168.11.223 10.88.88.12 例如,您的主机分配了以下IP:192.168.11.223 10.88.88.12

You could have two separate containers both listening on: 192.168.11.223:80 10.88.88.12:80 您可以使用两个单独的容器来监听:192.168.11.223:80 10.88.88.12:80

If you look at the syntax for docker run: 如果你看一下docker run的语法:

-p=[]      : Publish a container᾿s port or a range of ports to the host 
             format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
             Both hostPort and containerPort can be specified as a range of ports. 
             When specifying ranges for both, the number of container ports in the range must match the number of host ports in the range. (e.g., `-p 1234-1236:1234-1236/tcp`)
             (use 'docker port' to see the actual mapping)

yes, it is possible as long as containers are using different IP address. 是的,只要容器使用不同的IP地址就可以。 you can check the IP address of the containers by using below command. 您可以使用以下命令检查容器的IP地址。

docker inspect -f '{{ .NetworkSettings.IPAddress }}' <container ID>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM