Machine: [Feature Request] Specify a static IP for VirtualBox VMs

Created on 16 Aug 2015  ·  212Comments  ·  Source: docker/machine

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!

drivevirtualbox kinenhancement

Most helpful comment

Guys, come on. Could we please stop +1 on an obviously long-running issue? There was a Like button implemented for that particular reason. No need to spam the subscribers.

All 212 comments

+1, need this feature

+1

+1

+1

Here's a good workaround. Add the following to your ~/.bash_profile:

eval `docker-machine env <docker-machine name>`
export <some variable>=$(docker-machine ip <docker-machine name>)

@letsgolesco you can use --virtualbox-hostonly-cidr option for that, which will select the proper vboxnetX host-only adapter.

By default, seems docker-machine picks one randomly, so forced to use the one for vboxnet0 (192.168.59.3/24, the one used by boot2docker) seems to solve the issue:

$ docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "192.168.59.3/24" dev
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine env dev

$ docker-machine ip dev
192.168.59.103

Cheers.

@luislavena - Unfortunately that did not work.

+1

Def need this feature though!

@krinkere not sure your env, but this worked for my OSX installation by doing that, but failed when using machine on Linux, which required tweaks to dhcpserver configuration of vboxnet0:

$ VBoxManage dhcpserver modify --ifname vboxnet0 --disable

$ VBoxManage dhcpserver modify --ifname vboxnet0 --ip 192.168.59.3 --netmask 255.255.255.0 --lowerip 192.168.59.103 --upperip 192.168.59.203

$ VBoxManage dhcpserver modify --ifname vboxnet0 --enable

$ docker-machine create --driver "virtualbox" --virtualbox-cpu-count "-1" --virtualbox-disk-size "30000" --virtualbox-memory "2560" --virtualbox-hostonly-cidr "192.168.59.3/24" dev

This is, as you can see, a huge hack :smile_cat:, but works for now :smile:

Would love to see this feature!!

@luislavena - Thanks for your response

Here is what I get on my Mac.

$ docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "192.168.99.3/24" dev
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine env dev

$ docker-machine ip dev
192.168.99.100

While I do have ip in the right subnet, it is not the IP that I would want to use... Def would love to see this feature

+1 usefull feature

+1 This feature would really help us out!

We use docker-machine to run a local Elasticsearch cluster and we have /etc/hosts entry for 192.168.59.103 which maps to elastic-local.mycompany.com. Since we have moved over to docker-machine from boot2docker, we have had to continuously manually update this hosts file entry to whatever is the new IP assigned to our docker VM. We could write some automation which would update this hosts file entry for us but it would be much simpler if docker-machine just let us use a configurable IP address for new machines.

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1 (y)

+1

+1

+1

Temporarily I am using some bash scripts to update the hosts files but is kludgy since you have to update /etc/hosts via sudo'd script but since you can get ip from docker-machine it works. I may share a gist.

ianlintner-wf, do share the script please. I'm about to write one exactly like that myself, but if I can not reinvent the wheel that'd be great.. yeah..

Here is the primary script I am using. I cleaned it up a little, but it lacks more formal default params and other stuff.

https://gist.github.com/ianlintner-wf/a2b917158aaf100d9fb3

+1 please!

thanks for the script @ianlintner-wf. I have written one that dynamically adds the host/ip of any running docker machines. Have tested on OS X El Capitan.

https://gist.github.com/cookandy/f14fadd31721dc6e136e

The IP increment is cached by VirtualBox DHCP server. Stop and restart VirtualBox will reset the IP increment.

+1, our OSX devs are still using boot2docker because our env relies on static /etc/hosts entries. (btw thanks for the script @ianlintner-wf )

+1

+1

+1

+1

+2

My hacky-hack solution, which is definitely not for everyone: I'm running Cassandra in a docker machine, and I want to be able to get to it from the node.js app I'm developing. Rather than use a fixed IP for the docker machine, I do:

docker-machine stop dockervm
VBoxManage modifyvm "dockervm" --natpf1 "tcp-port9160,tcp,,9160,,9160"
VBoxManage modifyvm "dockervm" --natpf1 "tcp-port9042,tcp,,9042,,9042"
docker-machine start dockervm
eval "$(docker-machine env dockervm)"

which enables port forwarding for port tunnelling from localhost to the docker VM. Now I can just connect to localhost:9042. (This would be much easier if someone were to fix https://github.com/docker/machine/issues/41. :D)

@luislavena thank you, just recreated default machine with these options and everything works fine now

+1

+1, I will try the script with the automatic update of hosts file, but definitely I want this feature!

Based on @cookandy & @ianlintner-wf script, I've created mine.

You can configure it with a map between docker-machine-name and host name.

It will update your hosts by removing older docker machine ips and a new ones

https://gist.github.com/juliengarcia/9a208ca5cf6590b25150

+1

:+1:

+1

+1

:+1:

+1

+1

+1

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.

+1 any ETA on this feature ?

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.

@schmunk42 very good work around :+1:

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)

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

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?

You can find some documentation here or here

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

+1

:+1:

+1

+1

+1

+1

+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.

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 [email protected] wrote:

+1


Reply to this email directly or view it on GitHub.

A very useful feature :+1:

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

@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

:+1:

+1

:+1:

+1

+1

+1

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.

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).

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.

+1,especially working with machine driver vmwarevsphere

+1

+1

+1

+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

@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"

+1

+1

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"

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

+1

+1

+1

+1

+1

+1_000000 :)

+1

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

+1

+1

+1

+1

What i have done is to create a copy of your hosts file anywhere.

Set a placeholder for the parts you would like to dynamically change

<docker-machine-ip> doctify.local.com
<docker-machine-ip> doctify.local.co.uk
<docker-machine-ip> www.doctify.local.com
<docker-machine-ip> www.doctify.local.co.uk

FYI: I am using gnu-sed as my default sed so it is not going to be the one on your mac
brew install gnu-sed --with-default-names

and in my start script i just sed replace those and then switch out the hosts file. Now if you have your copy of your hosts file the same as before. it should not affect any of your other services

#!/bin/bash

echo
echo "########################################################"
echo "####              Update Docker Dev Hosts           ####"
echo "########################################################"
echo

machine_name="doctify-dev"

update_hosts() {
    echo
    # Copy over default hosts file
    echo "Copy Default Hosts..."
    cp -fv nginx/hosts docker/hosts

    echo
    # Replace with actual ip address
    echo "Replace hosts file..."
    echo "$machine_name IP ($machine_ip)"
    sed -i -e "s|<docker-machine-ip>|$machine_ip|g" docker/hosts

    echo
    # Move to /etc/hosts
    echo "Move hosts to correct location..."
    sudo mv -fv docker/hosts /etc/hosts

    echo
    # Remove copied hosts file
    echo "Clean up hosts file if it still remains..."
    rm -rfv docker/hosts
}

echo
# Get Current IP
echo "Retrieving Current IP Address..."
ip=($(cat /etc/hosts | grep doctify.local.com | sed -n '1p'))
echo "Current IP: $ip"

echo
# Get IP address of docker machine
echo "Get Current Machine IP address..."
machine_ip=$(docker-machine ip $machine_name)
echo "Machine IP: $machine_ip"

echo
if [ "$ip" = "$machine_ip" ]; then
    echo "IP Addresses are matching. Exiting..."
else
    echo "IP Addresses differ. Updating..."
    update_hosts
fi

Start Script:

#!/bin/bash

echo
echo "########################################################"
echo "####            Start Docker Dev Environment        ####"
echo "########################################################"
echo

$machine_name=doctify-dev

echo
echo "Start Machine..."
docker-machine start $machine_name

echo
echo "Attach Machine to Terminal..."
eval $(docker-machine env --shell bash $machine_name)

echo
echo "Update hosts IP..."
source docker/update-hosts

echo
echo "Set Machine IP as Hostname..."
export DOCKER_HOST_IP=$(docker-machine ip $machine_name)

echo
echo "Clean Docker Images..."
source docker/clean

echo
echo "Build Docker Images..."
docker-compose build

echo
echo "Start Docker Images..."
sh docker/bin/osx-dev -r "docker-compose up" -i ".rsyncignore"

+1

+1

+1

+1

+1

+1

+1

+1

Here's what I use to create my docker-machine environments with consistent IPs:

#--- Usage: d-machine-static new_dm_name new_dm_ip [new_dm_memory]
function d-machine-static {
    new_dm_name="${1}"
    new_dm_ip="${2:-192.168.90.100}"
    new_dm_memory="${3:-1024}"
    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "${new_dm_ip}/24" --virtualbox-memory "${new_dm_memory}" "${new_dm_name}"
    echo -e "\033[1;34m${new_dm_name}\033[0m = \033[0;32m$(/usr/local/bin/docker-machine ip ${new_dm_name})\033[0m"
}

Some IPs tend to succeed more often than others, and I've had the best luck with those of the form 192.168.x.100 such that x < 99.

And here's what I use to quickly switch between them:

#--- Switch to a different docker-machine environment.
function d-env () {
    target_environment="${1}"
    if [[ $(/usr/local/bin/docker-machine status ${target_environment}) = "Stopped" ]]; then
        docker-machine start "${target_environment}"
        sleep 1
    fi
    eval "$(/usr/local/bin/docker-machine env ${target_environment})"
    echo -e "\033[1;34m${target_environment}\033[0m = \033[0;32m$(/usr/local/bin/docker-machine ip ${target_environment})\033[0m"
}

Feel free to use, modify, and distribute to your heart's content.

+1

I also had to add the default gateway...

Here's how I do this on Windows 10 using PowerShell & Hyper-V (with VMNat.dll service for NAT instead of ICS).

  • subnet is 192.168.233.0/24
  • DHCP server is running on .1
  • DNS & NAT on .2
  • new docker-machine to take .10 static IP

`````` PowerShell
docker-machine create --driver hyperv --hyperv-virtual-switch "VMWare NAT" --hyperv-memory "512" consul0

yet to test if default gateway works after reboot....

echo "kill ``more /var/runudhcpc.eth0.id```nifconfig eth0 192.168.233.10 netmask 255.255.255.0 broadcast 192.168.233.255 up" | docker-machine ssh consul0 "sudo tee /var/lib/boot2docker/bootsync.sh" > $null

kill dhcp on eth0 interface right away

docker-machine ssh consul0 "sudo cat /var/run/udhcpc.eth0.pid | xargs sudo kill"

bring up eth0 with new static IP..

following command hangs... I have to CTRL+C after a while...

docker-machine ssh consul0 "sudo ifconfig eth0 192.168.233.10 netmask 255.255.255.0 broadcast 192.168.233.255 up"

fix the tls certs which are for the old dynamic IP

docker-machine regenerate-certs consul0

ensure internet still works by re-adding default gateway (it went missing in my case)

docker-machine ssh consul0 "route add default gw 192.168.233.2"
``````

good to know:

#get the ip
$(Get-VM consul0).NetworkAdapters[0].IPAddresses[0]

#open vmconnect
$vm=Get-VM consul0

vmconnect $env:COMPUTERNAME $vm.Name -G $vm.Id

Note: Sometimes the boot2docker init seems to fail and Hyper-V can't get the Adapter IP...

+1

+1

We re still in plus one mode? :)

Its screwing up my settings all the time and would totally make sense to give fixed ips especially when you run multiple dev environments for different projects locally

We're awaiting confirmation that the above strategy works for others using docker-machine on Mac OSX. Here it is in a more concise form:

1) Set the variable x to be any whole number between 1 and 98. For example:
x='42'

2) Set the variable new_dm_name to be the name of your new docker-machine environment. For example:
new_dm_name='magic'

3) Run this command:
docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.$x.100/24" "$new_dm_name"

Your new static IP should now be 192.168.x.100 and should persist between sessions so long as you don't assign that IP to anything else. To confirm that this is the case I recommend running these commands:

docker-machine ip "$new_dm_name"
docker-machine stop "$new_dm_name"
docker-machine start "$new_dm_name"
docker-machine ip "$new_dm_name"

With any luck this will be adaptable to Windows as well.

+1

well, I'm doomed. My company is using 192.168.99.x for our own local ip, so please allow this to be changed.

@xbeta - you can just reconfigure the VBox network? I wouldn't be able to tell you how to do that as I'm using Hyper-V, but if you use the default VBox driver, just have a look at the virtual nics created by that, docker-machine is just wrapping around VBox

+1

Could people please, please, please stop with the +1 comments? Github now has a +1 feature. Just click the +Smiley in the top right, and click the thumbs up icon. It conveys your support without emailing everybody who's commented here. #kthxbye

https://blog.docker.com/2016/03/docker-for-mac-windows-beta/

Faster and more reliable: no more VirtualBox!

Looks like Docker is ditching VirtualBox. You may want to adjust your expectations on this issue ever getting a fix.

+1

+1

+1

:+1:

+1

+1

+1

Yeah, please be nerds, keep plussing. It instills so much confidence.

@everett-toews VirtualBox is still useful for multi host docker solutions like Swarm, Kubernetes, Nomad, etc. Docker for Mac is equivalent to a one host setup (your mac is the host).
Another local solution is VmwareFusion which does keep the ip of a host when is it restarted. This is a recent fix btw.
So yes, we need an easier way to specify static ip's for VirtualBox.

@gittycat Yep. And that's why I still run Docker Toolbox. Just so I can create a Swarm. Even so, I'd rather see this Feature Request: Multiple VMs in Docker for Mac/Windows.

+1

+1

+1

+1

+1

If you need to set up a static ip for a boot2docker machine just edit /var/lib/boot2docker/profile and add

sudo cat /var/run/udhcpc.eth1.pid | xargs sudo kill 
sudo ifconfig eth1 <ip address to assign> netmask <subnet mask> broadcast <broadcast address> up

After that you need to regenerate the certs
docker-machine regenerate-certs rancher -f

+1

I think it's time, at least for Mac n Windows, to close this request as irrelevant. All of my problems around docker-machine ip were solved by docker for Mac. Thoughts everyone?

@caledhwa agree, docker mac is quite stable now cant see any reason to deal with docker-machine anymore

@caledhwa Docker for Mac only supports one machine - if you have a multi machine setup, this is still relevant.

@caledhwa as @davidolrik said, Docker for Mac/Windows/AWS/Azure only support 1 host which rules it out for multi-node Swarm/Kubernete/Mesos/etc development, demos and testing.

👍

+1

Still no movement on this? This is a blocker for using docker-machine as I cannot change the conflicting network address of the guest.

To be able to fix the docker machine IP for virtualbox is necessary feature.

I setup a several docker hosts as an swarm example, and after the reboot, almost every host's IP changed, and will got the error:

x509: certificate is valid for 192.168.99.103, not 192.168.99.100

Yes, I might be able to use docker-machine regenerate-certs to fix the certificate problem, however, the key-value store's IP also changed, and the result is my swarm stopped working, until I manually change them in the configuration one by one again.

Maybe it will be better to just remove them and create those host again. However, every images/containers in those hosts will be lost.

So, please make it possible to specify IP of the host, or just automatically allocate fixed IP for the virtualbox docker host, and don't let it change over the reboot.

+1
I have a case where I use dnsmaq for DHCP and DNS, based on the comments above I tried disabling the DHCP server of the virtual box interface in my case its vboxnet4 like VBoxManage dhcpserver remove --netname HostInterfaceNetworking-vboxnet4 and need to reboot the machine to pick the DHCP range from dnsmasq, the issue am facing is that on every new machine creation the docker-machine is enabling dhcp sever on vboxnet4 and other servers using dnsmasq are not able to look up the docker-machine(s), i have to repeat the removal of vnet and then reboot docker-machines. it will be great if we can add a flag to docker-machine-create to help no enable dhcp kind of thing.

+1

+1

+1

+1

Here's a command that encapsulates @micheletedeschi's work and provides some convenience for switching between DHCP and static configurations. It also updates your /etc/hosts file if requested.

https://github.com/fivestars/docker-machine-ipconfig

We've been using it for the last few weeks with great success. Just run it right after creating your docker machine.

Usage: docker-machine-ipconfig <command> args...

Commands:
    ls                             List running docker-machines' ip addresses

    static <machine> [ip-address]  Configure <machine> to use a static IP address
                                   (default is current address)

    dhcp <machine>                 Configure <machine> to use DHCP client to gather IP address

    hosts                          Update /etc/hosts file

Everyone was thinking on a more "generic" solution (docker-machine native feature) as this option is interesting also on Vsphere, Azure and other deploy environments. But it's truly welcome.

+1

+1

+1

+1

+1

+1

@micheletedeschi's workaround more or less works for me, but it appears that the ip occasionally reverts from the one I set it to with his script (192.168.99.100:2376) to the one that was auto generated when I created my machine (192.168.99.104:2376). Fairly new to docker, anyone have any insight?

+1, ran into same issue today using ZooKeeper and VirtualBox

@micheletedeschi's workaround more or less works for me, but it appears that the ip occasionally reverts from the one I set it to with his script (192.168.99.100:2376) to the one that was auto generated when I created my machine (192.168.99.104:2376). Fairly new to docker, anyone have any insight?

Are you killing the DHCP client? After it reverts, can you confirm the DHCP client is still not running?

I found another workaround that finally works for me: I simply start the swarm agent on each machine using _advertise=$(ifconfig eth1 | grep -Eo "192.168.99.1[0-9]{2}"):2376_ and publish the current IP to ZooKeeper (or any other discovery service). This is ok in my case since I can rely on each machine getting assigned a 192.168.99.1** address (it's just an experimental setup). think there're more advanced regex expressions that can be applied to arbitrary IP ranges. This is not nice and might be kinda hacky, but it does the job.

+1

+1

Arbitrarily-changing VM IPs and TLS certs that suddenly become invalid upon reboot has caused lots of gotcha moments and wasted man-hours

Is this even prioritised? A whole 13 months this case has been open. +10000

My guess is that there is a philosophical disagreement between some people on the docker machine team and the rest of the world on this issue. Using DHCP and having a non deterministic IP is pretty much worthless for all of my uses cases.

I have been using this workaround for the last year. It is adapted from some of the one-liner workarounds described above.

SSH onto the docker machine
docker-machine ssh default

Add/edit the boot2docker startup script
sudo vi /var/lib/boot2docker/bootsync.sh

#!/bin/sh
/etc/init.d/services/dhcp stop
ifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up

sudo chmod 755 /var/lib/boot2docker/bootsync.sh
exit

Restart machine and regenerate certs
docker-machine restart default
docker-machine regenerate-certs default

+1

+1

+1 (for docker-machine and jimmidyson/minishift)

+1

@eytanhanig Unfortunately your method did not work for me. I'm on OS X 10.11.6. The machines get the desired IPs but there is an error connecting to them. Docker suggests regenerating keys, but that did not fix the issue.

+1

+1

It's ridiculous how long people have been +1ing this.

+1

I love the have this feature, however, I didn't find any elegant approach to implement it.

The problem is caused by the DHCP in Virtualbox, if the system reboot, the DHCP server will lost all allocated IPs data, and try to reallocate the IP for those VM, which not likely to have the same IP for the last time.

I'm not sure it's right to let the Virtualbox to persistent the DHCP data. And Boot2docker will not persistent the IP allocated by DHCP, which is actually correct behavior.

IMHO, I don't think try to workaround the DHCP is the right way to do, I think adding the ability to specify a static IP to Boot2docker and docker-machine virtualbox driver should solve the problem.

It's maybe better to implement the static IP in https://github.com/boot2docker/boot2docker project first, such as, adding an ability to be specified a static IP, rather than using DHCP.

Here is the code to triggers DHCP call:

https://github.com/boot2docker/boot2docker/blob/master/rootfs/rootfs/bootscript.sh#L16

# Trigger the DHCP request sooner (the x64 bit userspace appears to be a second slower)
echo "$(date) dhcp -------------------------------"
/etc/rc.d/dhcp.sh
echo "$(date) dhcp -------------------------------"

We can make it conditional, by adding a set of IP configuration in /var/lib/boot2docker/profile, and if a static IP has been configured, then use the static IP instead of request IP from DHCP.

Then, we can add one or more options for virtualbox driver to specify the static IP configuration, such as --virtualbox-boot2docker-cidr, --virtualbox-boot2docker-gateway, --virtualbox-boot2docker-dns ...

+1

+1

+1

Guys, come on. Could we please stop +1 on an obviously long-running issue? There was a Like button implemented for that particular reason. No need to spam the subscribers.

No need to spam the subscribers.

You can unsubscribe.
1

I'm all for spamming the subscribers.

Some of those subscribers are part of the project team.

On 26 Jan 2017 14:20, "Sergey Nevmerzhitsky" notifications@github.com
wrote:

No need to spam the subscribers.

You can unsubscribe.
[image: 1]
https://cloud.githubusercontent.com/assets/1006042/22332699/4cf1a8b4-e3e3-11e6-8bad-52406c3b8055.png


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/docker/machine/issues/1709#issuecomment-275386350,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AG44NZ2KdnsH6JTU6iI_5Uieh9WZxWXBks5rWJ2RgaJpZM4FsQYg
.

I'm all for spamming the subscribers.
Some of those subscribers are part of the project team.

Maintainer checking in. Please _do not_ do spam the issue with +1 comments. This is a well known issue and spamming the thread doesn't do any good. Indicate support through the "+1" emoji reactions if you'd like. As always, we're happy to evaluate pull requests or detailed proposals. Thanks all.

(As a side note, @twang2218 seems to have the right idea generally -- static IPs are trickier than they look to get right and hit the use cases subscribers to this thread might have in mind -- perhaps an issue could be started in the boot2docker repo to discuss.)

I do not really want a static ip. But once the ip address is picked during creation, it should keep it on subsequent restarts. Is this already available?

I had a docker with VPN and the standard 192.168.99.100 wasn't loading. I figured this was because of the VPN connection, so I setup a bridge network inside Virtualbox to access the docker via 192.168.1.159. I would like this to show up on Kitematic's published IP address. Is there any way to do so?

For those who can not wait this feature to be implemented, assigning custom ip, plus custom cpu/memory is possible by using Vagrant VirtualBox provider and docker-machine generic driver.

For example in windows, place this Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "AlbanMontaigu/boot2docker"
  config.vm.provider "virtualbox" do |v|
    v.memory = 8192
    v.cpus = 8
  end
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.synced_folder "C:/Users", "/c/Users"
  config.vm.network "private_network", ip: "192.168.99.103"
end

and place vagrant default ssh private key with name vagrant.pem

and run

vagrant up
docker-machine create --driver generic --generic-ip-address=192.168.99.103 --generic-ssh-user=docker --generic-ssh-key=vagrant.pem default
docker-machine ls

and you'll get docker host with custom ip, cpu, memory etc configured via vagrant at the same time accessible via docker-machine.

You can find working file set for Windows in https://github.com/joelhandwell/dockerhost

@jcwilson Hi Josh et al, thanks a lot.. I forked your script in order to fix the same issue (see https://github.com/minishift/minishift/issues/343 , https://github.com/minishift/minishift/issues/598) in combination with minishift:

https://github.com/ahilbig/docker-machine-ipconfig

Any news?

Currently, I am experimenting with this for Minishift: https://github.com/minishift/minishift/issues/1293
Although, another approach would be to use something like cloud-init (which is maybe to heavy for this approach, or embed a DHCP server inside the Go-based binary and use this to force an allocated IP address)

+1.
Currently having more than one machine is horrific pain. If you use additional certificates (for example, for custom docker repository) and change order of starting machines, you have to do:

Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.100:2376"
: x509: certificate is valid for 192.168.99.101, not 192.168.99.100

This forces you to regenerate certificates or disable all machines and restore order of executing (what sometimes can be painful, too).

Additionally, using Chef to configure Docker-based project would also be much better if you could assume created machine would have always the same IP (for example, adding it to hosts file and you could forget about IPs).

+1

+1

+1
thx jcwilson, the script docker-machine-ipconfig is very good! i needed to alter the eth1 to eth0 in the script, after this, the script working perfect for me. I'm use vmware vsphere.

bye.

+1

As part of minishift, we have implemented https://github.com/minishift/minishift/issues/1316 and this has been working well so far. We are now looking into alternatives to do the same for the other hypervisors over time, but first we will fix the address using https://github.com/minishift/minishift/issues/1457 (to prevent a new address from being assigned). We haven't been able to reserve time yet, we are going to land this in minikube too.

+1

The simplest workaround which I found is to rely on sequence of starting virtual machines by doker-machine.

VirtualBox has DHCP server which starts assigning IP adresses from 192.168.99.100.

  1. Stop all VMs
    docker-machine stop ${docker-machine ls -q}
    This step will release all assigned addresses to VirtualBox DHCP

  2. Start VMs in order
    docker-machine start vbox1 && docker-machine start vbox2 && docker-machine start vbox3
    This steps assign IP like this:
    192.168.99.100 : vbox1
    192.168.99.101 : vbox2
    192.168.99.102 : vbox3

Notice that docker-machine start vbox1 vbox2 vbox3 will not work, but I don't figure out why.

+1

when i run docker-machine.exe start, it takes more than 15 sec for DHCP waiting for an IP(if internet is broken, it takes more than 2minutes), i think if can set a static ip, it can boot fast.

This functionality has been added to Minishift spome time ago, but it
needs a modification to the ISO to function.
On Hyper-V we ran into issues that the boot2docker image does not
played nicely... so it only worked on CentOS and Fedora based images.
Also, this does not work with the current KVM driver as it relies on DHCP.

This would need a slight redesign...

On Sun, Feb 3, 2019 at 11:08 PM fcying notifications@github.com wrote:
>

when i run docker-machine.exe start, it takes more than 15 sec for DHCP waiting for an IP(if internet is broken, it takes more than 2minutes), i think if can set a static ip, it can boot fast.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

--

Gerard Braad | http://gbraad.nl
[ Doing Open Source Matters ]

The simplest workaround which I found is to rely on sequence of starting virtual machines by doker-machine.

VirtualBox has DHCP server which starts assigning IP adresses from 192.168.99.100.

  1. Stop all VMs
    docker-machine stop ${docker-machine ls -q}
    This step will release all assigned addresses to VirtualBox DHCP
  2. Start VMs in order
    docker-machine start vbox1 && docker-machine start vbox2 && docker-machine start vbox3
    This steps assign IP like this:
    192.168.99.100 : vbox1
    192.168.99.101 : vbox2
    192.168.99.102 : vbox3

Notice that docker-machine start vbox1 vbox2 vbox3 will not work, but I don't figure out why.

Sometimes I had to kill VBoxNetDHCP.exe process on Windows, to make vbox assign IPs starting from 192.168.99.100
Otherwise, vbox would not release IPs used by previous machines (not sure why)

👍

It would really make my life easier.

The simplest workaround which I found is to rely on sequence of starting virtual machines by doker-machine.

VirtualBox has DHCP server which starts assigning IP adresses from 192.168.99.100.

  1. Stop all VMs
    docker-machine stop ${docker-machine ls -q}
    This step will release all assigned addresses to VirtualBox DHCP
  2. Start VMs in order
    docker-machine start vbox1 && docker-machine start vbox2 && docker-machine start vbox3
    This steps assign IP like this:
    192.168.99.100 : vbox1
    192.168.99.101 : vbox2
    192.168.99.102 : vbox3

Notice that docker-machine start vbox1 vbox2 vbox3 will not work, but I don't figure out why.

It's very useful.

In order to assign a static ip address 192.168.99.50 to my docker-machine (default) on VirtualBox, I used:

$ echo -e "kill \`cat /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 default sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null

$ docker-machine restart default
Restarting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Waiting for SSH to be available...
Detecting the provisioner...
Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

$ docker-machine env
Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.50:2376": x509: certificate is valid for 192.168.99.103, not 192.168.99.50
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
Be advised that this will trigger a Docker daemon restart which might stop running containers.

$ docker-machine regenerate-certs default
Regenerate TLS machine certs?  Warning: this is irreversible. (y/n): y
Regenerating TLS certificates
Waiting for SSH to be available...
Detecting the provisioner...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...

$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.50:2376"
export DOCKER_CERT_PATH="/Users/mandrake/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env)

My environment is OSX, VirtualBox (boot2docker.iso)

Hoping this can be useful 🙂
BTW: +1 for the new feature request 😉
Cheers.

Yes, there is an option to create with specific ip. All you need to do is specify ip/ip-range with
--virtualbox-hostonly-cidr option.

For example:

  • when you want to assign specific ip, let say you want to assign 10.15.1.24, then appropriate config will be (the most important thing is the mask that we are using here. For specific ip you need to set subnet mask to 32) :
    docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "10.15.1.24/32" dev

  • if you want to pick an ip from specific ip range, then you need to use appropriate subnet mask.

If you issue is that you have to keep updating your host file you can just install Avahi on your container and use mDSN.

Was this page helpful?
0 / 5 - 0 ratings