Have docker container connect to network devices

后端 未结 1 1469
慢半拍i
慢半拍i 2021-01-24 16:18

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.

相关标签:
1条回答
  • 2021-01-24 16:33

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

    0 讨论(0)
提交回复
热议问题