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

六眼飞鱼酱① 提交于 2019-12-03 06:15:50

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.

  1. You can call a create command of docker machine to create your Docker instance with flag

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

    This flag set a range of addresses, which could be set for a Docker instance. 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.

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. 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). Or take a look at this comment, where is shown, how you can do it for OS X by configuring Virtual Box, like:

$ 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).
(Source: imranraja85 and micheletedeschi's comment)

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:

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.

Based on the discussion at the docker/machine feature request thread we wrote the following script:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!