Docker-compose up failing because "port is already allocated"
My docker container is able to successfully build but when I enter the command docker-compose build, the following error is returned:
Starting docker_etl_1 ...
Starting 1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1 ...
Starting 1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1
Starting 1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1 ... error
ERROR: for 1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1 Cannot start service
postgis: driver failed programming external connectivity on endpoint
1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1
(91464afbee8bf7212061797ec0f4c017a56cc3c30c9bdaf513127a6e6a4a5a52): Error starting
userland prStarting docker_etl_1 ... done
ERROR: for postgis Cannot start service postgis: driver failed programming external connectivity
on endpoint 1e5f56853e10_1e5f56853e10_1e5f56853e10_docker_postgis_1
(91464afbee8bf7212061797ec0f4c017a56cc3c30c9bdaf513127a6e6a4a5a52): Error starting
userland proxy: Bind for 0.0.0.0:5432 failed: port is already allocated
Here is my docker-compose.yaml
version: '2'
services:
postgis:
build: ./postgis
volumes:
- ../src/main/sql:/sql
ports:
- "5432:5432"
etl:
build: ./etl
volumes:
- ..:/national-voter-file
entrypoint:
- python3
- /national-voter-file/load/loader.py
and here is the Dockerfile:
FROM mdillon/postgis:9.5
ENV POSTGRES_DB VOTER
RUN mkdir /sql
COPY ./dockerResources/z-init-db.sh /docker-entrypoint-initdb.d/
EXPOSE 5432
I don't believe I have another container running so I'm confused by the message Bind for 0.0.0.0:5432 failed: port is already allocated
netstat | grep 5432?
Hi @shin- ,
I just did a hard reset on my machine and I get:
$ docker-compose up
_collections.so could not be extracted!
After that I have the same issue as posted by @patrickconnors :

Any ideas?
@budnik Try running docker-compose down to clean up containers and networks, then up again and see if that fixes things.
I ran into the same issue today (with a postgres container), and despite having tried docker-compose down and then up again, the problem still persists.
Both docker-compose ps and docker ps show me an empty output.
I may have found a solution, though:
this is how my postgres service is defined
version: '2.1'
services:
postgres:
image: postgres:9.5.4
env_file:
- docker-compose.env
ports:
- 5432:5432
and in my case the fix was simply to disable the port binding, that is changing the last part as:
ports:
- 5432
Not sure if this is the right solution, nor if it can be generally applied to all use cases.
Might it be an issue with docker-compose itself ?
For reference:
$ docker-compose --version
docker-compose version 1.12.0, build b31ff33
Solution above was what I needed, thanks a lot @lorenzo-pasa !
I was having the same issue after updating my docker-compose to 3.3 version. @lorenzo-pasa solution work locally for me (still need to try in prod)
Below is part of my docker-compose for reference:
nginx:
image: nginx:1.12.2-alpine
volumes:
- .:/usr/share/nginx/app
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "80"
depends_on:
- web
I had this and also stuff like Cannot start service mysql: network 4b8e7a23e3724fd351e428be30cdf7a9294d142ac23959ca9092278fc81e96c1 not found. I resolved them by using --build --force-recreate flags for the up command.
This 👇 works for me!!
docker-compose down
docker rm -fv $(docker ps -aq)
sudo lsof -i -P -n | grep 5432
- For Mac
kill -9 <process id>
- Linux
sudo kill <process id>
ERROR: for iky_gateway Cannot start service iky_gateway: b'driver failed programming external connectivity on endpoint iky_gateway (47d83edbbc1568eae6d26f5e75931797b7e23c6528ff7cc8140f50811fb44922): Error starting userland proxy: Bind for 0.0.0.0:8080 failed: port is already allocated'
I've tried docker-compose down, didn't fix it.
Another solution that may be helpful to future readers is: double-check your docker-compose.yml file and check to make sure you don't have 2 items attempting to run on port 8080.
If you do have 2 items configured to run on port 8080 you can get "port already allocated" error message for the service/container that is not causing the problem - which will cause you confusion when you try and kill the service/container and get no resolution to the error message.
@Rub21's solution worked for me, except kill -9 {pid} kept restarting the process. This was because I had MySQL Server running. On OSX I had to:
- System Preferences
- Search for
mysql - Press the
Stop MySQL Serverbutton
I removed port binding and it worked for me, instead of doing mysql: ports: - 3306:3306 i had to change to mysql: ports: - 3306 and all was good.
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) Then remove any existing data e.g docker volume rm docker_db_data docker-compose up
Same problem here. I am using docker version 17.05.0-ce, build 8965be, with docker-compose 1.13.0 and 1.22.0. The compose-yml file is 3.2.
With apologies to @KazibweStephen , this is not a useful solution. You are telling docker-compose to pick an abritrary port for clients to connect to the container's mysql service. You might as well tell mysql not to listen to any port or just remove the port configuration altogether.
I have tried various solutions above, including rebooting the server. No other containers are running, and there are no conflicts in the yml file. I recall that this worked in a much older version of docker/compose. netstat -anp on the host shows nothing listening on the ports.
If I completely reomve the ports sections, the processes start up OK. I then use nsenter ... netstat to verify the container is listening in on the correct port (in its namespace). I then check with netstat to verify there is no conflict on the default namespace. I can then use a utility such as nc to listen on the same port.
I can see no reason vis-a-vis the documentation why the configuration does not work. The conclusion is there is something wrong with docker-compose's proxy setup.
Unbelievable. I had, in fact, a tiny error which caused the problem. My configuration needed two ports, and one of these had a typo, replicating the other. facepalm
netstat | grep 5432?
Hi, when I run this, I got this message..
98808d86b49cff5d stream 0 0 98808d86b6dd9be5 0 0 0 /tmp/.s.PGSQL.5432. Can you please help me what to do ? Thanks
and in my case the fix was simply to disable the port binding, that is changing the last part as:
ports: - 5432Not sure if this is the right solution, nor if it can be generally applied to all use cases.
Might it be an issue with
docker-composeitself ? For reference:$ docker-compose --version docker-compose version 1.12.0, build b31ff33
This fix worked for me.
docker-compose -version
docker-compose version 1.23.1, build b02f1306
I had a slightly different situation, posting here to record another case of this happening.
When using a docker-compose.yml and docker-compose.override.yml file, which both contain a port mapping, where the host port is the same for both mappings, but the container port differs, then this causes docker to try and allocate the host port twice.
docker-compose.yml:
ports:
- 8080:8080
docker-compose.override.yml:
ports:
- 8080:8181
Perhaps this is an issue on its own, creating overriding mappings results in duplicate binding attempts. This situation is specifically narly because nothing is listening on the port until you attempt to bring the containers online. Which fails and therefor shuts down the entire composition, which results in the port becoming un-allocated again.
The solution of @lorenzo-pasa worked for me! I'm using docker-compose and nginx, running ubuntu OS, thanks dude.
I am curious: why is this issue closed, while numerous people still seem to encounter a problem(including myself)? Is the official fix to never write (for instance) 8080:8080 and to always write instead simply 8080? Just to be clear this is not a complaint I am sincerely curious.
@Ezwen agree I tried out all of the suggestions posted above but still running in errors. I'm running PHPstorm and try to Xdebug my application in a dockerbox. When starting the my dockerbox i am using docker-compose up with a
ports: - "9001:9001"
which exposes the port correctly. (vpnkit.exe is a part of Docker)

I see that vpnkit.exe is listening to port 9001. So when I now try to start my Xdebug in PHPstorm it comes up with the error message

I don't know why I can't tell the PHPstorm debugger to listen to port 9001 while it is always used by vpnkit.exe when I start my docker container?