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

查看:333
本文介绍了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天全站免登陆