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:

    # Publish host port 8080 to container port 80 docker run -d --name frontend -p 8080:80 nginx:alpine

    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:

    # Run a container using the host’s network stack docker run -d --name monitor --network host node:alpine node app.js

    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 DriverScopeDNS Service DiscoveryCommon Use
    bridge (default)single hostnosimple, single host
    bridge (user-defined)single hostyesmulti-container stacks
    hostsingle hostn/aperformance, monitoring
    overlaymulti-hostyesSwarm/K8s, production
    macvlansingle/multi-hostnolegacy integration
    Driver Type and Use Case Summary

     

    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.

    1. Stop and Remove the existing container: docker stop test01 && docker rm test01
    2. 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:

    1. Stop the container: docker stop test01
    2. Commit the state to a new temporary image: docker commit test01 test01_snapshot
    3. 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.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more

    Last updated on Mar 17, 2026

    Was it helpful?

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.