I defined a service in docker-compose.yml in the following way:
version : "3"
services:
web:
image: nginx
ports:
- 8080-8082:80
When I try to start three instances of this service with the command
docker-compose up -d --scale web=5 -d
one instance is started, but when docker-compose tries to start the other instances, they produce the error message
ERROR: for nginxtest_web_1 Cannot start service web: Ports are not available: listen tcp 0.0.0.0:8080: bind: Only one usage of each socket address (protocol/network address/port)
is normally permitted.
Sometimes two instance are started successfully and only one instance fails. The runtime behavior seems to be quite random. Obviously, docker-compose assigns the same host port to multiple nginx instances. When I repeat the above command two more times all instances get started with different host ports assigned. This error also occurs with other docker images. When I omit the host port range in the docker-compose.yml file different random host ports are assigned and everything works as expected.
I use docker version 19.03.12 and docker-compose 1.29.0 on Windows 10 (WSL 2).
Is this a docker-compose bug? Is there any workaround?
Update: Of course I only scale to 3 instances (the number of ports in my host port range)
docker-compose up -d --scale web=3 -d