Swarm service port mappings


My container runs a TCP socket server which listens to commands on port 6000.

Now i launch this same container as a swarm service with multiple replicas.
Somehow, I want all the containers in the service to be able to receive the commands simultaneously at their internal port 6000, when I make a tcp connection from outside.

But when i connect to the host using :6000, only one container at any time receives the commands.

I created the swarm with 2 replicas:

docker service create
–replicas 2
–network mynet
–mount type=bind,source=/tmp/.X11-unix,destination=/tmp/.X11-unix
-p 6000:6000
–name service_nv2
myContainerImage /home/command.sh

I see my 2 containers are launched correctly on the 2 hosts.
But only one of them gets my commands.

Hi, this is the normal behavior.
Swarm has an internal load balancer which takes your request and point it to one of the replicas using Round Robin algorithm.


Hi @eldeberde
Thank you for your message.

Do you know if there is any way to influence that internal load-balancer and tell it to forward the message to a particular container with a specific ip-address of the container?

Hi. I don’ t know any way.

One possible solution is use other load balancer like https://traefik.io/ and register your replicas when it starts.
It has an API where your containers can register itself, and them you can play with that.

I think you can also use haproxy