有没有办法强制 docker-machine 创建具有特定 ip 的虚拟机?

Is there a way to force docker-machine to create vm with a specific ip?

有没有办法强制 docker-机器创建具有特定 ip 的 docker 虚拟机(假设 ip 可用)?

docker/machine issue 1709

中主动请求

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.

current workaround:

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)

Michele Tedeschi (micheletedeschi) adds

我已将命令更新为:

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

(将 prova-discovery 替换为您的 docker-机器的名称)


这是我现在使用的(Windows)脚本(dmvbf.bat),基于上面的内容:

@echo off
setlocal enabledelayedexpansion
set machine=%1
if "%machine%" == "" (
    echo dmvbf expects a machine name
    exit /b 1
)
set ipx=%2
if "%ipx%" == "" (
    echo dmvbf x missing ^(for 192.168.x.y^)
    exit /b 2
)
set ipy=%3
if "%ipy%" == "" (
    echo dmvbf y missing ^(for 192.168.x.y^)
    exit /b 3
)

echo kill $(more /var/run/udhcpc.eth1.pid) | docker-machine ssh %machine% sudo tee /var/lib/boot2docker/bootsync.sh >NUL
echo ifconfig eth1 192.168.%ipx%.%ipy% netmask 255.255.255.0 broadcast 192.168.%ipx%.255 up | docker-machine ssh %machine% sudo tee -a /var/lib/boot2docker/bootsync.sh >NUL
echo route add default gw <gateway ip address here> | docker-machine ssh %machine% sudo tee -a /var/lib/boot2docker/bootsync.sh >NUL

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

docker-machine ssh %machine% "sudo ifconfig eth1 192.168.%ipx%.%ipy% netmask 255.255.255.0 broadcast 192.168.%ipx%.255 up"

(注:在Windows10上,Monty Wild udhcpc.eth0.pid,不是udhcpc.eth1.pid)

我启动虚拟机 (docker-machine start <machine-name>),然后:

 dmvbf <machine-name> 99 101

我只做过一次。

下一个docker-machine start <machine-name>,IP为192.168.99.101。

我的解决方法是查看 docker 机器连接到的 VBoxNet,然后我进入 VirtualBox 重置 DHCP 范围以仅提供我希望机器拥有的 IP。

我更改了 VirtualBox 的 DHCP 范围。将上限和下限都设置为 192.168.99.100 并重新启动 docker 机器。要更改范围,请转到 VirtualBox->Files->host network manager->choose the appropriate adapter->change the upperbound.

下面的脚本使用 default 作为机器名称,10.100.1.100 作为静态 IP 地址。

cat << EOF | docker-machine ssh default sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null
ifconfig eth1 10.100.1.100 netmask 255.255.255.0 broadcast 10.100.1.255 up
ip route add default via 10.100.1.1
EOF

重启机器:

docker-machine restart default

重新生成证书:

docker-machine regenerate-certs default -f

最后,显示环境变量:

docker-machine env default

是的,有一个选项可以使用特定的 ip 创建。您需要做的就是使用 --virtualbox-hostonly-cidr 选项指定 ip/ip-range。

例如:

  • 当你要分配具体的ip时,比如你要分配10.15.1.24,那么相应的配置就会是(最重要的是我们在这里使用的掩码。对于具体的ip您需要将子网掩码设置为 32) :

    docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "10.15.1.24/32" dev

  • 如果你想从特定的ip范围内选择一个ip,那么你需要使用合适的子网掩码。