Docker Networks: Types, Port Publishing, and Container Communication

·1319 words

Docker networking determines how containers communicate. On a shared Compose network, Flask can connect to Redis using the service name redis. A browser on the host can reach Flask through a port published with -p. This guide covers common network types, when to create a custom network, and when to publish a port.

Common commands at a glance
#

CommandWhat it does
docker network lsList Docker networks
docker network create "$bridge_net"Create a custom bridge network (set the variable as shown below first)
docker run --network "$bridge_net" ...Choose a network when creating a container
docker network connect "$net2" "$app1"Connect the container to the second exercise network (set the variables as shown below first)
docker run -pPublish a container port on the host
docker port "$web"Check the port mappings for the exercise container (set the variable as shown below first)
docker network inspect bridgeInspect the default bridge network and its endpoints
docker network rm "$bridge_net"Remove the test network once no containers are attached (set the variable as shown below first)

Docker Network Types
#

DriverWhat it doesWhen to use it
bridgeConnects containers on one host. A user-defined bridge resolves container names.Most single-host applications.
hostShares the host’s network stack on Linux, so -p has no effect.A service that needs to use the host’s network directly.
noneLeaves only the container’s loopback interface.A task that needs no network access.
overlayConnects containers or services across Docker hosts in a swarm.Services distributed across multiple Swarm nodes.
macvlanGives each container a MAC address visible on the physical network.Networks that allow additional MAC addresses when an application must appear as a separate LAN device.
ipvlanGives containers addresses on the underlying network with fewer MAC addresses than macvlan.Specialized setups with constraints on MAC addresses or routing.
Third-party driversProvide networking features through plugins.An environment with requirements the built-in drivers do not cover.

Start with a user-defined bridge for single-host exercises.

On Linux, host networking shares the host’s network stack, so -p has no effect. Docker Desktop 4.34 and later can also enable host networking for Linux containers in settings; that implementation supports only TCP and UDP.

Default Bridge vs Custom Bridge
#

Docker creates a default network named bridge after installation. All containers not specifying a network connect to this network.

View current networks:

Shell
docker network ls

You’ll see default networks like bridge, host, and none.

Default bridge limitations: containers can only access each other by IP, not by container name. Recreating a container can change its IP address.

Custom bridge networks solve this problem:

To avoid colliding with existing networks or containers, generate a unique prefix in one shell and reuse these variables throughout the exercises.

Shell
lab_id="docker-learning-$(date +%s)-$$"
bridge_net="${lab_id}-bridge"
app1="${lab_id}-app1"
app2="${lab_id}-app2"
web="${lab_id}-web"
Shell
docker network create "$bridge_net"

Start a BusyBox client that includes wget and an Nginx service on the same network. This exercise relies on busybox:1.37 providing wget; do not assume another image includes this tool.

Shell
docker run -d --name "$app1" --network "$bridge_net" busybox:1.37 sleep 300
docker run -d --name "$app2" --network "$bridge_net" nginx:1.30-alpine

Now app1 can access the other container using the name app2; Docker automatically resolves it to the corresponding container’s IP. Use wget inside app1 to request the Nginx page and verify name resolution without relying on whether the Nginx image includes ping:

Shell
docker exec "$app1" wget -qO- "http://$app2"

The command should return the Nginx welcome page, confirming that the client can find and reach the service by container name.

Clean up:

Shell
docker stop "$app1" "$app2"
docker rm "$app1" "$app2"
docker network rm "$bridge_net"

Compose network behavior: docker compose up automatically creates a bridge network named after the project, connecting all services to this network where services can access each other by service name. This is why Flask in article 5 could access the Redis container using redis.

Publish a container port
#

On a shared bridge network, containers can reach each other’s listening ports directly. Publish a port when a client needs to connect through an address on the Docker host. Dockerfile’s EXPOSE documents the intended port, while -p sets the actual mapping.

Run Nginx and publish container port 80 on host port 8080. Engine 28.0.0 and later restrict this loopback mapping to the host. On earlier versions, machines on the same layer 2 network may still reach ports published to localhost.

Shell
docker run --rm -d --name "$web" -p 127.0.0.1:8080:80 nginx:1.30-alpine
docker port "$web"
curl -I http://127.0.0.1:8080

Look for 127.0.0.1:8080 in the mapping and HTTP status 200 in the response. Stop the test container before trying another mapping. --rm removes it automatically.

Shell
docker stop "$web"

To try another mapping, replace only the -p argument in that command and run the container again. Stop it before switching to the other option.

MappingEffect
-p 8080:80Publish on all host interfaces by default. Use it when other machines need access and check the host’s network policy.
-p 80Let Docker choose an available host port. Run docker port "$web" to find it.

Inter-Container Communication: Same Network vs Cross-Network
#

Same custom bridge network: Containers access each other by name, no port publishing needed. For example, Flask accessing Redis with both containers on the same Compose network allows Flask to connect directly to redis:6379 without publishing Redis port 6379 to the host.

Different networks: By default, containers on different networks cannot access each other. You can connect one container to multiple networks:

Shell
net1="${lab_id}-net1"
net2="${lab_id}-net2"
docker network create "$net1"
docker network create "$net2"

docker run -d --name "$app1" --network "$net1" busybox:1.37 sleep 300
docker run -d --name "$app2" --network "$net2" nginx:1.30-alpine

# Verify that cross-network access fails; a timeout or name resolution error is expected
docker exec "$app1" wget -T 3 -qO- "http://$app2" || echo "Expected: app1 cannot reach app2 across separate networks"

# Connect app1 to net2 as well
docker network connect "$net2" "$app1"

# Now app1 has interfaces on both net1 and net2, can access app2
docker exec "$app1" wget -qO- "http://$app2"

Check which networks app1 is connected to before removing it:

Shell
docker inspect --format '{{json .NetworkSettings.Networks}}' "$app1"

The output should include the actual values of net1 and net2, confirming that app1 has joined both networks.

Clean up:

Shell
docker stop "$app1" "$app2"
docker rm "$app1" "$app2"
docker network rm "$net1" "$net2"

View Network Details
#

Shell
docker network inspect bridge

Shows network subnet, gateway, connected containers and their IPs. Very useful for troubleshooting network issues.

Common Issues
#

Containers can’t ping or access each other Check if they’re on the same network. Default bridge doesn’t support container name resolution; switch to a custom bridge. If using multiple networks, confirm the two containers share at least one common network.

-p mapped but host can’t access Confirm the application inside the container listens on 0.0.0.0 rather than 127.0.0.1. Check host firewall and port usage. Use docker logs to see if the application started successfully.

Compose service can’t access another service Check the service name and confirm both services are connected to the same Compose network. depends_on expresses a dependency; for health-based startup ordering, see the Compose guide.

Why didn’t UFW block a published port?

Docker configures its own firewall rules for published ports, and traffic may bypass UFW’s usual input-chain rules. Do not use UFW’s status alone to decide whether a container port is exposed. Restrict the published address, inspect the host’s effective rules, and follow Docker’s firewall guidance. Avoid copying unverified iptables/nftables changes.

-p doesn’t work in host network mode With --network host, containers directly use the host network stack; -p has no effect. Ports the container listens on are directly on the host.

References
#