Docker Compose 应用程序会自动绑定到相同的端口,但Compose 未启动。

huangapple go评论48阅读模式
英文:

Docker Compose apps automatically bind to same port but compose doesn't start

问题

I have two copies of the same application that I want to deploy in a cluster and I'm using docker-compose for now. This application listens to port 8000 and if the port is busy it increments the port number until it finds a free port (8001, 8002 etc). These ports are used for the apps to communicate with each other, i.e. app1 will listen to 8000 and talk to app2 in 8001.

Since I'm deploying these using docker-compose, each container gets their own IP address. This means that both apps will bind to port 8000 (since it's not busy from their perspective). Knowing this, I'm trying to map those ports in the docker-compose.yml file as follows

version: "3.9"
  services:
    app1:
      container_name: app1
      image: app:latest
      ports:
        - 8000:8000
    app2:
      container_name: app2
      image: app:latest
      ports:
        - 8000:8001

However, when I try to docker-compose up this thing it fails with the following error

bind for 0.0.0.0:8000 failed: port is already allocated

i.e. it can't bind the same container (internal) port even though they are from different containers and are mapped to different host ports. Both app1 and app2 will choose port 8000 because from their perspective is free but I'm unable to map it externally. How can I solve this?

My preference would be for each app to bind to a different port i.e. the whole compose setup should share the same ports even though containers have different IP addresses.

NOTE: Changing the port selection code is not an option, this is a third party library and frankly it's a regular pattern to use.

英文:

I have two copies of the same application that I want to deploy in a cluster and I'm using docker-compose for now. This application listens to port 8000 and if the port is busy it increments the port number until it finds a free port (8001, 8002 etc). These ports are used for the apps to communicate with each other, i.e. app1 will listen to 8000 and talk to app2 in 8001.

Since I'm deploying these using docker-compose, each container gets their own IP address. This means that both apps will bind to port 8000 (since it's not busy from their perspective). Knowing this, I'm trying to map those ports in the docker-compose.yml file as follows

version: "3.9"
  services:
    app1:
      container_name: app1
      image: app:latest
      ports:
        - 8000:8000
    app2:
      container_name: app2
      image: app:latest
      ports:
        - 8000:8001

However, when I try to docker-compose up this thing it fails with the following error
> bind for 0.0.0.0:8000 failed: port is already allocated

i.e. it can't bind the same container (internal) port even though they are from different containers and are mapped to different host ports. Both app1 and app2 will choose port 8000 because from their perspective is free but I'm unable to map it externally. How can I solve this?

My preference would be for each app to bind to a different port i.e. the whole compose setup should share the same ports even though containers have different IP adresses.

NOTE: Changing the port selection code is not an option, this is a third party library and frankly it's a regular pattern to use.

答案1

得分: 0

I think you have swapped the ports around. The first port is the host port and the second port is the internal container port.

Try

version: "3.9"
  services:
    app1:
      container_name: app1
      image: app:latest
      ports:
        - 8000:8000
    app2:
      container_name: app2
      image: app:latest
      ports:
        - 8001:8000

That will map the containers to host ports 8000 and 8001.

英文:

I think you have swapped the ports around. The first port is the host port and the second port is the internal container port.

Try

version: "3.9"
  services:
    app1:
      container_name: app1
      image: app:latest
      ports:
        - 8000:8000
    app2:
      container_name: app2
      image: app:latest
      ports:
        - 8001:8000

That will map the containers to host ports 8000 and 8001.

huangapple
  • 本文由 发表于 2023年4月13日 23:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定