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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With