Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign specific IP to container and make that accessible outside of VM host?

I wish to make two of my containers available outside of the VM host on their separate, specific IP addresses (192.168.0.222, 192.168.0.227), without port mapping. That means I wish to access any port directly on the containers by using its IP. I already have machines running in the network outside of the VM host in the range 192.168.0.1–192.168.0.221.

Is this now possible with Docker 1.10.0, and if so, how?

I'm on OS X 10.11 with docker version 1.10.0, build 590d5108 and docker-machine version 0.6.0, build e27fb87, using boot2docker/VirtualBox driver.


I have been trying to figure this out for some while, without luck, and I've read the following questions and answers:

  • How to assign static public IP to docker container
  • How to expose docker container's ip and port to outside docker host without port mapping?
  • How can I make other machines on my network access my Docker containers (using port mapping)?
like image 455
fredrik Avatar asked Oct 19 '22 16:10

fredrik


1 Answers

According to Jessie Frazelle, this should now be possible.
See "IPs for all the Things"

This is so cool I can hardly stand it.

In Docker 1.10, the awesome libnetwork team added the ability to specifiy a specific IP for a container. If you want to see the pull request it’s here: docker/docker#19001.

# create a new bridge network with your subnet and gateway for your ip block
$ docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic

# run a nginx container with a specific ip in that block
$ docker run --rm -it --net iptastic --ip 203.0.113.2 nginx

# curl the ip from any other place (assuming this is a public ip block duh)
$ curl 203.0.113.2

# BOOM golden

That does illustrate the new docker run --ip option that you now see in docker network connect.

If specified, the container's IP address(es) is reapplied when a stopped container is restarted. If the IP address is no longer available, the container fails to start.

One way to guarantee that the IP address is available is to specify an --ip-range when creating the network, and choose the static IP address(es) from outside that range. This ensures that the IP address is not given to another container while this container is not on the network.

$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network

$ docker network connect --ip 172.20.128.2 multi-host-network container2

The "making accessible" part would involve, as usual, port forwarding.

like image 154
VonC Avatar answered Oct 31 '22 22:10

VonC