skip to Main Content

docker compose returns port is already allocated if deploy.replicas is set to greater than 1.

I am running docker compose on Windows using WSL.

docker command:

docker compose up

docker file (based on the example from Docker web site ):

version: '3.8'

services:
  frontend:
    image: yeasy/simple-web:latest
    ports:
      - "8080:80"
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: vip

output:

[+] Running 3/3
 ⠿ Network test_default       Created                                                                                          0.0s
 ⠿ Container test-frontend-2  Created                                                                                          0.1s
 ⠿ Container test-frontend-1  Created                                                                                          0.1s
Attaching to test-frontend-1, test-frontend-2
Error response from daemon: driver failed programming external connectivity on endpoint test-frontend-1 (ecfbee8bdfb96ec2f381257c719fbd617477ccd4b528b3b60d9c7de6463a56cb): Bind for 0.0.0.0:8080 failed: port is already allocated

I expected that docker compose would allow this configuration and would create a VIP as described by Docker website.

Setting replicas: 1 allows you to run the command without an issue and access the website with curl http://localhost:8080.

I am aware of a workaround with Nginx as a load balancer, for example from @vinodkrane.

I wonder if it should be possible to use built-in Docker capabilities as described in the Docker documentation.

My environment:

  • Windows: Windows 10 Enterprise, OS build 19045.3208
  • WSL version: 1.2.5.0
  • Ubuntu 20.04.6 LTS
  • Docker version 24.0.5, build ced0996

2

Answers


  1. Chosen as BEST ANSWER

    TLDR: endpoint_mode is not supported in Docker Compose.

    Thanks to bmitch and Guillaume Lours replies from Docker Community slack:

    Compose doesn't add a load balancer in front of the port. Think of it as a front end to docker run. However swarm mode would do that.

    The v3 compose file, the deploy section, and VIP endpoint, are all added for swarm mode. The functionality has been backported to docker compose where possible, but not all features will be possible without an orchestrator.

    The other option is to add a traeffik or nginx service to redirect traffic to your replicas.


  2. A simple solution is to actually NOT use port mapping this way, but reach your container using the docker service (in this case specified by frontend), docker will round-robin the requests to each replica.

    However, you can do different port bindings by doing:

    ports:
          - "8080-8085:80"
    

    this will allow more ports (8080-8085) to be mapped to port 80 inside each replica.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search