for-mac icon indicating copy to clipboard operation
for-mac copied to clipboard

port is already allocated without running containers

Open oliverhr opened this issue 10 years ago • 51 comments

Expected behavior

Container creation

Actual behavior

Error: port is already allocated

Information

Diagnostic ID: 269F8ACE-C09C-4FB4-856C-8208C92B0020 Docker for Mac: 1.12.0-beta21 (Build 11019) macOS: Version 10.11.6 (Build 15G31) [OK] docker-cli [OK] app [OK] moby-syslog [OK] disk [OK] virtualization [OK] system [OK] menubar [OK] osxfs [OK] db [OK] slirp [OK] moby-console [OK] logs [OK] vmnetd [OK] env [OK] moby [OK] driver.amd64-linux

Steps to reproduce

  1. Run a container using -p with any port - in my case two containers nginx (80, 443) and a node app running on 10011
  2. Remove the container
  3. Create the same or another container on the same port

Result for command docker ps -a: screenshot 2016-08-09 16 02 18

Portscan on 0.0.0.0 with docker running - But without running any container screenshot 2016-08-09 15 45 34

Portscan on 0.0.0.0 without docker: screenshot 2016-08-09 16 18 05

oliverhr avatar Aug 09 '16 21:08 oliverhr

Problem remains even after uninstall beta version and install stable.

oliverhr avatar Aug 09 '16 21:08 oliverhr

Solution: screenshot 2016-08-09 17 04 14

I don't like but – It is what it is

oliverhr avatar Aug 09 '16 22:08 oliverhr

Also seeing this when my mac sleeps and this is woken up; containers are killed (issue #85) and then when trying to restart containers using docker-compose up, I'm seeing the above error. Only fix is to restart my machine :(

jamesdixon avatar Aug 17 '16 03:08 jamesdixon

same here, reset to factory defaults would fix that.

qxhy123 avatar Aug 23 '16 10:08 qxhy123

In addition to this and #85, it's making development very difficult. Any additional information/assistance I can provide to help?

jamesdixon avatar Sep 05 '16 03:09 jamesdixon

The same issue on OSX 10.11.6 with Docker 1.12.1 stable

sergeyklay avatar Sep 17 '16 09:09 sergeyklay

@sergeyklay could you upload diagnostics from a reproduction on 1.12.1? The diagnostics in 1.12.1 are much better than in 1.12.0, in particular they include a process list from inside the VM which would show ports being held open from there. (The port opening is requested by the docker engine by running a proxy process, which communicates with a server on the Mac which actually opens the port. When the process inside the VM quits, the ports are closed on the Mac)

It would also really help if someone could help me reproduce this. Has anyone got a simple script or compose file they could share? Every time I try to repro this it always seems to work as expected :(

For example if I run an nginx in one terminal:

$ docker run -it -p 80:80 -p 443:443 nginx

and then in another terminal

$ curl http://localhost
... lots of html ...

I can see the ports bound via netstat:

$ netstat -an | less
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  *.80                   *.*                    LISTEN     
tcp4       0      0  *.443                  *.*                    LISTEN  
...

I can then Control+C the docker run and the ports go away. If I run docker run again it all comes back as expected :(

For a more complicated example, if I clone the example-voting-app:

$ git clone git://github.com/docker/example-voting-app
Cloning into 'example-voting-app'...
remote: Counting objects: 238, done.
remote: Total 238 (delta 0), reused 0 (delta 0), pack-reused 238
Receiving objects: 100% (238/238), 182.09 KiB | 0 bytes/s, done.
Resolving deltas: 100% (71/71), done.

I can repeatedly bring it up and down with

$ docker-compose up -d
# manually verify that ports 5000 and 5001 are bound
$ docker-compose down
# manually verify that ports 5000 and 5001 are free

with no error :(

djs55 avatar Sep 17 '16 10:09 djs55

Just wanted to add, I ran into this as well, but on different ports.

➜  lsof | grep 16380
com.docke 14092 cory   20u    IPv4 0xc6baa1d6e0a73c6d        0t0      TCP *:16380 (LISTEN)
com.docke 14092 cory   21u    IPv6 0xc6baa1d6d43e7c75        0t0      TCP localhost:16380 (LISTEN) (LISTEN)

This was with no containers running, and even after removing all containers, restarting docker, and then finally reinstalling docker for mac.

Restoring factory settings and setting everything up again now. Fortunately, that part is all automated... but it's a very odd problem.

counterbeing avatar Sep 21 '16 21:09 counterbeing

I don't know what that was, but restoring to factory defaults did the trick!

counterbeing avatar Sep 21 '16 22:09 counterbeing

did the trick

Ya, and removes all images, containers, volumes, networks, etc

sergeyklay avatar Sep 21 '16 22:09 sergeyklay

This happen again after shutdown computer while closing docker.

On linux users have reported that after remove local-kv.db with all containers stoped the problem gets solved.

The thing is on Mac I can't find this file. On linux the path is /var/lib/docker/network/files/ but not in OsX or macOS.

oliverhr avatar Sep 30 '16 21:09 oliverhr

Just ran into the same issue. After trying out some things, i found it. I did not want to reset my machine.

  • run docker-compose down (or similar when not using compose) to remove the network
  • stop docker and start again
  • start container without issues (or test with lsof -i tcp:PORTNUMBER)

roelvanhintum avatar Oct 05 '16 15:10 roelvanhintum

I already tried by stopping all the containers, even removing all of them. The reset is a desperate measure but is the only thing that kinda works, as I say before by far is not the best but It is what it is at the moment.

oliverhr avatar Oct 05 '16 16:10 oliverhr

@oliverhr In my case stopping containers was not sufficient. Cleaning up the remaining networks and after that stopping and starting docker fixed it. The containers are fine, it's just the lingering networks that keep blocking the ports.

roelvanhintum avatar Oct 05 '16 17:10 roelvanhintum

Similar problem here. Container using port 3000/3001 cannot be started: "Bind for 0.0.0.0:3001 failed: port is already allocated". When I remove port 3001, I get the same message for port 3000. Happened this morning, no problems the days before, no system updates in between. Stopped/removed container, restarted docker daemon ("sudo service docker restart"), but no result.

Netstat shows this: tcp6 0 0 :::3000 :::* LISTEN 7039/docker-proxy tcp6 0 0 :::3001 :::* LISTEN 7031/docker-proxy

I start container with "-p 0.0.0.0:3000:3000 -p 0.0.0.0:3001:3001" options.

I am on a slow internet connection, so removing docker folder and recreating all images is not really an option at the moment.

The only way I can get around this is using a different port: "-p 0.0.0.0:4000:3000 -p 0.0.0.0:4001:3001".

There must be some way to release these ports??

docker version: Client: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:22:43 2016 OS/Arch: linux/amd64

Server: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:22:43 2016 OS/Arch: linux/amd64

ghost avatar Oct 06 '16 06:10 ghost

@ctammes Did you try disconnecting or removing networks with docker network?

roelvanhintum avatar Oct 06 '16 06:10 roelvanhintum

docker network disconnect : no result. Suggestion from #25981 that kills process using the port: no result.

ghost avatar Oct 06 '16 07:10 ghost

I checked it again, here is what i did to solve it:

  • remove the network listed in docker network ls
  • stop docker to release the port (after this the port should be unused on your mac)
  • start docker (the port should not be in use before starting any containers)

roelvanhintum avatar Oct 06 '16 07:10 roelvanhintum

@roelvanhintum Follwed your suggestions, but no result.

Tried to recreate image/container, but that lead to another error: Error response from daemon: oci runtime error: process_linux.go:334: running prestart hook 0 caused "exit status 1: time="2016-10-06T09:47:15+02:00" level=fatal msg="no sandbox present for 5b31e0edc7d82e0f91c9dd61d0f593337cf81bed293fd83ccc7b20f0a0062a23" \n" Error: failed to start containers

Finally removed all present containers "docker rm $(docker ps -aq)" and removed files to increase free space (I had 93% in use, after clean-up 83%). Build the images and containers without problems now.

So, maybe it is a combination of things that leads to this. I don't know if the free space really has any influence or that removing all containers did the trick.

ghost avatar Oct 06 '16 08:10 ghost

Got the same problem again. All containers removed, stop docker engine: no ports in use, start docker engine without any containers present: ports are in use according to 'netstat -an'.

Loosing my confidence in docker ...

This are the steps I did to get the containers running again:

  1. stop all running containers: docker stop
  2. remove all containers: docker rm $(docker ps -aq)
  3. remove network: docker network rm
  4. stop daemon: sudo service docker stop
  5. verify no ports are in use: netstat -an | egrep ":(3000|3001|3316)"
  6. start daemon: sudo service docker start
  7. verify no ports are in use: netstat -an | egrep ":(3000|3001|3316)"
  8. create network: docker network create
  9. build/start containers: docker run --restart=always ...

But why does this happen??

ghost avatar Oct 09 '16 15:10 ghost