Compose: Docker-compose up failing because "port is already allocated"

Created on 22 Jun 2017  ·  64Comments  ·  Source: docker/compose

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

Most helpful comment

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>

All 64 comments

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 :
selection_055

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:

  1. System Preferences
  2. Search for mysql
  3. Press the Stop MySQL Server button

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:
      - 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

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)
grafik

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
grafik

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?

Thanks, @lorenzo-pasa ! That was exactly my problem and your solutions worked. I tried a lot of stuff to solve this with no result. Thank you, again!

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

For me the issue was another docker container was still running in the background from a different project.

I fixed by running:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

I have a same issue as @maritaria mentioned about.

@freesoft It's probably best to open a new issue for this.

I tried everything here but nothing seems to work then i did this:

sudo lsof -i -P -n | grep 5432

kill all the processes
sudo kill

then it worked for me.

Had the same issue with

 Version:           18.09.3
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        774a1f4
 Built:             Thu Feb 28 06:53:11 2019
 OS/Arch:           linux/amd64
 Experimental:      false

One of the docker-proxy processes was hanging, so killing it and systemctl restart docker solved the issue.

I ran docker system prune (be cautious with this command) and then restarted docker, it worked for me.

docker network prune seems to resolve the issue for me

sudo lsof -i -P -n | grep 5432

worked for me ec2 linux instance

sudo lsof -i -P -n | grep

works for me but do i have to keep killing the processes now and then ?

In my case, I had an already running instance of node.js on local with same port.

When I stopped it, docker-compose up --build started to work!

in my case, I restart S.O

@Rub21 's

sudo lsof -i -P -n | grep <port number>

Reviled the using program to be docker-pr.

After updating my search keywords accordingly, I ran into this.
That solved my problem of not being able to deploy services using ports that are not suppose to be in allocated, but are anyway.

Thanks.

BTW:
In my case, I would believe the source for my error was me doing a hard-shutdown while executing docker network prune.
I guess the cli was not able to finish the task, therefore I had all those "non-existing" leftovers in my docker/network path.

In my case this worked https://github.com/docker/for-mac/issues/205#issuecomment-250856158

On linux users have reported that after remove local-kv.db with all containers stoped the problem gets solved.

File is in /var/lib/docker/network/files/local-kv.db

kill -9 $(sudo lsof -i -P -n | grep 8888 | awk '{print $2}') did the trick for me

unfortunately, trick with "kill -9.." doesn't work.
got zombies in place of docker-proxy process:

docker run -it -p 50000:50000 hello-world
docker: Error response from daemon: driver failed programming external connectivity on endpoint musing_dijkstra (2afd0e53bb80223bc07650e59bd16b43c6d971828a589db6f43dfa50a7a1ab42): Bind for 0.0.0.0:50000 failed: port is already allocated.

docker run -it -p 80:80 hello-world
docker: Error response from daemon: driver failed programming external connectivity on endpoint cocky_goldberg (8dab653c7575c298efea1b808ef2ff16f19bb6c23f51612787e19a434ceeb2ff): Bind for 0.0.0.0:80 failed: port is already allocated.

top | grep zombie
Tasks: 319 total, 1 running, 181 sleeping, 0 stopped, 2 zombie

uname -r
4.14.35-1844.4.5.el7uek.x86_64

@lorenzo-pasa I came across same problem for consul service and your trick worked for me. Thanks for the solution.

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

This solution by @joseph-luketelo (thanks!) worked on MacOS Mojave. Didn't even need to "remove any existing data...". Just running the first two commands solved it.

  • deleting /var/lib/docker/network/files/local-kv.db (with everything off and down) did not work
  • pruning networks did not work
  • deleting all volumes and containers did not work
  • doing all of the above together did not work
  • killing docker-proxy instances occupying the ports did not work (nothing shows in netstat output for that port any longer, but still this error message).
  • If I do the above then restart docker service, docker-compose up just hangs indefinitely.

This has been happening to me on a close-to-weekly basis for a month or so now and so far the only thing which does the trick is re-installing docker every time. Why is this closed? Even if the above steps took place, this is clearly a bug of some variety...

all the mentioned solutions didn't work for me.

This is worked:

sudo rm -rf /var/lib/docker
sudo service docker restart # to re-create the db again

I also tried to reinstall docker and docker-compose, but I don't have any idea if this step helps. It didn't work until I did the mentioned steps above.

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 (this also solve my problem)

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

disabling port binding also solved my problem

I was getting the same error after re-building my docker config.

ハ➜ docker-compose up
Screen Shot 2020-02-03 at 10 38 30 AM

I tried:
ハ➜ docker-compose down
but still faced the same issue....

ef18b3f0f2864b22e102): Bind for 0.0.0.0:10002 failed: port is already allocated

ERROR: for db  Cannot start service db: driver failed programming external connectivity on endpoint eeschenck_db_1 (a7caadb27e327b901df83bb42fd19bd704224aa03a75ef18b3f0f2864b22e102): Bind for 0.0.0.0:10002 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.

I then tried restarting the Docker application and and exited out my 'Visual Studio Code' IDE which has a docker extension...

Screen Shot 2020-02-03 at 10 47 23 AM

I then tried once more and tried to netstat the port..
ハ➜ netstat -n | grep 10002
Nothing found for that port... Should have tried this before.

ハ➜ docker-compose up

Screen Shot 2020-02-03 at 10 52 16 AM

Looks like Visual Studio Code Docker extension might be the issue for me.

Can you share your docker-compose file config
u need to remove port mirroring

Worked for me

docker-compose down
docker network prune
sudo service docker restart
docker-compose up

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

@lorenzo-pasa Thank you, this solution worked for me.

Simply restarting the docker desktop daemon on Mac did the trick for me (Docker Desktop 2.2.0.4).
Seems like something misbehaved at startup.

docker stop $(docker ps -a -q)
Run this command before attempting other fixed like -8080:8080 to -8080 or even docker rm$(...)

I've reset my machine and it's start to work again. Probably not an accurate solution but worked

If docker ps output nothing and docker-compose down/up didn't help then check if the port is used with this command:
sudo lsof -i -P -n | grep 9200

If it is, then just run:
sudo service docker restart

have same issue. I do :

docker-compose down
docker-compose build
docker-compose up -d

but sometimes error happens

ERROR: for xxxxxx  Cannot start service web: driver failed programming external connectivity on endpoint xxxxx (xxxx): Bind for 0.0.0.0:22000 failed: port is already allocated

I cant restart docker because another containers runs there

@mogadanez as proposed by @KorossGame you can try to find which process is already using this port.

Have a good day,

@pandaatrail
its docker, but its not help, I cant restart entire docker.

@mogadanez If you can not restart docker - check what is using a specific port.
sudo lsof -i -P -n | grep 9200
Also check if docker containers are not using the same input ports with:
docker ps

It simply doesn't work. lsof doesn't show any ports being used. Removing images and recreating the whole stack doesn't work. Restarting docker doesn't work. Only thing that worked was rebooting the system. Good luck.

Hello,
you just need to verify if there is another service use the same port,

if you are using Windows you need to run the following command:

  1. docker-compose down
  2. netstat -ano | findstr :port ( as example netstat -ano | findstr :18080)
  3. taskkill /pid the_pid_of_the_port /f
  4. docker-compose up

And that's it 😄

@olfamoussaoui I tried all of that but for mac. No solution works.

@gabrielhpugliese have you tried to see if the port is in use ?

@olfamoussaoui I tried all of that but for mac. No solution works.

I had similar compose behaviour on Ubuntu. It was side effect, based on wrong iptables settings. Port translation should be checked and released manually. Don't remember corect syntax for MacOS. It shouldn't be so hard to find it by rosette stone.

I don't understand why I need all of that and the problem is away tho. It
is not a new setup I am building and I didn't create new
containers/services. I just re-created my server container with up -d
--build. Really weird.

vitaly-zverev notifications@github.com schrieb am Mi., 19. Aug. 2020,
17:27:

@olfamoussaoui https://github.com/olfamoussaoui I tried all of that but
for mac. No solution works.

I had similar compose behaviour on Ubuntu. It was side effect, based on
wrong iptables settings. Port translation should be checked and released
manually. Don't remember corect syntax for MacOS. It shouldn't be so hard
to find it by rosette stone.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/docker/compose/issues/4950#issuecomment-676495723,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJR3UTBMGLV2ECW5GCNQDTSBPVNXANCNFSM4DQLFGPQ
.

I don't understand why I need all of that and the problem is away tho. It is not a new setup I am building and I didn't create new containers/services. I just re-created my server container with up -d --build. Really weird. vitaly-zverev notifications@github.com schrieb am Mi., 19. Aug. 2020, 17:27:

@olfamoussaoui https://github.com/olfamoussaoui I tried all of that but for mac. No solution works. I had similar compose behaviour on Ubuntu. It was side effect, based on wrong iptables settings. Port translation should be checked and released manually. Don't remember corect syntax for MacOS. It shouldn't be so hard to find it by rosette stone. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#4950 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJR3UTBMGLV2ECW5GCNQDTSBPVNXANCNFSM4DQLFGPQ .

Ummm, Ok, as I understand their root cause should be analysed at place where docker integrated with network translation. It's OS dependent and has many implications,
just as any integration bug.

Have been a lot of good answers. I found the issue was another app that i had installed (Sky Go) on Catalina which somehow was causing a port conflict even though it was closed.
It's worth checking such apps (inc Skype) arent causing the issue.

I also faced the same issue. I am using the port 8080 and every time when I tried to run docker it showed port 8080 already in use. Then I checked if any other application blocked the port but didn't find anything.
Then followed the following steps.

  • docker-compose down (to stop my container)
  • docker ps (to check any container is running in my case I found hasura graphql was running )
  • docker container stop [Container_ID]
  • docker container rm [Container_ID] (in my case I also removed the container)
    and After following the above process my docker worked

If you're on WSL2, check to see if Hyper-V is enabled or not. If it isn't, just enable it, that'll do.

OS restart helped me

Was this page helpful?
0 / 5 - 0 ratings