Have docker container connect to network devices

2019-02-28 14:26发布

问题:

I am trying to make a few docker containers to hold some of my everyday tooling. But a lot of my tools depend on being able to connect to devices (via wifi) to pull data.

I have been doing research and am rather confused trying to understand what, if anything, would need to be done to support this scenario. I know usually docker containers are the server not the client. But i have read about people doing the opposite.

I am trying to figure out what kind of changes/configuration would be needed to do this.

回答1:

By default, Docker will create a virtual network on your physical server; e.g. the docker0 interface. That happens in bridge mode, it allows this interface to connect to the internet through your eth0 device. eth0 is the physical system interface; everything related to your local as well as global network will pass through the eth0 physical interface.

If you want to access the internet or your local network inside your running docker container, you have to add nameserver as per your physical system /etc/resolv.conf file during startup of the docker daemon. This way, everything you can normally access with your physical system you'll be able to access with the docker container.

One more thing, you have to expose ports when you start up your docker container so that it can pull data from outside the docker network with the help of the eth0 interface. All this configuration is automatically taken care with the help of iptables. Docker will adds iptables rules to forward traffic from docker0 to eth0 and your service will work perfectly.

Example

docker run -it --name "$container_name" -d -h "$host_name" -p 9080:9080 -p 1522:1522 "$image_name"

In the above case, my application is pulling data from $host_name via port 1522. -p 1522:1522 means it will send request to port 1522 on the physical machine, similarly, the physical machine will send this request to the network hosted machine on the same 1522 port."



标签: docker