Mapped port on localhost accepts connection immediately, before container starts listening on it's port

Expected behavior

I’ve found that when a container is started with a TCP port mapping, a connection can be established to the port on the host well in advance of the container listening on it’s internal port.

I maintain a integrating testing library that uses docker to deploy containers for “dependencies”, and we judge whether or not a container is fully up and ready to have tests run against it based on whether or not it accepts TCP connection on it’s mapped port.

Actual behavior

A connection can be made to the mapped port on localhost as soon as the container is started, before it internally starts listening on the port on the other side of the mapping.


$ pinata diagnose -u
OS X: version 10.11.5 (build: 15F34) version v1.11.1-beta12
Running diagnostic tests:
[OK]      Moby booted
[OK]      driver.amd64-linux
[OK]      vmnetd
[OK]      osxfs
[OK]      db
[OK]      slirp
[OK]      menubar
[OK]      environment
[OK]      Docker
[OK]      VT-x
Docker logs are being collected into /tmp/20160527-132920.tar.gz
Most specific failure is: No error was detected
Your unique id is: 3BD460DE-3CF4-4393-82DF-35B5E27266AE
Please quote this in all correspondence.