Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fruttodelmondo/72ffae3998a2092d299c886148813079 to your computer and use it in GitHub Desktop.
Save fruttodelmondo/72ffae3998a2092d299c886148813079 to your computer and use it in GitHub Desktop.
Demonstrating how to use Raspberry Pi 5 GPIO pins in a Docker container

This gist demonstrates how to access the Raspberry Pi 5 GPIO pins inside a docker container without the need to run Docker with --privileged

Copy Dockerfile and gpiod-test.py onto your host, then run

docker build -t gpiotest . && docker run -it --device /dev/gpiochip4:/dev/gpiochip4 gpiotest
FROM debian:latest
RUN apt update && apt install -y python3 python3-libgpiod
COPY "./gpiod-test.py" "/gpiod-test.py"
CMD ["python3", "gpiod-test.py"]
import gpiod
from time import sleep
LED_PIN = 17
chip = gpiod.Chip('gpiochip4')
led_line = chip.get_line(LED_PIN)
led_line.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
try:
while True:
led_line.set_value(1)
sleep(0.5)
led_line.set_value(0)
finally:
led_line.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment