Advanced Networking: Port Mapping and Multi-Host Setups
Exposing Services with Port Mapping
By default, Docker containers aren’t reachable from outside the host. To expose a service, use port publishing:
Now, localhost:8080 on your host forwards to the container’s port 80. This is essential for making web apps or APIs accessible externally.
Using the Host Network Driver
For maximum performance (lowest latency, no port mapping), use the host network driver. This attaches the container directly to the host’s network stack:
This approach removes the network isolation layer—use with care, as all ports and interfaces are shared between container and host.
Multi-Host Networking with Overlay Driver
To connect containers across multiple hosts, you must use the overlay driver, which requires Docker Swarm or Kubernetes. Overlay networks enable DNS-based service discovery and encrypted cross-host communication but require orchestration setup.
| Docker Network Driver | Scope | DNS Service Discovery | Common Use |
|---|---|---|---|
| bridge (default) | single host | no | simple, single host |
| bridge (user-defined) | single host | yes | multi-container stacks |
| host | single host | n/a | performance, monitoring |
| overlay | multi-host | yes | Swarm/K8s, production |
| macvlan | single/multi-host | no | legacy integration |
Port mapping to an Existing Container#
Docker does not allow adding port mappings directly with docker start, port forwarding can be specified only with docker run (and docker create) command. Below are some strategies mentioned to achieve the same.
Strategy 1: The Fresh Restart#
This is the cleanest approach if your application configuration is stored in a Dockerfile or your data persisted in Volumes.
- Stop and Remove the existing container:
docker stop test01 && docker rm test01 - Relaunch using the original image, adding the required port flags:
docker run -d -p 8080:8080 --name test01 original_image_name
Strategy 2: The State Commit (Preserving Session State)#
If you have made manual, un-mirrored changes inside the container’s writable layer (e.g., installed a temporary tool or edited a config file internally) and need to keep them, follow the Commit Workflow:
- Stop the container:
docker stop test01 - Commit the state to a new temporary image:
docker commit test01 test01_snapshot - Relaunch from the snapshot with the new port mapping:
docker run -d -p 8080:8080 --name test01_new test01_snapshot
Note: Neither of the approach will have the volumes from old container, new container will have new set of volumes. To ensure your new container has the same data as the old one, you must include the volume flags in your final step. docker run -p 8080:8080 -v my_data:/app/data -td test02
A Quick Tip: Docker Compose. Instead of committing and re-running, you just change one line in a text file and run docker-compose up -d. It handles the port changes and volume persistence automatically!
Conclusion#
Advanced Docker networking provides flexible ways to expose services and connect containers across environments. Features like port mapping, host networking, and overlay networks allow developers to control accessibility, optimize performance, and scale applications beyond a single host. Choosing the right network driver ensures the right balance between isolation, performance, and scalability for modern containerized applications.