Problem
I'm using Docker-Compose and want to set a locally-addressable IP (such as 10.1.1.100) for one of the containers. This IP is not on my host machine's subnet.
Vagrant style
In a similar Vagrant project, there's a line:
config.vm.network :private_network, ip: "10.1.2.100"
This works great in that project. I can target the machine at 10.1.2.100
as if it's an available IP on my network. I don't even have to create a subnet.
Question
I've been looking for how I'd setup a container with a locally-addressable IP with Docker (specifically Docker-Compose), but haven't been able to get it working.
Failed configurations
I've tried adding networks
, and assigned a static IP with ipv4_address: 10.1.1.100
. Sadly, it seems as though this entire network is only accessible via Docker itself, not via the host machine.
If I try to use ports
to expose an IP as 10.1.1.100:80:80
, I get this error instead:
Cannot start service SERVICE_NAME: Ports are not available: listen tcp 10.1.1.100:80: bind: can't assign requested address.
But this works fine if I simply put 80:80
. So it must be the IP binding that causes this issue.
I also tried setting network_mode
on only this service and neither host
nor bridge
worked correctly.
Lastly, I found I could add to driver_opts
:
com.docker.network.bridge.host_binding_ipv4: "10.1.1.100"
This made it impossible to start the container similar to the same error I received when using the ports
method.