让Docker容器连接到网络设备 [英] Have docker container connect to network devices

查看:125
本文介绍了让Docker容器连接到网络设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一些docker容器来存放我的一些日常工具。但是我的许多工具都依赖于能够连接到设备(通过wifi)以提取数据。

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.

我一直在做研究,但很困惑,试图了解需要采取什么措施来支持这种情况。我知道通常docker容器是服务器而不是客户端。但是我读到有关人们做相反的事情。

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.

推荐答案

默认情况下,Docker将在您的物理服务器上创建一个虚拟网络。例如 docker0 接口。这是在 bridge 模式下发生的,它允许该接口通过您的 eth0 设备连接到Internet。 eth0 是物理系统接口;与您的本地以及全球网络有关的所有内容都将通过 eth0 物理接口传递。

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.

如果要在运行的Docker容器中访问Internet或本地网络,则必须添加 nameserver 根据docker守护进程启动期间的物理系统 /etc/resolv.conf 文件。这样,您通常可以通过物理系统访问的所有内容都将可以通过docker容器访问。

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.

另一件事,您必须在暴露端口时启动您的Docker容器,以便它可以在 eth0 界面的帮助下从Docker网络外部提取数据。在 iptables 的帮助下,所有这些配置都会自动进行。 Docker将添加 iptables规则将流量从 docker0 转发到 eth0 并且您的服务将正常运行。

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.

示例

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

在上述情况下,我的应用程序通过端口 $ host_name 提取数据> 1522 。 -p 1522:1522 表示它将发送请求到物理机上的端口 1522 ,类似地,物理机将将此请求发送到同一 1522 端口上的网络托管计算机。

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容器连接到网络设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆