How does docker's `net=host` setting work, and how can I do something similar with e.g. VirtualBox?

16,890

Solution 1

Thanks to some help from my colleagues I found a solution to this problem. This solution works with boot2docker/VirtualBox. I just created my docker VM with boot2docker init, I didn't make any specific changes to the VM configuration.

First you run the docker image with --net=host, so that it shares the network with the host VM e.g.

docker run -it --net=host ubuntu bash

Then you need to find the IP address from the VM used for the docker containers, you can do this by running boot2docker ssh the OSX host. You can then find the IP address of the VM by finding its gateway:

$ netstat -rn | grep UG | awk '{print $2}'
10.0.2.2

So in my case it's 10.0.2.2. You can now access ports opened on the host, i.e. on a program running on OSX from your docker container by using this IP address.

To automate you could find the IP address first and then pass it into the docker command as an environment variable...

Solution 2

I have found another answer that works, I'll share that here so that people can choose :)

First you need to figure out what the IP address of the preferred network interface is on your OSX host is. The following shell command did this for me:

echo "show State:/Network/Global/IPv4" | scutil | grep PrimaryInterface | awk '{print $3}' | xargs ifconfig | grep inet | grep -v inet6 | awk '{print $2}'

In my case this prints out: 10.226.98.247

Then you can simply use that address inside docker, or even better give this address a hostname for inside docker:

docker run -it --add-host dockerhost:10.226.98.247 ubuntu bash

Then you can use the same dockerhost hostname in your docker container regardless of what environment you're launching your container in...

Share:
16,890

Related videos on Youtube

Leo
Author by

Leo

Updated on July 16, 2022

Comments

  • Leo
    Leo almost 2 years

    Docker has a run option net=host documented here that allows you to run a virtual machine that shares the network stack with the host — for example, processes inside the docker container can connect to the host machine via localhost and vice versa.

    I want to set up a Linux VM on Mac OS X that does the same thing; I've tried using Vagrant and its various networking settings without much luck.

    Does Docker's VM rely on the host and guest OSes both being Linux, or is there some way to accomplish this OSX->Linux that I'm missing?

    • Salem
      Salem over 9 years
      Have you tried their tutorial? docs.docker.com/installation/mac Docker uses containers as the underlying technology so you will need linux as host (a vm) and guest (containers).
    • Leo
      Leo over 9 years
      Right--my problem is that net=host gets the guest container access to the host vm's network stack, but I want it to have access to the grandparent's (osx) network stack.
  • Nathan Basanese
    Nathan Basanese about 8 years
    // , Does this work with DockerMachine in the same way?