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

DockerPort

Docker Problem Overview


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.

Docker Solutions


Solution 1 - Docker

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>

Solution 2 - Docker

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

Solution 3 - Docker

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

Solution 4 - Docker

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>

Solution 5 - Docker

The quick fix is ​​a just restart docker:

  1. sudo service docker stop
  2. sudo service docker start

Solution 6 - Docker

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

  1. I kept on seeing blank like below for docker container ls[![enter image description here][1]][1]
  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. [1]: https://i.stack.imgur.com/YDaMQ.png

Solution 7 - Docker

I've had same problem with docker-compose:

  1. Killed docker-proxy processe .
  2. Restart docker
  3. Start docker-compose again.

Solution 8 - Docker

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.

Solution 9 - Docker

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.

Solution 10 - Docker

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.

Solution 11 - Docker

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

Solution 12 - Docker

In my case, there was no process to kill.

Updating docker fixed the problem.

Solution 13 - Docker

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.

Solution 14 - Docker

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)

Solution 15 - Docker

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

Solution 16 - Docker

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.

Solution 17 - Docker

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.

Solution 18 - Docker

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.

Solution 19 - Docker

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

Solution 20 - Docker

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

Solution 21 - Docker

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>

Solution 22 - Docker

simply restart your computer, so the docker service gets restarted

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnik BaruaView Question on Stackoverflow
Solution 1 - DockeryamenkView Answer on Stackoverflow
Solution 2 - Dockeruser5965280View Answer on Stackoverflow
Solution 3 - DockerFahima MokhtariView Answer on Stackoverflow
Solution 4 - DockerAlexandre LaraView Answer on Stackoverflow
Solution 5 - DockerDevTomekView Answer on Stackoverflow
Solution 6 - DockerpaulView Answer on Stackoverflow
Solution 7 - DockerKIA CEEDView Answer on Stackoverflow
Solution 8 - DockerWonzView Answer on Stackoverflow
Solution 9 - DockereverymanView Answer on Stackoverflow
Solution 10 - DockerAkshay Vijay JainView Answer on Stackoverflow
Solution 11 - DockerJ. DoeView Answer on Stackoverflow
Solution 12 - DockerMattView Answer on Stackoverflow
Solution 13 - DockerCharlieView Answer on Stackoverflow
Solution 14 - DockerRayView Answer on Stackoverflow
Solution 15 - DockerMorganView Answer on Stackoverflow
Solution 16 - DockerRodut NaitsircView Answer on Stackoverflow
Solution 17 - DockerKen Ratanachai S.View Answer on Stackoverflow
Solution 18 - DockerGulzarView Answer on Stackoverflow
Solution 19 - DockerundefinedView Answer on Stackoverflow
Solution 20 - DockerYassine ChilaliView Answer on Stackoverflow
Solution 21 - DockergellowgView Answer on Stackoverflow
Solution 22 - Dockeruser4894254View Answer on Stackoverflow