Search code examples
dockerboot2docker

Docker not releasing ports


(On a Mac, Boot2Docker, docker v 1.1.1)

Having trouble where Docker will not release the ports that it uses when assigning host ports. For instance, I start 3 containers with the same image. My docker ps returns the following:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
9875e2d28c3b        49ffc1050348        /sbin/my_init       19 hours ago        Up 1 seconds        0.0.0.0:49169->22/tcp, 0.0.0.0:49170->3306/tcp   boring_goldstine
f0a40c3bb4a1        49ffc1050348        /sbin/my_init       19 hours ago        Up 6 seconds        0.0.0.0:49167->22/tcp, 0.0.0.0:49168->3306/tcp   sleepy_wright
0fb913db3528        49ffc1050348        /sbin/my_init       19 hours ago        Up About a minute   0.0.0.0:49165->22/tcp, 0.0.0.0:49166->3306/tcp   agitated_jones

Scrolling over to ports you'll see that 49170 is the last allocated port.

If I stop and rm those containers and run another, Docker used to use the lowest available port. However now the number just keeps increasing:

docker run -P -d 49ffc1050348

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
82fdf0e54846        49ffc1050348        /sbin/my_init       19 hours ago        Up 3 seconds        0.0.0.0:49171->22/tcp, 0.0.0.0:49172->3306/tcp   high_euclid

How do I get it to reuse the old ports?


Solution

  • The portmapper logic changed in version 1.1.0. The logic is now written to increase until 65535, until it resets to BeginPortRange which is 49153.

    You can use docker run -p <host port>:<container port> to map a host port to a container port.