Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker-compose up failing because "port is already allocated" #4950

Closed
ghost opened this issue Jun 22, 2017 · 89 comments
Closed

Docker-compose up failing because "port is already allocated" #4950

ghost opened this issue Jun 22, 2017 · 89 comments

Comments

@ghost
Copy link

ghost commented Jun 22, 2017

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

@shin-
Copy link

shin- commented Jun 22, 2017

netstat | grep 5432?

@budnik
Copy link

budnik commented Jul 31, 2017

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?

@shin-
Copy link

shin- commented Jul 31, 2017

@budnik Try running docker-compose down to clean up containers and networks, then up again and see if that fixes things.

@lorenzo-pasa
Copy link

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

@hackermanmx
Copy link

Solution above was what I needed, thanks a lot @lorenzo-pasa !

@shin- shin- closed this as completed Jan 24, 2018
@weblancaster
Copy link

weblancaster commented Jan 29, 2018

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

@realmhamdy
Copy link

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.

@Rub21
Copy link

Rub21 commented Jun 20, 2018

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>

@tiancaipipi110
Copy link

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.

@PJLindsay
Copy link

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.

@timendez
Copy link

timendez commented Jul 23, 2018

@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

@KazibweStephen
Copy link

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.

@joseph-luketelo
Copy link

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

@otheus
Copy link

otheus commented Sep 5, 2018

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

@wonmaungthein
Copy link

wonmaungthein commented Oct 21, 2018

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

@straurob
Copy link

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

@maritaria
Copy link

maritaria commented Dec 7, 2018

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.

@Devether15
Copy link

The solution of @lorenzo-pasa worked for me! I'm using docker-compose and nginx, running ubuntu OS, thanks dude.

@Ezwen
Copy link

Ezwen commented Dec 23, 2018

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.

@drauscher
Copy link

drauscher commented Jan 11, 2019

@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?

fureigh added a commit to 18F/nsf that referenced this issue Jan 16, 2019
Otherwise `docker-compose up` throws an error about the port already
being allocated.

Per docker/compose#4950 (comment).
@marcosleonel
Copy link

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

@kmturley
Copy link

kmturley commented Feb 13, 2019

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)

@freesoft
Copy link

I have a same issue as @maritaria mentioned about.

@shin-
Copy link

shin- commented Feb 15, 2019

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

@abhishekpalavancha
Copy link

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.

@mikaelhg
Copy link

mikaelhg commented Mar 4, 2019

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.

@Shirish-Singh
Copy link

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

@asencis
Copy link

asencis commented Feb 8, 2021

One potential solution for this is you don't always need to have an external port binding in your docker-compose.yml file:

ports:
    - "5432"

Should be sufficient for postgres, as you only really want container <=> container interactions for development. (You shouldn't be shipping your DB in a container tho for production environments).

@Chocofede
Copy link

I don't know if it will help someone, but nothing posted above helped me: All docker containers were stopped, no port in use in the system... I was astonished, and then, I found this in SO that did the trick:

https://stackoverflow.com/questions/24993704/docker-error-cannot-start-container-port-has-already-been-allocated

The issue is about restarting the docker service. As stupid as running:

sudo service docker restart

And then, I was able to run the docker-compose back and all worked.

@jonasaud93
Copy link

Worked for me

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

Thanks! Pruning the network after docker-compose down was the thing that I missed by trying the previous answers.

@plapa
Copy link

plapa commented Mar 15, 2021

Yup

I don't know if it will help someone, but nothing posted above helped me: All docker containers were stopped, no port in use in the system... I was astonished, and then, I found this in SO that did the trick:

https://stackoverflow.com/questions/24993704/docker-error-cannot-start-container-port-has-already-been-allocated

The issue is about restarting the docker service. As stupid as running:

sudo service docker restart

And then, I was able to run the docker-compose back and all worked.

Yup, I can also confirm that this solved my issue!

@CsabaNa
Copy link

CsabaNa commented Mar 26, 2021

@drauscher

@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?

Have you had a solution for this problem? I just run into it the xdebug was working fine until yesterday evening then in storm:
Can't start listening for connections from 'xdebug': Port 21003 is busy
and restart the containers:
Cannot start service backend: Ports are not available: listen tcp 0.0.0.0:21003: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
literally i changed nothing one run worked the other started to fail.

compose file:

backend:
image: backend
links:
- "bc2db"
depends_on:
- bc2db
ports:
- "21443:443"
- "2180:80"
- "21000:8000"
- "21003:9003"
...
gateway:
image: gateway
links:
- "bc2db"
depends_on:
- bc2db
ports:
- "23443:443"
- "2380:80"
- "23003:9003"

All of which are working except the xdebug. All the solution what were mentioned here have been tried none of them works for me. The confusion is this was working in one secundes and suddenly stopped everything in the next one.

@al-yakubovich
Copy link

al-yakubovich commented Mar 30, 2021

In my case I had this problem because there were two replicas in my compose file:

version: '3'
services:
    web:
        build: .
        ports:
            - "5000:5000"
        deploy:
            replicas: 2

When I've changed a number of replicas to one, docker-compose up started working

@chris-aeviator
Copy link

I don't see why this issue has been closed. Pruning networks or forcefully restarting docker daemon is a workaround for a bug - it's not that the bug went away, it's just people embraced workarounds.

This happens quite often on multiple machines and looks like a bug, though not an easy to reproduce one

@JANHMS
Copy link

JANHMS commented May 24, 2021

I had the same problem two times now....
I tried the solutions of above, but none seemed to help me. I restarted and it did not work. Hence I shut my MAC fully down and manually restarted it. Finally docker-compose up worked without any problems.
-> Sometimes restarting is a simple but effective solution :-D

@jis0324
Copy link

jis0324 commented May 24, 2021

I don't see why this issue has been closed. Pruning networks or forcefully restarting docker daemon is a workaround for a bug - it's not that the bug went away, it's just people embraced workarounds.

This happens quite often on multiple machines and looks like a bug, though not an easy to reproduce one

Yes, My thought also is same as you.
As we can see. many developers got this problem.
I think this problem has to solve as programmatically.

@yassinelachgar
Copy link

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)

Doing this, then restarting docker engine & docker-compose up worked for me.

@otheus
Copy link

otheus commented Jul 29, 2021

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)

Doing this, then restarting docker engine & docker-compose up worked for me.

That's not really a solution in a production environment :-/

@yassinelachgar
Copy link

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)

Doing this, then restarting docker engine & docker-compose up worked for me.

That's not really a solution in a production environment :-/

Fortunately I've never encountered this error on my production environment, only on my local dev env (macos).

@jaime2505
Copy link

Thanks @olfamoussaoui, that work for me

if you are using Windows you need to run the following command:
docker-compose down
netstat -ano | findstr :port ( as example netstat -ano | findstr :18080)
taskkill /pid the_pid_of_the_port /f
docker-compose up
And that's it 😄

@halindraprakoso
Copy link

halindraprakoso commented Sep 1, 2021

what worked for me:

docker-compose down
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
launchctl list | grep docker
launchctl stop com.docker.docker.<number>

@nicu-chiciuc
Copy link

so I'm not sure if this is a fluke, but for some reason after I quit Spotify it seemed to work. I've tried most of the solutions above before that

@MikeB2019x
Copy link

docker-compose down docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) launchctl list | grep docker launchctl stop com.docker.docker.<number>

Yes, this works but make sure you know what 'launchctl' does (it stops docker but that doesn't clear up the problem per se as starting/stopping docker alone did not stop the error from reappearing). The important part is the 'docker rm' because it seems to remove the port allocation that docker is perceiving (likely deleting some reference in docker). For example, this 'docker-compose.yml' of mine (below) suddenly stopped launching with the error that port 5665 was already allocated even with no containers running in my system. docker compose down/stop/rm seemed to clear whatever was making docker think the port was already allocated.

version: "3"
services:
  postgres:
    image: postgres:13
    container_name: graphstruct_db
    volumes:
      - ./graphstruct:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=graphstruct_data
    ports: 
      - 5665:5432
volumes:
  graphstruct:

@GCSBOSS
Copy link

GCSBOSS commented Jun 27, 2022

I'm having this issue in Docker Compose version v2.6.0

Mentioned in #9117

@lfabl
Copy link

lfabl commented Jul 28, 2022

If you use windows and if non of them worked, Delete your relevant container in Docker. Then right click on docker tray icon at the bottom right of the screen and close docker. Try run again.

@wilokecom
Copy link

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

You saved my day

@jinek
Copy link

jinek commented Jul 10, 2023

I've got a port bound to the neighbor container! not the one which was provided in docker-compose file.
Thus, one of the containers could not start with "Host is already in use" / "port is already allocated".
Similar issue is described here https://bytefreaks.net/applications/docker-warning-host-is-already-in-use-by-another-container

But, in my case, another container is exposed silently, and it even does not show that in docker container ls !
DockerComposeBug

@dxvargas
Copy link

dxvargas commented Dec 19, 2023

My problem was that I had a file called docker-compose.override.yml that was being loaded even with the command docker-compose up -d;. In the override I had the same service with the same port and hence this conflict.

@HopedForLuck
Copy link

so basically i deleted container and restarted docker and that helped...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests