Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

macos

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?

like image 360
Leo Avatar asked Aug 06 '14 21:08

Leo


People also ask

What does Docker network host do?

Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine. To access the application inside the container, use the port at the host's IP address (e.g., port 80).

Does Docker container have same IP as host?

You should now see that port 80 of your host is forwarding traffic to port 80 of the container. What about the container's IP address? You may think that containers use the same IP address as the host machine but that is not the case. We can test this using a Docker inspect command.

How does container networking work?

Container networks run on software platforms, such as Docker, that can be run on the cloud. Container's efficiency comes from the fact they are self-contained. They use the bare minimum of software and resources to run and use a router to direct traffic instead of a switch.

How does Docker bridge network work?

In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.


1 Answers

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...

like image 183
David Bosschaert Avatar answered Nov 15 '22 23:11

David Bosschaert