Anik Barua
Anik Barua

Reputation: 3461

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

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.

Upvotes: 330

Views: 561447

Answers (30)

beyza_k
beyza_k

Reputation: 11

Error message says "port is already allocated", it indicates that the port is already in use on your machine, likely by another instance of PostgreSQL or another service. When I got this message I've checked is there any Docker container using this port or not.

docker ps

Then I realized that there is one PostgreSQL container using same port. I removed the container and it worked.

docker stop <container_id>
docker rm <container_id>

Upvotes: 1

Use the IPv6 Adress before the Port. For example:

-p [2a01:4f3:a533:8354:5:2:0:1]:2049:2049 \

Here's a full example of setting up an NFS server in Docker using IPv6:

docker network create --ipv6 \
--subnet="2a01:4f3:a533:8354:5:2::/126" \
--gateway="2a01:4f3:a533:8354:5:2::1" \
ip6net-nfs-1

docker run -d \
  --name=nfs-1 \
  -e NFS_MOUNT_PORT=2049 \
  -e ALLOWED_CLIENT=2a01:4f3:8353:b999::1 \
  -v /lib/modules:/lib/modules \
  -v /var/lib/docker/mount/your_data:/data \
  --restart unless-stopped \
  --privileged \
  --network=ip6net-nfs-1 \
  -p [2a01:4f3:a533:8354:5:2:0:1]:2049:2049 \
  -p [2a01:4f3:a533:8354:5:2:0:1]:111:111 \
  -p [2a01:4f3:a533:8354:5:2:0:1]:32765-32767:32765-32767 \
  mekayelanik/nfs-server-alpine:latest

Note: Set ALLOWED_CLIENT to * if you want to allow multiple clients. However, if you do this, make sure to configure your external firewall to permit only specific IP addresses to access the NFS server.

In this setup, your NFS server will be available at the IPv6 address 2a01:4f3:a533:8354:5:2::1. If you’re using Hetzner Cloud, you’ll need to add this IP address to your network configuration. Here is an example:

cat > /etc/network/interfaces.d/99-custom-nfs <<EOF
# Reachable for all IPs in the /124 (16 IP Addresses)
iface eth0 inet6 static
    address 2a01:4f3:a533:8354:2::1/124
    dns-nameservers 2a01:4ff:ff00::add:1 2a01:4ff:ff00::add:2
    gateway fe80::1
    # Setup to respond to any address in the /124 subnet
    post-up ip -6 route add local 2a01:4f3:a533:8354:2::/124 dev eth0
    pre-down ip -6 route del local 2a01:4f3:a533:8354:2::/124 dev eth0

# Additional network configuration
# NFS 1
iface eth0 inet6 static
    address 2a01:4f3:a533:8354:5:2::1/127
    # Setup to respond to any address in the /127 subnet
    post-up ip -6 route add local 2a01:4f3:a533:8354:5:2::/127 dev eth0
    pre-down ip -6 route del local 2a01:4f3:a533:8354:5:2::/127 dev eth0
EOF

# Reboot
reboot now

I hope this provides some clarity on how to use multiple IPv6 addresses with Docker, as it was quite a challenge to figure out. You may need to enable IPv6 in Docker by modifying the /etc/docker/daemon.json file. Additional information can be found online.

Upvotes: 0

michellhornung
michellhornung

Reputation: 21

just make a service docker restart and then you can run docker compose up -d on your compose file.

Upvotes: 0

Yogesh Shukla
Yogesh Shukla

Reputation: 21

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

Upvotes: 1

J. Doe
J. Doe

Reputation: 13013

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

Upvotes: 11

Kikito
Kikito

Reputation: 224

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!

Upvotes: 1

Henshal B
Henshal B

Reputation: 1972

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

Upvotes: 0

shamaseen
shamaseen

Reputation: 2478

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.

Upvotes: 0

Onur Bolaca
Onur Bolaca

Reputation: 87

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.

Upvotes: 6

KIA CEED
KIA CEED

Reputation: 305

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

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

Upvotes: 14

Yassine Chilali
Yassine Chilali

Reputation: 507

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

Upvotes: 16

user4894254
user4894254

Reputation: 11

simply restart your computer, so the docker service gets restarted

Upvotes: -4

undefined
undefined

Reputation: 690

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

Upvotes: 0

gellowg
gellowg

Reputation: 81

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>

Upvotes: 0

Gulzar
Gulzar

Reputation: 27896

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.

Upvotes: 5

Ken Ratanachai S.
Ken Ratanachai S.

Reputation: 3547

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.

Upvotes: 1

Wonz
Wonz

Reputation: 455

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.

Upvotes: 15

Rodut Naitsirc
Rodut Naitsirc

Reputation: 31

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.

Upvotes: 2

Ray
Ray

Reputation: 186

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)

Upvotes: 3

Fahima Mokhtari
Fahima Mokhtari

Reputation: 2062

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

Upvotes: 106

Matt
Matt

Reputation: 4550

In my case, there was no process to kill.

Updating docker fixed the problem.

Upvotes: 4

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

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.

Upvotes: 9

DevTomek
DevTomek

Reputation: 653

The quick fix is ​​a just restart docker:

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

Upvotes: 50

Charlie
Charlie

Reputation: 9108

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.

Upvotes: 7

Alexandre Lara
Alexandre Lara

Reputation: 2568

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>

Upvotes: 46

everyman
everyman

Reputation: 3407

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.

Upvotes: 6

Morgan
Morgan

Reputation: 1310

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

Upvotes: 1

paul
paul

Reputation: 4487

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.

Upvotes: 20

yamenk
yamenk

Reputation: 51738

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>

Upvotes: 498

user5965280
user5965280

Reputation:

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

Upvotes: 108

Related Questions