Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 74/ 160
04. Docker Networking

Custom Subnets, Gateways & Static Container IP Assignment

Define subnets and optionally pin static IPs when DHCP-style assignment is not enough.

IntermediateSubnet · Gateway3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Use a custom subnet for route integration and reserve static IPs for legacy exceptions, not as normal service discovery.

Think of it like…

A static desk number works for an office that never moves; a DNS name is the receptionist routing visitors as desks are rearranged.

Where you’ve seen it

In practice, docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17 is the concrete move for this job: define subnets and optionally pin static IPs when DHCP-style assignment is not enough. The release is not accepted until docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM confirms the result.

Words you’ll hear

Subnet
The implementation control in docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17 for define subnets and optionally pin static IPs when DHCP-style assignment is not enough.
Gateway
The result to observe with docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM, rather than infer from a successful command alone.
Static IP
The production boundary exposed when using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.

Visual diagram

Loading interactive diagram

Theory & deep dive

Subnet control path

The concrete mechanism is docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17. The observable result is docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM; if those disagree, inspect the runtime evidence before changing unrelated settings.

Define subnets and optionally pin static IPs when DHCP-style assignment is not enough. Keep Subnet, Gateway and Static IP in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Subnet setup

docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17

Key takeaway: Prove Gateway with docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM, not with command success alone.

Static IP failure boundary

A static IP creates ordering and reuse problems during rescheduling, and an overlapping subnet sends traffic toward Docker instead of the corporate route. Static assignment also bypasses DNS failover unless the rest of the stack updates routing deliberately. The narrow failure to design for is using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.

Accept this compromise explicitly: Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule. The controlled check-docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.

Key takeaway: Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A static IP creates ordering and reuse problems during rescheduling, and an overlapping subnet sends traffic toward Docker instead of the corporate route. Static assignment also bypasses DNS failover unless the rest of the stack updates routing deliberately.

Use a custom subnet for route integration and reserve static IPs for legacy exceptions, not as normal service discovery.
Key numbers at this step
Intent
Subnet

When to use it & trade-offs

What it is

A custom network can declare subnet, gateway and IPAM ranges; a container can request a static address on that user-defined network. Docker owns the address pool, so external routing and static assignment must avoid DHCP, VPN and peer network overlaps.

Why it matters: A static IP creates ordering and reuse problems during rescheduling, and an overlapping subnet sends traffic toward Docker instead of the corporate route. Static assignment also bypasses DNS failover unless the rest of the stack updates routing deliberately.

When to reach for it

  • The fit is strongest for this job: define subnets and optionally pin static IPs when DHCP-style assignment is not enough. Pair it with docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM in the runbook.
  • Production Subnet changes should make Gateway observable through docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17, rather than trusting the runtime default.
  • Keep a rollback example for the case where using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.

When to avoid or be careful

  • Do not accept command success while the system can still fail by using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.
  • Avoid erasing Static IP protection merely to dodge this cost: “Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule”.
  • Do not replace the evidence path-docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM-with a screenshot or a mutable tag.

docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17: practical choices

Explicit Subnet
Best for: Define subnets and optionally pin static IPs when DHCP-style assignment is not enough, with a reviewed runbook.

Strengths: A custom network can declare subnet, gateway and IPAM ranges; a container can request a static address on that user-defined network. Docker owns the address pool, so external routing and static assignment must avoid DHCP, VPN and peer network overlaps.

Watch out: Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule.

Ambient default
Best for: Short-lived Gateway experiments with low blast radius.

Strengths: It can begin with docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17 and fewer surrounding controls.

Watch out: It silently accepts using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks, which makes the result hard to reproduce after promotion.

Managed Static IP
Best for: Teams that centralize Static IP policy across many workloads.

Strengths: A static IP creates ordering and reuse problems during rescheduling, and an overlapping subnet sends traffic toward Docker instead of the corporate route. Static assignment also bypasses DNS failover unless the rest of the stack updates routing deliberately.

Watch out: The platform adds an ownership and debugging layer; retain docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM as the direct check.

Interview signals & numbers to know

When this topic comes up

  • docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17
  • docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM
  • Failure signature: using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks
  • Trade-off: Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule.

Benchmarks & rules of thumb

254 host addresses -
typical /24 pool after network and broadcast
172.28.16.1 -
example network gateway
1 address owner -
Docker IPAM must remain authoritative inside the subnet

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17 as proof and skipping docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM.
  • Using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks
  • Optimizing Subnet without accepting that custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule.

Likely interviewer follow-ups

  • Why does docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17 address Subnet here rather than through application code?
  • How would you distinguish healthy Gateway from the failure “using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks” using docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM?
  • When would this trade-off-“Custom addressing integrates legacy routing; stable names and IPAM make general workloads safer and easier to reschedule”-justify a different Static IP design?
Interview tip: Give one concrete operation-docker network create --subnet 172.28.16.0/24 --gateway 172.28.16.1 payments; docker run --network payments --ip 172.28.16.20 db:17-and one proof-docker network inspect payments and ip addr show inside db, then test the route back from an approved corporate VM. Then name the boundary: using 10.0.0.0/8 out of habit on a host connected to a 10/8 VPN, or reserving the same address in two compose project networks.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Custom Subnets, Gateways & Static Container IP Assignment with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Network Aliases & Multi-Homed Containers
Next topic
Reverse Proxying Containers with Nginx & Caddy