Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

Docker Access to Raspberry Pi GPIO Pins

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

question from:https://stackoverflow.com/questions/30059784/docker-access-to-raspberry-pi-gpio-pins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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:

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

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.

$ 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:

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...