brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: give a Docker container a fixed IP on a network with your own subnet

Docker
~/tips/tip-docker-static-ip-user-network

docker network create --subnet 192.0.2.0/24 lan

Pin a container's address when a firewall rule or old client needs a fixed IP.

Container names are the right way for containers to find each other. Some things still want an IP: a firewall rule, a DNS server address in a router, a device that only takes numbers. Docker can pin one on a user defined network.

docker network create --subnet 192.0.2.0/24 lan
docker run -d --name dns --network lan --ip 192.0.2.10 example/dns:1.0

The addresses are placeholders; pick a private subnet that is free on your machine. The container gets 192.0.2.10 every time it starts, also after it is recreated. In Compose the network needs an ipam block with the subnet, and the service gets ipv4_address under its network entry.

The subnet is the important part. --ip only works on a user defined network where you set the subnet yourself; on the default bridge Docker refuses it. I would also keep the fixed addresses at one end of the range and let Docker hand out the rest, so a container without a fixed IP never takes the one you reserved. --ip-range on the network can shrink Docker’s automatic pool for that.

A fixed IP is something you have to keep track of. Two containers with the same address cannot both start, and the error only shows up when the second one tries. Unless something outside Docker really needs the number, I would stick to names.

Further reading: docker container run: ip, docker network create

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link