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!

pangch commented 8 years ago

+1

svetlyak40wt commented 8 years ago

+1

fredboutin commented 8 years ago

+1

devotox commented 8 years ago

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"
timstott commented 8 years ago

+1

syardumi commented 8 years ago

+1

BrianHutchison commented 8 years ago

+1

Hyunk3l commented 8 years ago

+1

fernaspiazu commented 8 years ago

+1

stumpdk commented 8 years ago

+1

jimzucker commented 8 years ago

+1

Junus commented 8 years ago

+1

eytanhanig commented 8 years ago

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.

tahb commented 8 years ago

+1

so0k commented 8 years ago

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

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

nicdoye commented 8 years ago

+1

ryanwalker commented 8 years ago

+1

patprzybilla commented 8 years ago

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

eytanhanig commented 8 years ago

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.

vsaraswat commented 8 years ago

+1

xbeta commented 8 years ago

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

so0k commented 8 years ago

@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

yotamoron commented 8 years ago

+1

ashic commented 8 years ago

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

etoews commented 8 years ago

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.

tigran10 commented 8 years ago

+1

ghost commented 8 years ago

+1

kk17 commented 8 years ago

+1

bramswenson commented 8 years ago

:+1:

supp429 commented 8 years ago

+1

tombusby commented 8 years ago

+1

countless-integers commented 8 years ago

+1

dotmpe commented 8 years ago

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

gittycat commented 8 years ago

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

etoews commented 8 years ago

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

unixunion commented 8 years ago

+1

nskforward commented 8 years ago

+1

richburdon commented 8 years ago

+1

ahosam commented 8 years ago

+1

chenhuimin commented 8 years ago

+1

vigohe commented 8 years ago

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

rbdoer commented 8 years ago

+1

caledhwa commented 8 years ago

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?

tigran10 commented 8 years ago

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

davidolrik commented 8 years ago

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

gittycat commented 8 years ago

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

sukrit007 commented 8 years ago

👍

exit99 commented 8 years ago

+1

unixunion commented 8 years ago

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

twang2218 commented 8 years ago

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.