How does Docker Swarm map ports twice?

I have a test setup of 3 nodes: 1 management and 2 client nodes. I have created my service with 4 instances:

docker service create --replicas 4 --name my-service --with-registry-auth --publish 18001:8081 <registry>/my-service:latest.

Running docker service ps my-service shows that one of the nodes is running two instances of my-service:

q5wm8d5z9fhn my-service.1 /my-service:latest ip-172-30-1-163 Running
tf430sb0qbac my-service.2 /my-service:latest ip-172-30-1-39 Running
vjt37c3lqsyk my-service.3 /my-service:latest ip-172-30-1-237 Running
jdlkcjwux7q5 my-service.4 /my-service:latest ip-172-30-1-163 Running

How is it possible to have two instances running on the same node (ip-172-30-1-163) on the same port (18001)?

EDIT
I’ve played around with this a bit more. I am running stefanscherer/whoami on my 3 test nodes:

docker service create --replicas 6 --name whoami --publish 8080:8080 stefanscherer/whoami

Each node runs two instances of the whoami service:

80442tkm2l0w whoami.1 stefanscherer/whoami:latest ip-172-30-1-39 Running
szvo6weq30cx whoami.2 stefanscherer/whoami:latest ip-172-30-1-163 Running
8d4cu9xmdutg whoami.3 stefanscherer/whoami:latest ip-172-30-1-237 Running
puw3zy8o6bhc whoami.4 stefanscherer/whoami:latest ip-172-30-1-39 Running
wctsdib4p0tc whoami.5 stefanscherer/whoami:latest ip-172-30-1-163 Running
o8vdbvsdfbtc whoami.6 stefanscherer/whoami:latest ip-172-30-1-237 Running

curl-ing the service shows responses from all 6 containers:

root@ip-172-30-1-163:~# curl localhost:8080
I'm 7f95240a4cf7 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 959c2a1a8aa1 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 334ec0db9083 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 64193a9dc94c running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm d1520e7fa1af running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm be87b77b29fd running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 7f95240a4cf7 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 959c2a1a8aa1 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 334ec0db9083 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 64193a9dc94c running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm d1520e7fa1af running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm be87b77b29fd running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 7f95240a4cf7 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 959c2a1a8aa1 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 334ec0db9083 running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 64193a9dc94c running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm d1520e7fa1af running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm be87b77b29fd running on linux/amd64
root@ip-172-30-1-163:~# curl localhost:8080
I'm 7f95240a4cf7 running on linux/amd64

sorry, I don’t know the answer, removed my previous bad post

1 Like

Hi,
I’ll star with some boring theory :slight_smile: you cannot have two instances of the same process listening on one port, so you can’t have two containers (processes) using the same port in the same host, that is not a Docker issue it’s just how it was designed in Unix.

When you create the service using the port 18001, you’re asking Docker Swarm to create a loadbalancer in your worker nodes using the router mesh, so the 18001 port is listening on each node and distributes the traffic to the instances running in your worker nodes.

If you’re asking how you can have two instances created on each node when you create a service, you can play with placement preferences and constraints but I don’t think you can say put two replicas on each node, you say put 4 replicas on my worker nodes or put one instance on each node (using global).

Cheers!

That doesn’t really explain my observations because it looks like two containers are running on the same node.

Found the same question on SO which lead me to the explanation here.