Docker Community Forums

Share and learn in the Docker community.

Setting IP address on Local Build


(Cajund) #1

Hello,

As we know, this command builds a local VM with the docker engine:

docker-machine create -d virtualbox

However, the IP address is random (mostly). I would like to set this to something specific. I know it’s not configurable on the command line. Where in the code can I do this? Is there a file with the VBoxManage command somewhere that I can access?

Thanks.


(Dlsysadmin) #2

Yes, for VirtualBox you can do this using –virtualbox-hostonly-cidr. Below is a handy script that I use to do exactly this when creating a new docker-machine VM.

#--- 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"
}

(Cajund) #3

Excellent, thanks for this.

Where do you use this function? It looks like a shell function, which I will probably drop in a simple script. But where do you have it in your environment?

Thanks again. This is really helpful.


(Dlsysadmin) #4

It depends on your operating system. For Mac you would want to add it to the hidden file .bash_profile in your home folder. If using it in a script then you will want to place it at the top of the script itself.

Here’s a command to to automatically add it to .bash_profile. Just copy-paste the following into your terminal, hit enter, and then restart terminal to have the function d-machine-static available for all future sessions:

echo '

#--- 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"
}' >> ~/.bash_profile

(Cajund) #5

Thank you again. I’ve turned it into a shell script that I can run from the command line (for now).

One more question. I want to hard code a specific IP for the VM. So, if I call it with this:

d-machine-static default 192.168.33.11

I want the VM to have that IP. However, I see in the script the CIDR param is:

`–virtualbox-hostonly-cidr “${new_dm_ip}/24”

This results in something like: default = 192.168.33.101

If I change the line to:

`–virtualbox-hostonly-cidr “${new_dm_ip}/32”

It fails with:

Error with pre-create check: "VirtualBox is configured with multiple host-only adapters with the same IP \"192.168.33.11\". Please remove one."

I think maybe I need to alter things so both adapters can be loaded? My experience is with Vagrant and in that case, I just gave the config the IP, so I am not familiar with VirtualBox inner working. I’m sure I am missing something.

Thanks again, I appreciate this help.


(Cajund) #6

To document the issue here:

https://github.com/docker/machine/issues/1709


(Dlsysadmin) #7

Unfortunately I’m don’t know that much about Docker/VirtualBox’s availability of specific CIDR ranges so I’m not sure that I can be of much help on that matter.

I will say this though: Rather than trying to fiddle with the pre-existing “Default” environment you may have more luck setting up separate docker-machine environments and then using an alias or function to switch between them. Here’s the function that I use to do so, which you can also add to your .bash_profile:

#--- 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"
}

The only downside is that Kitematic doesn’t support other docker-machine environments, but that’s a small price to pay for a static IP.


(Dlsysadmin) #8

FYI I’ve had the best luck with IPs of the form 192.168.x.100 such that x is not equal to 100. I’ve edited the above code to reflect a default value falling in that range, 192.168.90.100.


(Cajund) #9

Thanks for this information. Unfortunately, we will require a static IP for our local machines. There are many reasons for this that we can’t avoid. So we may have to look at a vagrant solution.

Thanks again.