Gadgetoid / PY_LGPIO

Python packaging for the PY_LGPIO lg bindings.
https://github.com/joan2937/lg
The Unlicense
1 stars 1 forks source link

Docker PinFactoryFallback Raspberry Pi 5 #3

Closed Ed1ks closed 3 months ago

Ed1ks commented 3 months ago

While being happy that the package got installed and creating this post, I got another issue, that I get error when using gpiozero in python code:

/usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from lgpio: No module named 'lgpio'
  warnings.warn(
/usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from rpigpio: No module named 'RPi'
  warnings.warn(
/usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from pigpio: No module named 'pigpio'
  warnings.warn(
/usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from native: unable to open /dev/gpiomem or /dev/mem; upgrade your kernel or run as root

this is my dockerfile:

FROM python:3.11-slim-bullseye
ENV PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PUID=1000\
    PGID=1000

# Set the working directory inside the container
WORKDIR /app

# Create and set volumes

# Install system dependencies for PyAudio, evdev, and build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    supervisor \
    swig \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements.txt to the container and install Python dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

RUN pip install pigpio gpio gpiozero
RUN pip install https://github.com/Gadgetoid/PY_LGPIO/releases/download/0.2.2.0/lgpio-0.2.2.0.tar.gz

# Copy the rest of the application code to the container
COPY . .

# Ensure the entrypoint script is executable
RUN chmod +x ./entrypoint.sh

# Expose the port the app runs on
EXPOSE 8010

ENTRYPOINT ["./entrypoint.sh"]
CMD ["supervisord", "-c", "/supervisord_docker.conf"]

the code runs fine on Raspberry Pi 5 root, but if running docker container with priviliged mode, it doesnt seem to work. Any Ideas, whats wrong?

_Originally posted by @Ed1ks in https://github.com/Gadgetoid/PY_LGPIO/issues/2#issuecomment-2129600938_

Ed1ks commented 3 months ago

Ok after hours of testing different dependencys because I thought there is something needed like libgpio, rpigpio, gpiod or something, I finally did find out, it was because I run code without root permission 🙈. Everyone who want to access gpio with gpiozero inside a docker container on a raspberry pi 5: Dockerfile example:

FROM python:3.11-slim-bullseye
ENV PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONDONTWRITEBYTECODE=1 

# Set the working directory inside the container
WORKDIR /app

# install
RUN apt-get update && apt-get install -y --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements.txt to the container and install Python dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code to the container
COPY . .

# ... rest of dockerfile ...

while in requirements.txt this must be included:

gpiozero>=2.0.1
https://github.com/Gadgetoid/PY_LGPIO/releases/download/0.2.2.0/lgpio-0.2.2.0.tar.gz

no gpiod, rpi.gpiod, libgpiod or something else needed. Juste these two. you must run your python code as root. of course you could install these two packets also in Dockerfile with RUN. But this is probably the better way to go.

I hope this will help somebody.