docker / machine

Machine management for a container-centric world
https://docs.docker.com/machine/
Apache License 2.0
6.63k stars 1.97k forks source link

[Feature Request] Specify a static IP for VirtualBox VMs #1709

Open letsgolesco opened 9 years ago

letsgolesco commented 9 years ago

Hi, I've been searching through the docs and issues to figure this out but can't find an answer.

Basically, I want to be able to specify the IP address of a VM (i.e. the value that's listed under "URL" in docker-machine ls) when I create it with docker-machine create.

I want this because I've been relying on boot2docker's default address of 192.168.59.103, but now it varies from machine to machine.

Thanks!

mrichard commented 8 years ago

+1

lukasvice commented 8 years ago

+1

micheletedeschi commented 8 years ago

Docker tell me to regenerate certs if the IP changes, i need to set a static IP to avoid this problem or there is another solution?

eval "$(docker-machine env machine-1)"

Error running connection boilerplate: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.101:2376": x509: certificate is valid for 192.168.99.108, not 192.168.99.101
You can attempt to regenerate them using 'docker-machine regenerate-certs name'.
Be advised that this will trigger a Docker daemon restart which will stop running containers.
hagzag commented 8 years ago

+1 any ETA on this feature ?

schmunk42 commented 8 years ago

A workaround for some use-cases could be to create machines like so:

192.168.98.100

docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.98.1/24" m98

192.168.97.100

docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.97.1/24" m97

192.168.96.100

docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.96.1/24" m96

If there's no other machine with the same cidr, the machine should always get the .100 IP upon start.

tonivdv commented 8 years ago

@schmunk42 very good work around :+1:

micheletedeschi commented 8 years ago

My virtualbox has dhcp range 192.168.99.100 - 255 and I want to set an IP before 100.

I've found a simple trick to set a static IP: after create a machine i run this command and restart the machine:

echo "ifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null

This command create a file bootsync.sh that is searched by boot2docker startup scripts and executed.

Now during machine boot the command is executed and set static IP.

docker-machine ls
NAME              ACTIVE   DRIVER       STATE     URL                                      SWARM
test-1                      -        virtualbox     Running   tcp://192.168.99.50:2376      test-1 (master)
jgsqware commented 8 years ago

That's a good trick also @micheletedeschi :+1:

tino commented 8 years ago

I like the way @micheletedeschi fixed this. Seems like an easy and standard way that doesn't involve modifications of docker-machine. Maybe this should be documented somewhere?

micheletedeschi commented 8 years ago

You can find some documentation here or here

ashatrov commented 8 years ago

+1 I am working with 3 VM and i always have problems to connect via ssh and configuring my utils, hosts file etc.

stepanpelc commented 8 years ago

+1

alexw23 commented 8 years ago

:+1:

caledhwa commented 8 years ago

+1

ashic commented 8 years ago

+1

jwboardman commented 8 years ago

+1

kandalva commented 8 years ago

+1

pcattori commented 8 years ago

+1 I'm trying to automate machine creation and can't expect to have permissions to add entries to /etc/hosts over and over. Having a stable ip would mean only having to set this up once.

alexw23 commented 8 years ago

I ended up setting up a script that pre fills dnsmasq on the host using docker-machine ip

On 19 Dec 2015, at 12:18 PM, Pedro Cattori notifications@github.com wrote:

+1

— Reply to this email directly or view it on GitHub.

royling commented 8 years ago

A very useful feature :+1:

drags commented 8 years ago

Loving @micheletedeschi 's fix, though I wound up putting the IP on a virtual interface (replace eth1 with eth1:1 in the above command) since the eth1 interface is running DHCP and will eventually revert to the DHCP assigned IP address.

Not sure how long the dhcp lease refresh time is (ran for a day with the initial fix before I suddenly got the "you need to regenerate TLS certs" error and realized what was going on). Will report back if putting the IP on a virtual interface doesn't stick

micheletedeschi commented 8 years ago

@drags I've updated the commands with:

echo "kill `more /var/run/udhcpc.eth1.pid`\nifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null

then run command (only the first time)

docker-machine regenerate-certs prova-discovery

now the IP will not be changed by the DHCP

willseward commented 8 years ago

:+1:

tbrn commented 8 years ago

+1

skiss9 commented 8 years ago

:+1:

sarink commented 8 years ago

+1

scottgerard commented 8 years ago

+1

dcosson commented 8 years ago

+1

dcosson commented 8 years ago

Has anyone had luck scripting @micheletedeschi 's workaround? I want a single command everyone on the team can run to launch the dev vm. I tried the following:

docker-machine create -d virtualbox dev
echo "ifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh dev sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null
sleep 30
docker-machine restart dev
sleep 30
docker-machine regenerate-certs -f dev

When I run the commands manually it works. In the script, I had to put in the sleep 30 or the restart command would just hang. Now the script finishes successfully but after it runs the vm is still running with the old IP.

sarink commented 8 years ago

You could use vagrant to launch the docker machine and then use a plugin like vagrant-hostmanager. But of course then you've got other problems to deal with if you want it to be a single command to launch the VM cross-platform.

There's just no Perfect Solution at the present time (afaict).

dcosson commented 8 years ago

Yeah, I think I'll stick with Vagrant for now.

I'm building a Vagrant box image with docker running with DOCKER_OPTS="-H 0.0.0.0:2375". Then I can create a vagrant vm with a private network hard-coded IP and use that IP to set DOCKER_HOST and /etc/hosts, which I think will be a pretty ideal setup.

wuqionglinux commented 8 years ago

+1,especially working with machine driver vmwarevsphere

jamesma commented 8 years ago

+1

Jcamilorada commented 8 years ago

+1

bmoussaud commented 8 years ago

+1

dansandland commented 8 years ago

+1, just being able to set a machine's IP address. Currently the CLI only supports getting it. Using AWS Elastic IP's would require this. Otherwise the machine's config.json has to be manually updated.

~/.docker/machine/machines/my-machine-with-aws-elastic-ip/config.json

roocell commented 8 years ago

@dcosson w.r.t scripting.

I just start the docker-machine then run the following 2 commands (one to kill the dhcp client, two to set a static ip). I chose not to run 'docker restart' to trigger bootsync.sh. Perhaps that startup script doens't work? It's also possible the dhcp client was messing you up and resetting the eth1 IP on you.

echo "kill `more /var/run/udhcpc.eth1.pid`\nifconfig eth1 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh <machine-name> sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null
docker-machine ssh <machine-name> "sudo ifconfig eth1 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255 up"
shouro commented 8 years ago

+1

DieterVanmoortel commented 8 years ago

+1

imranraja85 commented 8 years ago

I had to slightly modify @roocell's snippet to kill the dhcp server immediately because I didn't want to reboot. Otherwise it would revert after a few minutes.

echo "kill `more /var/run/udhcpc.eth1.pid`\nifconfig eth1 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh <machine-name> sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null

docker-machine ssh <machine-name> "sudo cat /var/run/udhcpc.eth1.pid | xargs sudo kill"

docker-machine ssh <machine-name> "sudo ifconfig eth1 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255 up"
jquacinella commented 8 years ago

I have a feeling that not setting a static ip on my machine running consul is what is giving me issues with respect to my swarm cluster. +1 for this

milesibastos commented 8 years ago

+1

adithep commented 8 years ago

+1

samm0ss commented 8 years ago

+1

skirdey commented 8 years ago

+1

joshbedo commented 8 years ago

+1

OpakAlex commented 8 years ago

+1_000000 :)

rafarocha commented 8 years ago

+1

VonC commented 8 years ago

Based on @micheletedeschi's comment, here is my script to set a fixed IP to a docker-machine VM:
http://stackoverflow.com/a/35367277/6309

jimzucker commented 8 years ago

+1