Docker Community Forums

Share and learn in the Docker community.

Any way to "scale" a port mapping using a scaling strategy?


(Davidmichaelkarr) #1

So if I have a service in a compose file using a port mapping, that’s useful if I have a single instance of the service. If I scale the service, that obviously won’t work, because it will try to map the host port multiple times.

Is there any way (or plan) to allow for a “port scaling strategy” or something, such that if you scale a service with a port mapping, it would use some sort of algorithm to map the container port to multiple host ports?


(Feliperoberto) #2

well, a simple workaroud is to use a combination of label and an affinity filter like below. But remember to apply that to all your services who expose the same port on your cluster.
#docker-compose.yml
version: '2’
services:
app:
image: 'nginx:latest’
ports:
- "80:80"
environment:
- "affinity:com.port!=eigthy"
labels:
- “com.port=eigthy”
#docker-compose up -d;
#docker-compose scale app=3


(Nguoi An Phu) #3

Hi,

I use your docker-compose.yml but get this error.

$ docker-compose scale app=3
[33mWARNING←[0m: The "app" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Creating and starting nginx_app_2 ...
Creating and starting nginx_app_3 ...
ERROR: for nginx_app_2  Cannot start service app: driver failed programming external connectivity on endpoint nginx_app_2 (7ae329a1d19a7c2cda5b1e6bcc020297106b6a3015847a9c37c2dcc6e7047f1d): Bind for 0.0.0.0:80 failed: port is already allocated
ERROR: for nginx_app_3  Cannot start service app: driver failed programming external connectivity on endpoint nginx_app_3 (c5719371b8ac3d3a1ed9e53c4d89800b6381629b1f8e8bdfb6d840091b0543e1): Bind for 0.0.0.0:80 failed: port is already allocated