Micro service is running on one container. I have bound that service to Host port 8082
and launched this micro service using docker-compose
. Now I want to scale it to 3 container using docker-compose scale
command but it giving me following error-
ERROR: for 2 failed to create endpoint composetest_nginx_2 on.network composetest_default: Bind for 0.0.0.0:8082 failed: port is already allocated
ERROR: for 3 failed to create endpoint composetest_nginx_3 on.network composetest_default: Bind for 0.0.0.0:8082 failed: port is already allocated
What would be the solution for this?
You are not supposed to bind 3 containers' ports to the same port in host.
What you can do is setting up:
1 load balancer container (bind container's port XXXX to host's port 8082)
3 service containers (expose ports to load balancer)
bind
only binds 1 port to host. And expose
only exposes ports to the linked container so they can be from multiple containers.
Reference:
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
I solved it by passing a port range, with the correct syntax :P
ports:
- "9110-9120:3333"
if you are using Nginx or similar, then you can use docker-gen
command to auto-update configurations in Nginx.
See this for more info: https://deployeveryday.com/2016/09/28/composing-docker-environments-scale.html
the way to do it is to remove the source port. for example, change
ports:
- "5000:5000"
to
ports:
- "5000"
the downside is you don't know the range of the port number until the service started.
Good option is to give range port rather giving specific port in docker-compose...
ports:
- 8081+:8081
This way it will allow you to do scaling your service without getting into the issue you mentioned ( Port already allocated ) as Host port will different for each scaled instance & Docker will handle that for you...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.