Docker访问Raspberry Pi GPIO引脚

时间:2022-03-31 07:25:02

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

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

4 个解决方案

#1


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

在Linux主机上,有三种方法可以从Docker容器中访问GPIO引脚。

1. Running Docker with the "--privileged" option

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

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

$ docker run --privileged -d whatever

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

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

2. Adding the /dev/gpiomem device

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.

您可以是特定的,并且只在运行时将/ dev / gpiomem设备公开给容器,而不是将所有主机的设备暴露给容器。请注意,此设备需要在主机的Linux发行版中支持内核驱动程序。最近发布的Raspbian应该有这个。您与其他发行版的里程可能会有所不同。

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

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:

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

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

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

请注意,使用sysfs进行GPIO可能比设备方法慢。

GPIO libraries

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.

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

#2


You would probably use docker volumes to expose the sysfs interface. For example, something like:

您可能会使用docker卷来公开sysfs接口。例如,类似于:

docker run -v /sys:/sys fedora bash

This would expose /sys on the host as /sys inside the container, and you would have access to the /sys/class/gpio hierarchy.

这会将主机上的/ sys暴露为容器内的/ sys,并且您可以访问/ sys / class / gpio层次结构。

If you were using code that access the GPIO pins without using the sysfs interface you would need to expose whatever device node it is using inside the container, possibly with something like the --device argument to docker run.

如果您使用的代码在不使用sysfs接口的情况下访问GPIO引脚,则需要在容器内公开它所使用的任何设备节点,可能使用类似docker run的--device参数。

#3


I would use this image: https://github.com/acencini/rpi-python-serial-wiringpi, as a base image. Here you can easily access with python. Or you can decide to download node onto the image and use these two npm libraries to access through javascript

我会使用这个图像:https://github.com/acencini/rpi-python-serial-wiringpi作为基本图像。在这里,您可以使用python轻松访问。或者您可以决定将节点下载到图像上,并使用这两个npm库通过javascript访问

https://github.com/bryan-m-hughes/raspi -- https://github.com/bryan-m-hughes/raspi-gpio

https://github.com/bryan-m-hughes/raspi - https://github.com/bryan-m-hughes/raspi-gpio

The base for the whole thing is wiringPi as you can see in the Dockerfile, and that you have to run this command when you run up the image the first time:

正如您在Dockerfile中看到的那样,整个事物的基础是wiringPi,并且当您第一次运行图像时必须运行此命令:

docker run --device /dev/ttyAMA0:/dev/ttyAMA0 --device /dev/mem:/dev/mem --privileged -ti acencini/rpi-python-serial-wiringpi /bin/bash

Whats important here is that you open up dev ports and mem for wiringPi to access it. Privileged access to /dev/mem is required by wiringPi.

这里重要的是你打开开发端口和mem for wiringPi来访问它。 wiringPi需要特权访问/ dev / mem。

#4


In application with onoff on raspberry pi 3B+, mounting /sys/devices/platform/soc/3f200000.gpio is enough.

在使用覆盆子pi 3B + onoff的应用程序中,安装/sys/devices/platform/soc/3f200000.gpio就足够了。

docker run -v /sys/devices/platform/soc/3f200000.gpio:/sys/devices/platform/soc/3f200000.gpio ...

I am still looking for better solutions.

我仍在寻找更好的解决方案。

#1


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

在Linux主机上,有三种方法可以从Docker容器中访问GPIO引脚。

1. Running Docker with the "--privileged" option

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

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

$ docker run --privileged -d whatever

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

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

2. Adding the /dev/gpiomem device

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.

您可以是特定的,并且只在运行时将/ dev / gpiomem设备公开给容器,而不是将所有主机的设备暴露给容器。请注意,此设备需要在主机的Linux发行版中支持内核驱动程序。最近发布的Raspbian应该有这个。您与其他发行版的里程可能会有所不同。

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

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:

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

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

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

请注意,使用sysfs进行GPIO可能比设备方法慢。

GPIO libraries

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.

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

#2


You would probably use docker volumes to expose the sysfs interface. For example, something like:

您可能会使用docker卷来公开sysfs接口。例如,类似于:

docker run -v /sys:/sys fedora bash

This would expose /sys on the host as /sys inside the container, and you would have access to the /sys/class/gpio hierarchy.

这会将主机上的/ sys暴露为容器内的/ sys,并且您可以访问/ sys / class / gpio层次结构。

If you were using code that access the GPIO pins without using the sysfs interface you would need to expose whatever device node it is using inside the container, possibly with something like the --device argument to docker run.

如果您使用的代码在不使用sysfs接口的情况下访问GPIO引脚,则需要在容器内公开它所使用的任何设备节点,可能使用类似docker run的--device参数。

#3


I would use this image: https://github.com/acencini/rpi-python-serial-wiringpi, as a base image. Here you can easily access with python. Or you can decide to download node onto the image and use these two npm libraries to access through javascript

我会使用这个图像:https://github.com/acencini/rpi-python-serial-wiringpi作为基本图像。在这里,您可以使用python轻松访问。或者您可以决定将节点下载到图像上,并使用这两个npm库通过javascript访问

https://github.com/bryan-m-hughes/raspi -- https://github.com/bryan-m-hughes/raspi-gpio

https://github.com/bryan-m-hughes/raspi - https://github.com/bryan-m-hughes/raspi-gpio

The base for the whole thing is wiringPi as you can see in the Dockerfile, and that you have to run this command when you run up the image the first time:

正如您在Dockerfile中看到的那样,整个事物的基础是wiringPi,并且当您第一次运行图像时必须运行此命令:

docker run --device /dev/ttyAMA0:/dev/ttyAMA0 --device /dev/mem:/dev/mem --privileged -ti acencini/rpi-python-serial-wiringpi /bin/bash

Whats important here is that you open up dev ports and mem for wiringPi to access it. Privileged access to /dev/mem is required by wiringPi.

这里重要的是你打开开发端口和mem for wiringPi来访问它。 wiringPi需要特权访问/ dev / mem。

#4


In application with onoff on raspberry pi 3B+, mounting /sys/devices/platform/soc/3f200000.gpio is enough.

在使用覆盆子pi 3B + onoff的应用程序中,安装/sys/devices/platform/soc/3f200000.gpio就足够了。

docker run -v /sys/devices/platform/soc/3f200000.gpio:/sys/devices/platform/soc/3f200000.gpio ...

I am still looking for better solutions.

我仍在寻找更好的解决方案。