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
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.