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

后端 未结 2 460
闹比i
闹比i 2021-01-01 16:33

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 i

2条回答
  •  感情败类
    2021-01-01 16:40

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

提交回复
热议问题