Q: Docker container joining existing network with fixed ip (via docker compose)
Q: Docker container joining existing network with fixed ip (via docker compose)
Hi, I figured out how to get docker containers to join an existing network with putting "networks" into the respective sections of the docker-compose.yml
If I want to also give them fixed ip's on this network, what would the syntax look like in the docker-compose.yml?
In the above example I've declared a Docker Bridge network with the range
172.20.0.0/24
and a gateway at172.20.0.1
. I have a service namedapp
with a static IP of172.20.0.10
.The same is also possible with IPv6, though there are extra steps involved to make IPv6 networking work in Docker and it's not enabled by default so I won't go into detail in this comment.
Out of curiosity, what's the use case for a static IP in the Docker Bridge network? Docker compose assigns hostnames equal to the service name. That is, if you had another container in the
app
network from my example above, it could just do a DNS lookup forapp
and it would resolve to172.20.0.10
.thanky you, this looks like exactly what I need.
I do run several webservices (nextcloud, matrix) behind the same reverse proxy (nginx prxy manager). In my setup I have one docker with nginx running, which is the only one to be exposed to the web. It proxy-ing for the other services relies upon them being in the same network. It all works well, however I ran into problems when restarting my server after a shutdown. I suspect that some of the services tried to get the same ip adress as my nginx service, which results in that service not running properly and my whole reverse proxy setup falls apart at that point.
I'm not certain, that this is really what happens but I want to try and assign the fixed ip's and see if that solves the problem.
Honestly, I'd be surprised if static IPs fix it. Docker's default network type (bridge network) is very good at assigning IPs to containers without clashing, even with container restarts and replacements: it's been battle tested for years in production use. As others have said, standard DNS hostnames of containers should be sufficient. But I'll certainly be interested to hear your results.