Repository navigation
Port conflict with multiple "host:<port range>:port" services #7188
Description
Description of the issue
If a compose file defines multiple services that share an overlapping port range, a port conflict occurs when docker-compose up is executed. This behavior started to happen for me when upgrading to Docker Desktop 2.2.0 (Stable) for Mac. The version I was previously running (I believe 2.1.5) was able to select distinct ports for the two services without conflicting. Here is a POC Docker Compose file:
version: '2.1'
services:
postgres-foo:
image: postgres
ports:
- "127.0.0.1:32768-61000:5432"
postgres-bar:
image: postgres
ports:
- "127.0.0.1:32768-61000:5432"
Context information (for bug reports)
Output of docker-compose version
docker-compose version 1.25.2, build 698e2846
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019
Output of docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
Output of docker-compose config
(Make sure to add the relevant -f and other flags)
services:
postgres-bar:
image: postgres
ports:
- 127.0.0.1:32768-61000:5432/tcp
postgres-foo:
image: postgres
ports:
- 127.0.0.1:32768-61000:5432/tcp
version: '2.1'
Steps to reproduce the issue
docker-compose up
Observed result
Services fail to start and a port conflict error occurs
Expected result
Services start up. An available port within the overlapping range is selected for each service.
Stacktrace / full error message
docker-compose up
Starting docker-bug-poc_postgres-foo_1 ...
Starting docker-bug-poc_postgres-bar_1 ... error
Starting docker-bug-poc_postgres-foo_1 ... error
ERROR: for docker-bug-poc_postgres-bar_1 Cannot start service postgres-bar: Ports are not available: listen tcp 127.0.0.1:32768: bind: address already in use
ERROR: for docker-bug-poc_postgres-foo_1 Cannot start service postgres-foo: Ports are not available: listen tcp 127.0.0.1:37587: socket: too many open files
ERROR: for postgres-bar Cannot start service postgres-bar: Ports are not available: listen tcp 127.0.0.1:32768: bind: address already in use
ERROR: for postgres-foo Cannot start service postgres-foo: Ports are not available: listen tcp 127.0.0.1:37587: socket: too many open files
ERROR: Encountered errors while bringing up the project.
Additional information
I uploaded diagnostic information with the ID 37C41A27-71E7-4431-AFBE-5DA0EEC74A3C/20200126225028
Activity
cosmincrihan commentedon Jan 31, 2020
I am experiencing a similar issue that may come from the same cause.
The context is the same: upgrading to Docker Desktop 2.2.0 on MacOS breaks the port publishing when trying to run an NFS server container.
To reproduce:
The error shown:
I have already taken down every component related to NFS on the Mac itself, and nothing is occupying that port.
The output of
sudo lsof -iTCP -sTCP:LISTEN -n -P:Just downgrading to Docker Desktop 2.1.x solves everything.
Does anybody have any idea for a workaround?
thaJeztah commentedon Jan 31, 2020
Wondering how this would've worked before, as it wouldn't be possible to have two processes listening on the same port 🤔
cosmincrihan commentedon Jan 31, 2020
@thaJeztah should I file a separate issue, as it is not directly related to Docker compose?
ravishaheshan commentedon Feb 3, 2020
Also when you add scale and run the command multiple times, it will create more containers than the "scale" value. For example when I ran it with --scale=3 for the first time it creates one container successfully and two containers fail with port conflicts. Second time 2 containers success 1 fails, 3rd time 2 success 1 fails. when I execute
docker psit shows 5 containers.Note: I also have port range in my compose file. Before the 2.2.0 update It worked perfectly and created 3 containers and assigned ports within the given range.
MartinLyne commentedon May 20, 2020
Any news on this? After upgrading docker on mac to > 2.0 my compose script using port ranges on scalable service also fails. It works perfectly on docker < 2.0.
weimingwill commentedon Aug 29, 2020
@MartinLyne Did you find any solutions for this?
head1328 commentedon Jan 22, 2021
Same problem on MacOS with Docker Desktop v3.1.0 (51484) with the following docker-compose.yml:
First call fails, second call works:
So I tried:
docker-compose up -d kafka && docker-compose up -d --scale kafka=3 --scale zookeeper=2 kafkaand this works as well. It seems there is a problem with initial start & scaling and the same time.jheinzel commentedon Apr 29, 2021
The error occurs whenever a services which exposes a host port range is scaled, e. g.
Running 3 instances of this services with
results in a port conflict. Running this command three times resolves the problem.
I use docker version 19.03.12 and docker-compose 1.29.0 on Windows 10 (WSL 2).
mac89 commentedon Jun 17, 2021
Still occurs on docker version v20.10.7, docker-compose 1.29.2 on Windows 10 (WSL 2).
andriusvolkovas82 commentedon Jul 12, 2021
Same issue with latest versions in Win 10 (WSL 2)
mishrole commentedon Aug 10, 2021
Similar issue on Docker v20.10.7 and docker-compose v2.0.0 on Windows 10 (WSL 2).
Compose File
version: "3.8" services: app: image: mishroleapp environment: MONGO_URL: "mongodb://mongodb:27017/test" depends_on: - mongodb ports: - "3000-3001:3000" mongodb: image: mongodocker-compose updocker-compose pscarlocorradini commentedon Oct 12, 2021
Updates? 😪
dr3x commentedon Nov 18, 2021
Same issue pulling down the most recent Confluent platform binaries:
https://docs.confluent.io/platform/current/quickstart/ce-docker-quickstart.html
curl --silent --output docker-compose.yml
https://raw.githubusercontent.com/confluentinc/cp-all-in-one/7.0.0-post/cp-all-in-one/docker-compose.yml
docker-compose up -d
ERROR: for zookeeper Cannot start service zookeeper: Ports are not available: listen tcp 0.0.0.0:2181: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
Issue happens with both Docker Desktop 3.0.0 / 4.2.0
Update
After upgrading and rebooting machine, was able to launch the instance. So may be a timing issue with initial startup.
Here's a snippet of the yaml with the definitions:
`
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.0.0
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-server:7.0.0
hostname: broker
container_name: broker
depends_on:
- zookeeper
ports:
- "9092:9092"
- "9101:9101"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
`
13 remaining items
jbvsmo commentedon Sep 25, 2023
Hi any news on this issue? There are no good workarounds for us
felipevcc commentedon Nov 24, 2023
Hi. Same problem, anything new about this?
ndeloof commentedon Nov 24, 2023
8000-8010:80syntax already tells engine to select a random available port within the 8000-8010 rangesince #10067 (v2.14.1) we start containers sequentially to workaround race condition in engine selecting such a port.
Can you please confirm this issue persists with a recent release ?
felipevcc commentedon Nov 24, 2023
I am using version 2.23.0 and the problem persists
salmonvoice commentedon Feb 1, 2024
using Docker Desktop 4.26.1 - same problem.
Any updates on that?
ErjanGavalji commentedon Feb 6, 2024
Hello.
This might be related: we experience a
port is already allocatederror with a single port when using two configuration files.I described a simple repro here:
[BUG] Regression: array items[0,1] must be unique starting from 2.24.1 #11371
ndeloof commentedon Feb 6, 2024
@ErjanGavalji thanks for reporting, unrelated to original issue AFAICT, this is a parsing error fixed by compose-spec/compose-go#565. Any reason you use
extendsin your secondary file if this one is used as an override ?ErjanGavalji commentedon Feb 6, 2024
@ndeloof, I hope I got your question properly.
This is an attempt to provide the simplest repro, hence the service in the secondary.yml empty. In the full scenario we have volumes and env variables declared. We do not override the port or declare other ports, though.
ndeloof commentedon Feb 6, 2024
Sure, was just wondering this was revealing some unknown use-case. But to confirm, if your
secondary.yamlis used to tweak (aka "override") the service declared in themain.yaml, you don't needextends: compose will merge the original definition with additional attributes.ErjanGavalji commentedon Feb 6, 2024
@ndeloof Thanks for the tip, had missed that!
🥇
github-actions commentedon Mar 16, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.