简体   繁体   中英

Is it possible to use RPi.GPIO library in docker?

I used the official docker image of flask. And installed the rpi.gpio library in the container

pip install rpi.gpio

It's succeeded:

root@e31ba5814e51:/app# pip install rpi.gpio
Collecting rpi.gpio
  Downloading RPi.GPIO-0.7.0.tar.gz (30 kB)
Building wheels for collected packages: rpi.gpio
  Building wheel for rpi.gpio (setup.py) ... done
  Created wheel for rpi.gpio: filename=RPi.GPIO-0.7.0-cp39-cp39-linux_armv7l.whl size=68495 sha256=0c2c43867c304f2ca21da6cc923b13e4ba22a60a77f7309be72d449c51c669db
  Stored in directory: /root/.cache/pip/wheels/09/be/52/39b324bfaf72ab9a47e81519994b2be5ddae1e99ddacd7a18e
Successfully built rpi.gpio
Installing collected packages: rpi.gpio
Successfully installed rpi.gpio-0.7.0

But it prompted the following error:

Traceback (most recent call last):
  File "/app/hello/app2.py", line 2, in <module>
    import RPi.GPIO as GPIO
  File "/usr/local/lib/python3.9/site-packages/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!

I tried the method in this link, but it didn't work: Docker Access to Raspberry Pi GPIO Pins

I want to know if this can be done, and if so, how to proceed.

First make sure you're running Docker container as "privileged" like so:

docker run --privileged -it debian:latest

Also, double check that you're running an image that is meant to run on your processor.

For example, if you try to run "debian:latest" on your Raspberry Pi 4 it will actually pull "arm32v7/debian:latest".

Yes it is. To extend the answer of Ari M::

It's more safe to run

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

as it avoids full privileged host access.

It's also necessary to build your image on RaspberyPi.

This answers showed me the way:

How to enable wiringpi GPIO control inside a Docker container

Docker Access to Raspberry Pi GPIO Pins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM