Unable to scale service for multiple containers when binding host port in docker-compose.yml file

Microservice runs on a single container. I associated this service with the host port 8082and started this microservice with docker-compose. Now I want to scale it to 3 containers using the command docker-compose scale, but this gives me the following error:

ERROR: for 2 it was not possible to create the composetest_nginx_2 endpoint on the network competest_default: Bind for 0.0.0.0:8082 failed: the port has already been allocated

ERROR: for 3 it was not possible to create the composetest_nginx_3 endpoint on the competest_default network: Bind for 0.0.0.0:8082 failed: the port has already been allocated

What will be the solution for this?

+6
source share
3 answers

You do not have to bind 3 container ports to the same port on the host.

What you can do is configure:

  • 1 load balancing container (XXXX communication container container on port 8082)

  • 3 service containers (set ports for load balancing)

bindbinds only 1 port to the host. And exposeonly provides ports to the associated container so that they can be from multiple containers.

Link:

https://www.brianchristner.io/how-to-scale-a-docker-container-with-docker-compose/

https://github.com/vegasbrianc/docker-compose-demo/blob/master/docker-compose.yml

+10
source

if you use Nginx or similar, then you can use the command docker-gento automatically update configurations in Nginx.

. : https://deployeveryday.com/2016/09/28/composing-docker-environments-scale.html

0

A good option is to give a range of ports, not a specific port in docker-compose ...

ports:
 - 8081+:8081

Thus, this will allow you to scale your service without going into the problem you mentioned (the port is already allocated ), as the host port will be different for each scalable instance, and Docker will handle this ...

0
source

All Articles