1

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.

Kevin Ghadyani
  • 6,829
  • 6
  • 44
  • 62
  • check this https://stackoverflow.com/questions/27937185/assign-static-ip-to-docker-container – J-Jacques M Mar 18 '20 at 09:05
  • Yeah, none of those work; especially not the widely-accepted answer :/. – Kevin Ghadyani Mar 18 '20 at 09:49
  • In most environments you can't reach the container-private IP addresses from outside of Docker. Instead you need to publish specific `ports:` from containers and you can reach them via the host's IP address. Vagrant's VM-based networking setup is just different. – David Maze Mar 18 '20 at 10:21
  • My point is that Vagrant allows reaching container private IPs, at least the exposed one, even with Docker containers. They've figured something out. – Kevin Ghadyani Mar 18 '20 at 18:12

0 Answers0