有没有办法强制 docker-machine 使用特定的 ip 创建 vm? [英] Is there a way to force docker-machine to create vm with a specific ip?

查看:29
本文介绍了有没有办法强制 docker-machine 使用特定的 ip 创建 vm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制 docker-machine 创建具有特定 ip 的 docker vm(假设该 ip 可用)?

解决方案

这是在 中积极请求的docker/machine 问题 1709

<块引用>

当我使用 docker-machine create 创建虚拟机时,我希望能够指定虚拟机的 IP 地址(即在 docker-machine ls 中URL"下列出的值).

<块引用>

我想要这个是因为我一直依赖于 boot2docker 的默认地址 192.168.59.103,但现在它因机器而异.

当前的解决方法:

<块引用>

我的虚拟机的 dhcp 范围是 192.168.99.100 - 255,我想在 100 之前设置一个 IP.

<块引用>

我发现了一个设置静态 IP 的简单技巧:创建机器后,我运行此命令并重新启动机器:

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 >/开发/空

<块引用>

此命令创建一个文件 bootsync.sh,由 boot2docker 启动脚本搜索并执行.

<块引用>

现在在机器启动期间执行命令并设置静态 IP.

docker-machine ls名称活动驱动程序状态 URL SWARMtest-1 - virtualbox 运行 tcp://192.168.99.50:2376 test-1 (master)

Michele Tedeschi (micheletedeschi) 添加

我已经更新了命令:

echo kill `more/var/run/udhcpc.eth1.pid`
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 >/开发/空

<块引用>

然后运行命令(仅第一次)

docker-machine regenerate-certs prova-discovery

<块引用>

现在IP不会被DHCP改变

(用您的 docker-machine 的名称替换 prova-discovery)


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

@echo off设置本地启用延迟扩展设置机器=%1如果%machine%"=="(echo dmvbf 需要一个机器名退出/b 1)设置 ipx=%2如果%ipx%"=="(回声 dmvbf x 丢失 ^(对于 192.168.x.y^)退出/b 2)设置 ipy=%3如果%ipy%"=="(echo dmvbf y 丢失 ^(对于 192.168.x.y^)退出/b 3)echo kill $(more/var/run/udhcpc.eth1.pid) |docker-machine ssh %machine% sudo tee/var/lib/boot2docker/bootsync.sh >NUL回声 ifconfig eth1 192.168.%ipx%.%ipy% 网络掩码 255.255.255.0 广播 192.168.%ipx%.255 向上 |docker-machine ssh %machine% sudo tee -a/var/lib/boot2docker/bootsync.sh >NULecho route add default gw <gateway ip address here>|docker-machine ssh %machine% sudo tee -a/var/lib/boot2docker/bootsync.sh >NULdocker-machine ssh %machine% "sudo cat/var/run/udhcpc.eth1.pid |xargs sudo kill"docker-machine ssh %machine%sudo ifconfig eth1 192.168.%ipx%.%ipy% 网络掩码 255.255.255.0 广播 192.168.%ipx%.255 up"

(注意:在 Windows 10 上,Monty Wild 评论它是udhcpc.eth0.pid,不是 udhcpc.eth1.pid)

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

 dmvbf <机器名称>99 101

我只做过一次.

在下一次 docker-machine start 时,IP 将为 192.168.99.101.

Is there a way to force docker-machine to create the docker vm with a specific ip (assuming that ip is available)?

解决方案

That is actively requested in 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.

The 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

I've updated the commands with:

echo "kill `more /var/run/udhcpc.eth1.pid`
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

then run command (only the first time)

docker-machine regenerate-certs prova-discovery

now the IP will not be changed by the DHCP

(replace prova-discovery by the name of your docker-machine)


Here is the (Windows) script (dmvbf.bat) I now use, based on what is above:

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

(Note: on Windows 10, Monty Wild comments that it is udhcpc.eth0.pid, not udhcpc.eth1.pid)

I start the vm (docker-machine start <machine-name>), and then:

 dmvbf <machine-name> 99 101

I do that only once.

At the next docker-machine start <machine-name>, the IP will be 192.168.99.101.

这篇关于有没有办法强制 docker-machine 使用特定的 ip 创建 vm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆