Skip
Arish's avatar

10. Port Mapping


Port Mapping Basics

bash
1# Map host:container
2docker run -p 8080:80 nginx
3#            │    └── Container port
4#            └─────── Host port

Multiple Ports

bash
1docker run -p 8080:80 -p 8443:443 nginx

All Interfaces vs Specific

bash
1# All interfaces (0.0.0.0)
2docker run -p 8080:80 nginx
3
4# Specific interface
5docker run -p 127.0.0.1:8080:80 nginx
6
7# Random host port
8docker run -p 80 nginx
9docker port mycontainer  # See assigned port

UDP Ports

bash
1docker run -p 53:53/udp dns-server

View Port Mappings

bash
1# List all port mappings
2docker port mycontainer
3
4# Specific port
5docker port mycontainer 80

Expose vs Publish

dockerfile
1# Dockerfile - document port (no mapping)
2EXPOSE 80
3
4# Run - actually publish
5docker run -p 8080:80 myimage