How to allow multiple containers to utilise and expose one port

I have a service that sends messages to the docker container where I am using Linux as my host for the docker environment, I have tested the service and it is working fine for one container. However, I would like to use the same port I have exposed in a second and even more containers. I made the DockerFile (which is the base of all the containers) to have the part that should be exposed.

# Exposing image ports:
EXPOSE 8080

and I am running the normal docker command

docker run -it --cap-add=all --hostname debian -p 8080:8080 --dns-opt='options single-request' --sysctl net.ipv6.conf.all.disable_ipv6=1 --name Container_Name1 image

I know that it is not possible to make the second container utilise the same exposed port as I got the message,

docker: Error response from daemon: driver failed programming external connectivity on endpoint Container_Name2 (49a8eda5c2cdff68137b300dcc077b9b609bd3be29d8015be9168888790696fb): Bind for 0.0.0.0:8080 failed: port is already allocated.

However, I can’t change the port number every time to be able to connect to the rest of the containers. What would be the best approach from here to be able to connect to all containers using that port?

A port can be bound on an ip by exactly one process, regardless whether it’s a native app on the host, or the host port of a published container port.

If your traffic is http(s) only, you could use a reverse proxy like Traefik, to have a single container publishing the ports, and then use rules for domain names or paths to forward traffic to a target container. If your traffic is non http, but is wrapped in TLS, you could leverage SNI to forward traffic to target containers based on domain names. If none of this matches your situation: there is no way to overcome the limitation I wrote in the first sentence.

Hello Metin, thank you for your reply. Well, I’m using service that will be sending through UDP and/or TCP port (was testing to see which will be best for sending messages to container, haven’t decided yet). So, I’m not sure what would be the best approach for this case. However, I saw somewhere about port forwarding. Will it work for my case by making port forwarding and reverse? Say like container would have 8081.

Usually a reverse proxy is used as gatekeeper and to proxy/forward requests internally to the corresponding target service. Check nginx-proxy with companion and simple Traefik example. The proxy can usually handle TLS/SSL termination.

Note that this only works on http and https requests and pure TCP connections with TLS and HostSNI. It does not work for other TCP connections (like SSH) or for any UDP port, as there is no info provided which target to use.

Well, I am not targeting the SSH port. I am just aiming for messages to be sent through either TCP or UDP, I was able to perform some tests and I think UDP works fine with me.

You can use a reverse proxy for UDP, but it can only forward to a single service, potentially to multiple instances.

But it can’t proxy UDP to different services, as UDP does not include a domain or path, by which a proxy usually determines which target to send the packet to.

The same is true for TCP, unless it’s wrapped in TLS, which is not the case for SSH connections.