✦ For everyone, free.

Practical knowledge for real and everyday life

Home

7.1.1.2 Container IP Assignment

A focused guide to Container IP Assignment, connecting core concepts with practical Docker and container operations.

Container IP assignment is the process by which a container receives an IP address from the range associated with whatever Docker network it connects to, happening automatically when the container starts unless a specific, fixed IP address is explicitly requested.

Automatic IP Assignment

By default, Docker assigns the next available IP address from the connected network's configured address range.

docker network create mynet --subnet=172.20.0.0/16
docker run -d --name container-a --network mynet alpine sleep 1000
docker inspect container-a --format '{{.NetworkSettings.Networks.mynet.IPAddress}}'

This reveals the automatically assigned address, drawn from the 172.20.0.0/16 range configured for mynet.

Requesting a Specific, Fixed IP Address

For situations needing a predictable, fixed address rather than an automatically assigned one, a specific IP can be explicitly requested at container creation.

docker run -d --name container-a --network mynet --ip=172.20.0.10 alpine sleep 1000
Why IP Addresses Can Change Across Container Recreations

Because automatic assignment simply picks an available address at the time of creation, a container recreated later (even with the identical configuration) may receive a different IP address than it had previously — relying on a container's specific IP address persisting across recreation is generally fragile.

docker rm -f container-a
docker run -d --name container-a --network mynet alpine sleep 1000
docker inspect container-a --format '{{.NetworkSettings.Networks.mynet.IPAddress}}'

This newly created container may well receive a different IP address than its predecessor did.

Why Name-Based Resolution Is Generally Preferable to Fixed IPs

Because of this potential for IP addresses to change, referring to other containers by their container name (resolved automatically on a user-defined network) is generally a more robust approach than depending on a specific, fixed IP address remaining stable.

docker exec container-b ping container-a
Why Container IP Assignment Matters

Understanding how and when IP addresses are assigned — and why relying on name-based resolution is generally more robust than depending on a specific address — helps avoid fragile networking configurations that break unexpectedly when containers are recreated.