Docker Bridge Networking Explained: IPs and Ports
Explore Docker bridge networking with in-depth examples on IP assignment and port publishing.
Before You Start Docker bridge networking is a fundamental concept that allows containers to communicate within a single Docker host. This guide assumes you have Docker installed on your system. Familiarity with basic networking concepts is beneficial. Technical Background Docker uses a bridge network to allow containers to communicate with each other and the external world. A bridge network creates a virtual network interface on the host, which acts as a router for container traffic. Each container connected to the bridge gets its own IP address, allowing it to communicate with other containers on the same network. The bridge network operates at Layer 2 of the OSI model, meaning it uses MAC addresses for communication. However, Docker assigns IP addresses to containers, making it functionally similar to a traditional network. The default bridge network in Docker is called 'bridge', and unless specified otherwise, containers are automatically attached to it. The bridge network also supports port publishing, which maps a container port to a port on the host machine. This allows external access to the services running inside the containers. Step By Step 1. To list the networks available in Docker, use the command: Command: docker network ls 2. To inspect the default bridge network and see details like subnet and gateway, use: Command: docker network inspect bridge 3. To run a container with a specific port published, use the following command: Command: docker run -d -p 8080:80 --name my_container nginx Worked Example Example: Run a container with a published port: Command: docker run -d -p 8080:80 --name webserver nginx Example: Check the IP address assigned to a running container: Command: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' webserver Configuration Or Command Reference - docker network ls — List all Docker networks. - docker network inspect — Get detailed info about a specific network. - docker run -d — Run a container in detached mode. - -p host_port:container_port — Publish a container's port to the host. - --name — Assign a name to the container. Troubleshooting 1. Symptom: Cannot access the container service externally. Likely Cause: Port not published or firewall blocking. Fix: Verify port mapping and check host firewall settings. 2. Symptom: Containers cannot communicate with each other. Likely Cause: Containers are on different networks. Fix: Attach containers to the same network. 3. Symptom: Network not liste…