Docker
Networking
Subnet
Containerization
DevOps

What is the Docker Subnet used for?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A Docker subnet is the IP address range assigned to a Docker network. It exists so containers on that network can receive addresses, communicate with each other, and be routed predictably by Docker’s networking layer. In practice, understanding the subnet helps with connectivity debugging, avoiding IP conflicts, and designing multi-service environments cleanly.

What the Subnet Actually Does

When Docker creates a bridge or user-defined network, it allocates an address range such as 172.18.0.0/16. Containers attached to that network get IP addresses from that range.

That subnet is used for:

  • container-to-container communication
  • routing within the Docker network
  • network isolation between groups of containers
  • avoiding address overlap when multiple Docker networks exist

Without a defined subnet, Docker would not know how to assign addresses or route packets consistently.

Default Bridge Example

Docker’s default bridge network typically uses a private subnet automatically.

bash
docker network inspect bridge

The output usually includes an IPAM section showing the subnet and gateway. Containers attached to bridge receive addresses from that pool.

Creating a Custom Subnet

You can define one explicitly:

bash
1docker network create \
2  --driver bridge \
3  --subnet 192.168.100.0/24 \
4  mynet

Then start a container on it:

bash
docker run -d --name web --network mynet nginx

Now the container receives an address from 192.168.100.0/24.

Why You Might Want a Custom Subnet

Custom subnets are useful when:

  • you want predictable network segmentation
  • default ranges collide with VPN or office network ranges
  • multiple Docker environments must coexist cleanly
  • external routing rules depend on known private ranges

This is especially common on developer laptops that connect to corporate VPNs.

Container Discovery Usually Uses DNS, Not Fixed IPs

Even though Docker subnets assign IP addresses, most application code should use container names or service names rather than hard-coded IPs.

For example in Docker Compose, services resolve each other by name on the network. The subnet still matters underneath, but DNS-based naming is more stable than fixed addressing.

Docker Compose Example

yaml
1services:
2  api:
3    image: my-api
4    networks:
5      appnet:
6
7  db:
8    image: postgres:16
9    networks:
10      appnet:
11
12networks:
13  appnet:
14    ipam:
15      config:
16        - subnet: 172.30.0.0/24

Here both services share a custom subnet, but they should still talk using db and api as hostnames instead of fixed IPs.

Gateway and Routing

A subnet normally comes with a gateway address managed by Docker. Traffic between containers on the same network is routed internally, while traffic leaving the network goes through Docker’s NAT and host routing rules.

That is why subnet design can affect connectivity to outside networks and why overlapping ranges can break communication in confusing ways.

Common Debugging Scenario

If containers cannot reach each other or a host service, inspect the network:

bash
docker network inspect mynet
docker inspect web

Look for:

  • assigned container IP
  • configured subnet
  • gateway
  • network aliases

Many “Docker networking” bugs are really address-range conflicts or wrong network attachments.

Subnet Overlap Problems

If your Docker subnet overlaps with:

  • home LAN
  • office VPN
  • cloud VPC

then traffic may route incorrectly. A common fix is recreating Docker networks with a non-conflicting private range.

Good private choices usually come from unused RFC 1918 space already reserved for local container use in your environment.

Common Pitfalls

  • Assuming the Docker subnet is only about IP assignment and not about routing behavior.
  • Hard-coding container IPs instead of using container or service DNS names.
  • Letting Docker choose a subnet that overlaps with VPN or corporate network ranges.
  • Forgetting that each Docker network has its own isolated subnet space.
  • Debugging application connectivity without inspecting Docker network configuration first.

Summary

  • A Docker subnet is the address range used by a Docker network.
  • It enables container IP assignment, routing, and network isolation.
  • Custom subnets help avoid conflicts and improve predictability.
  • Use service names for application communication even when subnets are custom.
  • Network inspection is the first step when Docker connectivity behaves unexpectedly.

Course illustration
Course illustration

All Rights Reserved.