英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论