How to Fix Docker Daemon Port Binding Error on Windows

Problem Overview

When working with Docker on Windows, you might encounter the following error when trying to bind ports:

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:54328 -> 0.0.0.0:0: listen tcp 0.0.0.0:54328: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

This error indicates that Docker is unable to bind to the specified port due to access restrictions set by Windows.

Solution

To resolve this issue, you can restart the win service, which will release the ports and allow Docker to bind to them again. Follow these steps:

Open CMD As Admin and run in sequence:
net stop winnat
net start winnat

Here's a breakdown of what these commands do:

  • net stop winnat: Stops the win service, which is responsible for managing NAT and port mapping on Windows.
  • net start winnat: Restarts the win service, allowing Docker and other applications to bind to their required ports without conflicts.

Common Causes

The error typically occurs due to a conflict with the win Network Address Translation (NAT) service, which manages port allocation and mapping on Windows. Some common reasons include:

  • Another service is using the port Docker is trying to bind.
  • Windows has reached its limit for port assignments, particularly in development environments with many services.
  • Network settings or firewall configurations are preventing access to the port.

Detailed Explanation

When you encounter port binding issues with Docker, it means Docker cannot map local ports to the container's ports. By restarting the win service, you effectively clear out the existing port mappings and allocations that Windows holds onto, often resolving these conflicts. This process is particularly useful in environments where dynamic port allocation and frequent rebinding are common.

After running these commands, Docker should be able to bind to the requested ports without encountering the access permission error.

Additional Notes

While this solution is effective, it's good practice to ensure no critical services depend on win during the restart. You might also consider:

  • Checking for any application-specific logs that could indicate why the port was locked.
  • Ensuring that Docker and other related services are properly configured for your Windows environment.
  • Regularly monitoring your system's port usage to prevent similar issues.