Port Binding Strategies
Static Port Binding
Static port binding provides predictable network configuration for containers. Developers can explicitly map container ports to specific host ports.
## Static port mapping to specific host port
docker run -d -p 3306:3306 mysql:latest
graph LR
A[Container Port] --> |Mapped To| B[Host Port]
B --> C[External Network]
Dynamic Port Allocation
Dynamic port allocation allows automatic port assignment by Docker, providing flexibility in container networking.
## Random port mapping
docker run -d -P nginx:latest
Binding Type |
Characteristics |
Use Case |
Static Binding |
Predefined ports |
Consistent configurations |
Dynamic Binding |
Random port assignment |
Flexible deployments |
Multiple Port Binding
Containers can bind multiple ports simultaneously, supporting complex network configurations.
## Binding multiple ports
docker run -d \
-p 8080:80 \
-p 8443:443 \
nginx:latest
Host Network Mode
Host network mode removes port mapping isolation, directly using host network interfaces.
## Using host network mode
docker run --network host nginx:latest