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

dockerport

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.

Best Answer

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>