简体   繁体   English

如何使用docker-machine和VirtualBox创建具有特定URL的docker机器?

[英]How do I create a docker machine with a specific URL using docker-machine and VirtualBox?

I can create a Docker instance with the VirtualBox driver, but I cannot figure out how to specify the URL. 我可以使用VirtualBox驱动程序创建一个Docker实例,但我无法弄清楚如何指定URL。

Create Command: 创建命令:

docker-machine create --driver virtualbox myBox

docker-machine ls: docker-machine ls:

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
myBox     *        virtualbox   Running   tcp://192.168.99.100:2376   

I want the URL to be a specific URL. 我希望URL成为特定的URL。 How can I force the Docker instance to use a specific URL (ex: 192.168.99.113)? 如何强制Docker实例使用特定的URL(例如:192.168.99.113)?

Details: Mac 10.10.5 using Docker Toolbox 详细信息: Mac 10.10.5使用Docker Toolbox

Update 更新

I tried using the --virtualbox-hostonly-cidr command to set the IP range for the docker machine; 我尝试使用--virtualbox-hostonly-cidr命令设置--virtualbox-hostonly-cidr机器的IP范围; I specified a range of one IP address ("192.168.99.113/32"), but the machine created had an IP of 192.168.99.100, which is outside of the range I provided. 我指定了一个IP地址范围(“192.168.99.113/32”),但创建的机器的IP为192.168.99.100,超出了我提供的范围。

Is it even possible to set a docker machine with specific IP address? 是否可以设置具有特定IP地址的泊坞机?

We had the same problem some time ago, trying to change default docker IP in docker machine and we found only 2 ways, how to do it. 我们前段时间遇到了同样的问题,尝试更改docker机器中的默认docker IP,我们发现只有两种方法,如何做到这一点。

  1. You can call a create command of docker machine to create your Docker instance with flag 您可以调用docker machine的create命令来创建带有标志的Docker实例

    --virtualbox-hostonly-cidr "192.168.99.1/24"

    This flag set a range of addresses, which could be set for a Docker instance. 此标志设置一个地址范围,可以为Docker实例设置。 No guarantee, you get the address you want 不能保证,你得到你想要的地址

  2. You can change network settings of virtual machine, leaving NAT and port forwarding for it, making your Docker instance running like it was installed on Host OS. 您可以更改虚拟机的网络设置,为其保留NAT和端口转发,使您的Docker实例像在主机操作系统上安装一样运行。

Of course, the first one is standard approach and seems to be preferable. 当然,第一个是标准方法,似乎更可取。 But in some cases, the second could be usefull too. 但在某些情况下,第二个也可能有用。

Update: There is an open feature request for specifying a static IP for docker machine. 更新:有一个打开功能请求 ,用于为docker机器指定静态IP。 So, unfortunately it's not possible to do it right now. 所以,不幸的是,现在不可能这样做。 Only --virtualbox-hostonly-cidr property, but you have to provide a CIDR Prefix below 29 to make it works (tested for Win version). --virtualbox-hostonly-cidr属性,但您必须提供低于29的CIDR前缀才能使其正常工作(针对Win版本进行测试)。 Or take a look at this comment , where is shown, how you can do it for OS X by configuring Virtual Box, like: 或者看看这个评论 ,在哪里显示,如何通过配置Virtual Box来为OS X做到这一点,如:

$ 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

Pending the resolution of issue 1709 , I use the following script (a Windows one, to be adapted for mac). 问题1709的解决之前,我使用以下脚本(Windows 1,适用于mac)。
(Source: imranraja85 and micheletedeschi 's comment ) (来源: imranraja85micheletedeschi评论

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

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"

I start the vm ( docker-machine start <machine-name> ), and then: 我启动vm( docker-machine start <machine-name> ),然后:

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 start <machine-name> ,IP将为192.168.99.101。

Based on the discussion at the docker/machine feature request thread we wrote the following script : 根据docker / machine feature请求线程的讨论,我们编写了以下脚本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM