Docker 访问 Raspberry Pi GPIO 引脚 [英] Docker Access to Raspberry Pi GPIO Pins

查看:35
本文介绍了Docker 访问 Raspberry Pi GPIO 引脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Raspberry Pi 2 上运行 Docker 时,我们如何将 GPIO 引脚暴露给 Docker 容器?

When running Docker on the Raspberry Pi 2, how can we expose the GPIO pins to the Docker container?

推荐答案

在 Linux 主机上,有三种可能的方法可以从 Docker 容器内访问 GPIO 引脚.

On a Linux host, there are three possible ways to get access to the GPIO pins from within a Docker container.

像这样启动容器将使容器可以完全访问主机的设备,包括 GPIO:

Starting a container like this will give the container full access to the host's devices, including GPIO:

$ docker run --privileged -d whatever

检查 有关此选项的 Docker 文档.这可能不是最佳选择,具体取决于您的安全要求有多严格.

Check the Docker documentation on this option. It might not be the best choice depending on how tight your security requirements are.

与其将主机的所有设备都暴露给容器,您可以具体一些,只在运行时将/dev/gpiomem 设备暴露给容器.请注意,此设备需要主机的 Linux 发行版中的内核驱动程序支持.Raspbian 的最新版本应该有这个.您使用其他发行版的里程可能会有所不同.

Rather than exposing all of the host's devices to the container, you can be specific and only expose the /dev/gpiomem device to the container at runtime. Be aware that this device needs kernel driver support within the host's Linux distribution. Recent releases of Raspbian should have this. Your mileage with other distributions may vary.

$ docker run --device /dev/gpiomem -d whatever

3.在主机上使用 sysfs 文件系统

Pi 的 GPIO 表示在主机的文件系统中,位于/sys/class/gpio 下.这可以通过该文件系统中的虚拟文件以用户权限访问.使用 Docker 卷将其公开给您的容器:

3. Using the sysfs filesystem on the host

The Pi's GPIO is represented within the host's file system underneath /sys/class/gpio. This can be accessed with user privileges via the virtual files in that file system. Use Docker volumes to expose this to your container:

$ docker run -v /sys:/sys -d whatever

请注意,将 sysfs 用于 GPIO 可能会比设备方法慢.

Mind that using sysfs for GPIO is probably going to be slower than the device approach.

这三种方法中哪一种适合您的需求还取决于您在访问 GPIO 时使用的库.并非所有库都支持所有这三个选项.

Which of these three approaches fits your needs will also depend on the libraries you are using when accessing GPIO. Not all libraries support all three of these options.

这篇关于Docker 访问 Raspberry Pi GPIO 引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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