Docker - Bind for 0.0.0.0:4000 failed: port is already allocated
Asked Answered
D

27

300

I am using docker for the first time and I was trying to implement this - https://docs.docker.com/get-started/part2/#tag-the-image

At one stage I was trying to connect with localhost by this command -

$ curl http://localhost:4000

which showed this error-

curl: (7) Failed to connect to localhost port 4000: Connection refused

However, I have solved this by following code -

$ docker-machine ip default
$ curl http://192.168.99.100:4000

After that everything was going fine, but in the last part, I was trying to run the app by using following line according to the tutorial...

$ docker run -p 4000:80 anibar/get-started:part1

But, I got this error

C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: driver failed programming external connectivity on endpoint goofy_bohr (63f5691ef18ad6d6389ef52c56198389c7a627e5fa4a79133d6bbf13953a7c98): Bind for 0.0.0.0:4000 failed: port is already allocated.
Dilettantism answered 12/9, 2017 at 12:37 Comment(0)
E
465

You need to make sure that the previous container you launched is killed, before launching a new one that uses the same port.

docker container ls
docker rm -f <container-name>
Eulogize answered 12/9, 2017 at 13:17 Comment(5)
You can paste the CONTAINER ID from docker container ls command in <container-name> position.Safeguard
I am getting an empty list for docker container ls, and still the port is taken.Personable
Pro tip: docker ps does the same as docker container lsMallina
I restarted the docker service service docker restart and the port was still blocked. I pkilled all processes using that port and it was still blocked. Removing the container was the solution for me.Bountiful
docker container ls -a , maybe betterTranslunar
C
106

Paying tribute to IgorBeaz, you need to stop running the current container. For that you are going to know current CONTAINER ID:

$ docker container ls

You get something like:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
12a32e8928ef        friendlyhello       "python app.py"     51 seconds ago      Up 50 seconds       0.0.0.0:4000->80/tcp   romantic_tesla   

Then you stop the container by:

$ docker stop 12a32e8928ef

Finally you try to do what you wanted to do, for example:

$ docker run -p 4000:80 friendlyhello
Corso answered 6/10, 2017 at 10:17 Comment(2)
The first three characters of the container ID are sufficient to stop containers, so stopping the container is simpler as $ docker stop 12aIrishman
This should be the answer. I don't know why people are removing the container when you can just stop it to get access to that port.Illomened
R
93

I tried all the above answers, none of them worked, in my case even docker container ls doesn't show any container running. It looks like the problem is due to the fact that the docker proxy is still using ports although there are no containers running. In my case I was using ubuntu. Here's what I tried and got the problem solved, just run the following two commands:

sudo service docker stop
sudo rm -f /var/lib/docker/network/files/local-kv.db
Referent answered 12/7, 2020 at 5:56 Comment(6)
Keep in mind that deleting local-kv.db also deletes all user-defined networks.Gonium
On my system, this issue was resolved via systemctl restart docker. I did not need to run any other commands.Derange
how to do it on Mac?Sizar
@OlegAbrazhaev If you have Docker Desktop then just restart from thereDesertion
Lighten me as docker network prune, thanks!Contreras
In Docker Desktop for mac, i clicked on the Troubleshoot Menu (bug icon), then clicked on Restart Docker Desktop.Lula
W
43

I solved it this way:

First, I stopped all running containers:

docker-compose down

Then I executed a lsof command to find the process using the port (for me it was port 9000)

sudo lsof -i -P -n | grep 9000

Finally, I "killed" the process (in my case, it was a VSCode extension):

kill -9 <process id>
Wace answered 24/6, 2019 at 1:48 Comment(1)
Following the advice of yamenk's accepted answer showed no containers in use. This answer helped me to track down the process using the port. If this answer contained information for checking active docker contains as well then it should be the accepted answer. "port is already allocated" does not always mean "docker container is already running"Trela
V
43

The quick fix is ​​a just restart docker:

  1. sudo service docker stop
  2. sudo service docker start
Venegas answered 4/1, 2021 at 8:3 Comment(4)
This worked for me! I couldn't see the container anywhere so couldn't remove it.Brushwood
This worked for me as well. I'm blaming docker for windows...Harijan
Same happened for me, I even tried to stop all containers, but that didn't help.. (On debian bookworm proxmox vm)Radioactivate
This worked for me as well. lsof did not find any process using the portChi
C
20

Above two answers are correct but didn't work for me.

  1. I kept on seeing blank like below for docker container lsenter image description here
  2. then I tried, docker container ls -a and after that it showed all the process previously exited and running.
  3. Then docker stop <container id> or docker container stop <container id> didn't work
  4. then I tried docker rm -f <container id> and it worked.
  5. Now at this I tried docker container ls -a and this process wasn't present.
Cyder answered 29/1, 2019 at 16:15 Comment(2)
Even after removing all the dockers from archive (-a) option , I still get this error Error starting userland proxy: Bind for 0.0.0.0:8080 failed: port is already allocated.Fugal
Perhaps something else is occupying this port. Find out and if not necessary kill the process and your port will be free.Cyder
M
14

on linux 'sudo systemctl restart docker' solved the issue for me

Marked answered 27/4, 2022 at 7:20 Comment(0)
R
13

I have had same problem with docker-compose, to fix it:

  1. Killed docker-proxy processe
  2. Restart docker
  3. Start docker-compose again
Respiratory answered 21/6, 2019 at 9:12 Comment(1)
The other solutions didn't work for me, the problem occurred after I resized my AWS EC2 and needed to start the containers again. Running sudo systemctl restart docker fixed the issue. thanksBrasilin
W
13

When I used nginx docker image, I also got this error:

docker: Error response from daemon: driver failed programming external connectivity on endpoint recursing_knuth (9186f7d7f523732b99d3510029cde9679f3f3fe7b7eb5f612d54c4aacea58220): Bind for 0.0.0.0:8080 failed: port is already allocated.

And I solved it using following commands:

$ docker container ls
$ docker stop [CONTAINER ID]

Then, running this docker container(like this) again is ok:

$ docker run -v $PWD/vueDemo:/usr/share/nginx/html -p 8080:80 -d nginx:alpine

You just need to stop the previous docker container.

Weirdie answered 13/7, 2021 at 7:36 Comment(0)
E
9

If you are using Docker-Desktop, you can quit Docker-Desktop and then restart it. It solved the problem for me.

Eula answered 10/3, 2021 at 14:0 Comment(1)
I had the same issue, Bind for 0.0.0.0:80 failed: port is already allocated, but restarting the Docker Desktop (on Windows 10) did the trick for me, just like you said.Debbi
U
8

docker ps will reveal the list of containers running on docker. Find the one running on your needed port and note down its PID.

Stop and remove that container using following commands:

docker stop PID
docker rm PID

Now run docker-compose up and your services should run as you have freed the needed port.

Unquestionable answered 29/8, 2019 at 7:44 Comment(0)
D
6

For anyone having this problem with docker-compose. When you have more than one project (i.e. in different folders) with similar services you need to run docker-compose stop in each of your other projects.

Deneendenegation answered 13/2, 2019 at 19:36 Comment(0)
H
6

It might be a conflict with the same port specified in docker-compose.yml and docker-compose.override.yml or the same port specified explicitly and using an environment variable.

I had a docker-compose.yml with ports on a container specified using environment variables, and a docker-compose.override.yml with one of the same ports specified explicitly. Apparently docker tried to open both on the same container. docker container ls -a listed neither because the container could not start and list the ports.

Hudgins answered 3/1, 2021 at 9:18 Comment(1)
I had the same thing happen but sort of reversed - docker-compose.yml specified the port explicitly, and docker-compose.override.yml had a different explicit setting, plus an env var to tell the app to use that new port. Fixed by commenting out the ports in the docker-compose.yml file as we only ever use docker-compose locally (ie: with the override file). The env var defined for the app in docker-compose.override.yml was never in use as the app never started up - it appears for us just to be a mismatch between ports settings in the two compose files.Showthrough
S
6

FOR WINDOWS; I killed every process that docker use and restarted the docker service on services. My containers are working now. It is about ports that is still in use by Docker even though you are not using on that moment.

Stelle answered 18/8, 2022 at 7:31 Comment(1)
This is the fix that worked for me on Windows 11 in 2023.Crenel
D
4

In my case, there was no process to kill.

Updating docker fixed the problem.

Decapitate answered 10/1, 2021 at 21:43 Comment(0)
P
3

For me the containers where not showing up running, so NOTHING was using port 9010 (in my case) BUT Docker still complained.

I did not want to reset my Docker (for Windows) so what I did to resolve it was simply:

  1. Remove the network (I knew that before a container was using this network with the port in question (9010) docker network ls docker network rm blabla (or id)
  2. I actually used a new network rather than the old (buggy) one but shouldn't be needed
  3. Restart Docker

That was the only way it worked for me. I can't explain it but somehow the "old" network was still bound to that port (9010) and Docker kept on "blocking" it (whinching about it)

Paniculate answered 18/5, 2021 at 6:43 Comment(1)
This helped me out! One of my containers was complaining about the port already allocated and running lsof on my mac didn't yield any ports being used. Had to scroll down to this answer and this ended up showing the network I had to delete. Spinning the container back up then worked afterwards!Standley
P
3

For me, the problem was mapping the same port twice.

Due to a parametric docker run, it ended up being something like

docker run -p 4000:80 -p 4000:80 anibar/get-started:part1

notice double mapping on port 4000.

The log is not informative enough in this case, as it doesn't state I was the cause of the double mapping, and that the port is no longer bound after the docker run command returns with a failure.

Personable answered 20/1, 2022 at 17:50 Comment(1)
Thanks for the answer. I had a long run docker command string, I didn't realize I put -p option in the beginning and at the endSandpiper
L
2

On Linux, you can run sudo netstat -tulpn to see what is currently listening on that port. You can then choose to configure either that process or your Docker container to bind to a different port to avoid the conflict.

Ladida answered 2/7, 2021 at 13:6 Comment(0)
S
1

Stopping the container didn't work for me either. I changed the port in docker-compose.yml.

Summertree answered 11/2, 2019 at 1:14 Comment(0)
B
1

Don't forget the easiest fix of all....

Restart your computer.

I have tried most of the above and still couldn't fix it. Then just restart my Mac and then it's all back to normal.

Burney answered 11/8, 2021 at 4:24 Comment(2)
even that didn't workPersonable
Easiest way but annoying.Diverticulum
S
1

How to stop docker processes

Making Docker Stop Itself <- Safe and Fast

this is the best way to stop containers and all unstoppable processes: making docker do the job.


go to docker settings > resources. change any of the resource and click apply and restart.


docker will stop itself and its every process -- even the most stubborn ones that might not be killed by other commonly used commands such as kill or more wild commands like rm suggested by others.

i ran into a similar problem before and all the good - proper - tips from my colleagues somehow did not work out. i share this safe trick whenever someone in my team asks me about this.

Error response from daemon: driver failed programming external connectivity on endpoint foobar
Bind for 0.0.0.0:8000 failed: port is already allocated

hope this helps!

Semiskilled answered 8/12, 2022 at 1:32 Comment(1)
before figuring this out, i've tried almost everything. restarting the computer, restarting docker, docker compose down, kill, and even rm some containers and files. changing resources to trigger docker restart was the best! hahaSemiskilled
L
1

After doing, in Docker Desktop for mac, i clicked on the Troubleshoot Menu, then clicked on Restart Docker Desktop.

Lachesis answered 10/7, 2023 at 9:46 Comment(0)
E
0

For anyone still looking for a solution, just make sure you have binded your port the right way round in your docker-compose.yml

It goes: - <EXTERNAL SERVER PORT>:<INTERNAL CONTAINER PORT>

Eddra answered 2/3, 2022 at 21:2 Comment(0)
S
0

Had the same problem. Went to Docker for Mac Dashboard and clicked restart. Problem solved.

Spindry answered 15/3, 2022 at 11:32 Comment(0)
O
0

my case was dump XD I was exposing port 80 twice :D

        ports:
            - '${APP_PORT:-80}:80'
            - '${APP_PORT:-8080}:8080'

APP_PORT is defined, thus 80 was exposed twice.

Orpiment answered 28/9, 2022 at 14:5 Comment(0)
C
0

I tried almost all solutions and found out the probable/possible reason/solution. So, If you are using traefik or any other networking server, they internally facilitate proxy for load balacing. That, most use the blueprint as it, works pretty fine. It then passes the load control entirely to nginx or similiar proxy servers. So, stopping, killing(networking server) or pruning might not help.

Solution for traefik with nginx,

sudo /etc/init.d/nginx stop
# or
sudo service nginx stop
# or
sudo systemctl stop nginx

Credits

Chronologist answered 10/11, 2022 at 7:39 Comment(0)
W
-5

simply restart your computer, so the docker service gets restarted

Wade answered 16/4, 2022 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.