简体   繁体   中英

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

Been looking into using Docker for a REST service project. One question I have is whether we could use Docker to run multiple versions of the service on the same host/port.

For example, I want to have an endpoint at {myserver}:8080/v1/ and another at {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.

Is this possible with Docker containers?

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

This is as much a question about the way tcp ports work as the way docker works. In the same way that two applications can't bind to the same tcp port, neither can two docker containers.

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). 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

You could have two separate containers both listening on: 192.168.11.223:80 10.88.88.12:80

If you look at the syntax for 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. you can check the IP address of the containers by using below command.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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