From the official document, the static ip address should work with docker compose v3 + docker swarm. But I cannot make it work.
This is my docker compose yaml:
version: '3'
networks:
test:
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
services:
one:
image: mongo
networks:
test:
ipv4_address: '10.1.0.100'
two:
image: mongo
networks:
test:
ipv4_address: '10.1.0.101'
three:
image: mongo
networks:
test:
ipv4_address: '10.1.0.102'
I use docker stack up -c xxx.yml test
to deploy. I found the ip of the created containers are 10.1.0.3, 10.1.0.5, ...
Static IP addresses don't change when containers or services are stopped and started, making them useful for permanent networking. Assigning Docker containers static IP addresses is an easy way to make them more accessible.
By default, the container is assigned an IP address for every Docker network it connects to. And each network is created with a default subnet mask, using it as a pool later on to give away the IP addresses. Usually Docker uses the default 172.17. 0.0/16 subnet for container networking.
The default IP range for the docker0 network is 172.17. 0.0/16.
That works in non-swarm mode, but not in swarm mode. You may subscribe to this ticket to find out when/if support is added.
At the moment, there's an anecdotal claim of a workaround, but nobody else seems to have corroborated it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With