Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 429
Nyxynyx Avatar asked May 05 '15 17:05

Nyxynyx


People also ask

What can I use the GPIO pins of Raspberry Pi for?

The GPIO pins allow the Raspberry Pi to control and monitor the outside world by being connected to electronic circuits. The Pi is able to control LEDs, turning them on or off, run motors, and many other things. It's also able to detect whether a switch has been pressed, the temperature, and light.

Can GPIO pins be used for I2C?

An actual I2C bus is emulated using two GPIO pins (GPIO 6 and GPIO 7). One pin is for data signals (SDA), and one pin is for clock signals (SCL). The software driver enables client software to operate as either a transmitter or receiver, depending on its function.


1 Answers

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.

like image 169
Tobias Avatar answered Sep 27 '22 21:09

Tobias