Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 431
MJ. Avatar asked Jun 20 '15 19:06

MJ.


People also ask

Can two containers use the same port in a pod?

Because multi-container Pods share the same IP address and communicate on localhost , this means that two containers can't share the same port, if they're in the same Pod. For example, you couldn't have two containers in the same Pod which expose port 8080 , because there would be a conflict.

Can we run multiple Docker containers on a single host?

With Docker compose, you can configure and start multiple containers with a single yaml file. This is really helpful if you are working on a technology stack with multiple technologies.

Can you have multiple Docker containers running?

Docker Compose is a tool that helps us overcome this problem and efficiently handle multiple containers at once. Also used to manage several containers at the same time for the same application. This tool can become very powerful and allow you to deploy applications with complex architectures very quickly.

How many containers can 1 host run?

The typical organization that uses a container orchestrator runs 11.5 containers per host, as compared to about 6.5 containers per host in unorchestrated environments.


2 Answers

You can run both containers using different host ports, and use a haproxy/nginx/varnish (native or inside another container) listening to the host port, and redirecting to the right container based on the URL.

like image 190
gmuslera Avatar answered Oct 17 '22 04:10

gmuslera


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.

like image 36
Robert Moskal Avatar answered Oct 17 '22 05:10

Robert Moskal