RPI Camera not working in Docker on 64bit RPI 4b

Camera model

imx219

What is the problem?

I can't get the webcam to work when running Octoprint (OctoPrint 1.10.2) under docker on an RPI 4b. I have previously had it working when using the octopi image on a RPI 2b.

`MJPG Streamer Version.: 2.0
octoprint-1  |  i: Using V4L2 device.: /dev/video0
octoprint-1  |  i: Desired Resolution: 640 x 480
octoprint-1  |  i: Frames Per Second.: -1
octoprint-1  |  i: Format............: JPEG
octoprint-1  |  i: TV-Norm...........: DEFAULT
octoprint-1  |  i: Could not obtain the requested pixelformat: MJPG , driver gave us: YUYV
octoprint-1  |     ... will try to handle this by checking against supported formats. 
octoprint-1  |     ... Falling back to YUV mode (consider using -yuv option). Note that this requires much more CPU power
octoprint-1  |  o: www-folder-path......: /usr/local/share/mjpg-streamer/www/
octoprint-1  |  o: HTTP TCP port........: 8080
octoprint-1  |  o: HTTP Listen Address..: (null)
octoprint-1  |  o: username:password....: disabled
octoprint-1  |  o: commands.............: enabled
octoprint-1  | libv4l2: error turning on stream: Invalid argument
octoprint-1  | Unable to start capture: Invalid argument
octoprint-1  |  i: Can't enable video in first time
octoprint-1  |  i: cleaning up resources allocated by input thread
`

The camera is functioning fine when I use (on the host, not in docker);

rpicam-still --list-cameras

and it shows;

Available cameras
-----------------
0 : imx219 [3280x2464 10-bit RGGB] (/base/soc/i2c0mux/i2c@1/imx219@10)
    Modes: 'SRGGB10_CSI2P' : 640x480 [206.65 fps - (1000, 752)/1280x960 crop]
                             1640x1232 [41.85 fps - (0, 0)/3280x2464 crop]
                             1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                             3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]
           'SRGGB8' : 640x480 [206.65 fps - (1000, 752)/1280x960 crop]
                      1640x1232 [83.70 fps - (0, 0)/3280x2464 crop]
                      1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                      3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]


however, when I run (on the host, not in docker)

vcgencmd get_camera

I get this:

supported=0 detected=0, libcamera interfaces=0

my docker compose file looks like this;

services:
  octoprint:
    image: octoprint/octoprint
    restart: unless-stopped
    privileged: true
    ports:
      - 80:80
    devices:
    # use `python -m serial.tools.miniterm` to see what the name is of the printer, this requires pyserial
    #  - /dev/ttyACM0:/dev/ttyACM0
      - /dev/video0:/dev/video0
    volumes:
     - octoprint:/octoprint
    # uncomment the lines below to ensure camera streaming is enabled when
    # you add a video device
    environment:
      - ENABLE_MJPG_STREAMER=true

volumes:
  octoprint:

What did you already try to solve it?

Lots of combinations of CAMERA_DEV = /dev/video or /dev/vchiq
configuring in config.txt;
camera_auto_detect=0
dtoverlay=imx219

Have you tried running in safe mode?

No

Did running in safe mode solve the problem?

N/A

1 Like

Hello, I am in the same situation as you.

And what can you provide to find a solution?

unfortunately no, I'm still looking

It's taken me a while to investigate but essentially I've come to the conclusion it's simply not possible. Instead, what you can do is;

  1. use the octoprint docker minimal container for octopriint
  2. use raspisrv in a container for the webcam streaming
  3. if you really wanted security put nginx in front of it.

To get raspisrv working in a container, my docker file is;

FROM debian:bookworm-slim
RUN apt-get update

# Base set of utilities
RUN apt-get --assume-yes  install wget unzip make gnupg2

# Add RPI specific Repos
RUN echo "deb http://archive.raspberrypi.com/debian/ bookworm main" | tee /etc/apt/sources.list.d/raspi.list
RUN wget -O - -q http://archive.raspberrypi.org/debian/raspberrypi.gpg.key  | apt-key add -
RUN apt-get update

# Now raspisrv
RUN apt install -y python3

RUN wget https://github.com/signag/raspi-cam-srv/archive/refs/heads/main.zip
RUN unzip main.zip
RUN apt install -y python3-picamera2 --no-install-recommends

RUN apt install pip -y
RUN apt install python3.11-venv -y
RUN apt install -y udev 
RUN mv raspi-cam-srv-main raspi-cam-srv
RUN cd raspi-cam-srv
RUN python3 -m venv --system-site-packages /raspi-cam-srv/.venv

RUN /bin/bash -c "cd raspi-cam-srv && source .venv/bin/activate && pip install Flask==3.0.0"

RUN echo "cd raspi-cam-srv" | tee /start.sh
RUN echo "if [ ! -f /raspi-cam-srv/instance/raspiCamSrv.sqlite ]; then" | tee -a /start.sh
RUN echo "    source .venv/bin/activate && flask --app raspiCamSrv init-db" | tee -a /start.sh
RUN echo "fi" | tee -a /start.sh
RUN echo "source .venv/bin/activate && flask --app raspiCamSrv run --port 5000 --host=0.0.0.0" | tee -a /start.sh
RUN chmod a+x /start.sh
CMD ["/bin/bash", "-c", "/start.sh"]

and then my docker compose file is

services:
  raspicam:
    image: "raspicam"
    restart: unless-stopped
    user: root
    privileged: true
    ports: 
      - 5000:5000
    devices:
          - /dev/video0:/dev/video0
          - /dev/vchiq
    volumes:
          - /run/udev:/run/udev
          - /run/dbus/:/run/dbus
          - /var/local/raspisrv:/raspi-cam-srv/instance # where the database file is

  octoprint:
    image: octoprint/octoprint:minimal
    restart: unless-stopped
    privileged: true
    ports:
      - 80:5000
    devices:
    # use `python -m serial.tools.miniterm` to see what the name is of the printer, this requires pyserial
      - /dev/ttyACM0:/dev/ttyACM0

    volumes:
     - octoprint:/octoprint
    # uncomment the lines below to ensure camera streaming is enabled when
    # you add a video device
    environment:
      - ENABLE_MJPG_STREAMER=false

volumes:
  octoprint:

Octoprint still complains about vcgencmd but it's working ok :slight_smile:

Hi I find myself with this error

Failed to deploy a stack: time="2024-09-04T17:05:04Z" level=warning msg="/data/compose/4/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion" octoprint Pulling raspicam Pulling raspicam Error pull access denied for raspicam, repository does not exist or may require 'docker login': denied: requested access to the resource is denied octoprint Error context canceled Error response from daemon: pull access denied for raspicam, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

Suggest you read up on building docker images, but it's probably because you need to do;

$ docker build . -t raspicam

I don't understand

To make the raspicam docker image you need to take all the text the block:

and put it in a file named "Dockerfile"

then run the command "$docker build . -t raspicam" - this generates the raspicam container image, which you should be able to see when you run $docker image list