VideoCapture Doesn´t work in docker container

Hello everybody

In this moment i am developing microservice with java and openCV, i am using VideoCapture for image capture directly from device camera, but, when i tried to use The microservice since docker, i obtained this error

[ WARN:1@35.182] global /home/runner/work/opencv/opencv/opencv-4.6.0/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

The microservice started in docker container, but the VideoCapture not, this is my dockerfile

FROM openjdk:17
WORKDIR /Faceregister
COPY "./target/FaceRegister-0.0.1-SNAPSHOT.jar" "FaceRegister.jar"
COPY "./opencv_java460.dll" .
COPY "./haarcascade_frontalface_alt.xml" .
COPY "./opencv" .
ENTRYPOINT ["java", "-jar", "FaceRegister.jar"]

if somebody can help me please.

How did you start the container and how did you manage to make your screen available in Docker Desktop’s virtual machine? I don’t follow what WSL2 is capable of recently, but note that every container runs in the virtual machine. With the WSL2 backend, in a WSL2 distribution so the availability of the actual host is limited.

Also hard to say anything without knowing how you start the container, so please, share that too so people can tell you how you can make it work if it is possible.

Thanks for your answer, take a look

This is my dockerfile

FROM openjdk:17
WORKDIR /Faceregister
COPY "./target/FaceRegister-0.0.1-SNAPSHOT.jar" "FaceRegister.jar"
COPY "./opencv_java460.dll" .
COPY "./haarcascade_frontalface_alt.xml" .
## COPY "./opencv" .
ENTRYPOINT ["java", "-jar", "FaceRegister.jar"]

i did build the container using

docker-compose up --build 

directly path of the microservice and, the microservice started in port 8081 but when i tried use that y had this error

face-register  |
face-register  |   .   ____          _            __ _ _
face-register  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
face-register  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
face-register  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
face-register  |   '  |____| .__|_| |_|_| |_\__, | / / / /
face-register  |  =========|_|==============|___/=/_/_/_/
face-register  |  :: Spring Boot ::                (v3.2.3)
face-register  |
face-register  | 2024-04-12T15:14:13.364Z  INFO 1 --- [           main] c.i.s.f.f.FaceRegisterApplication        : Starting FaceRegisterApplication v0.0.1-SNAPSHOT using Java 17.0.2 with PID 1 (/Faceregister/FaceRegister.jar started by root in /Faceregister)
face-register  | 2024-04-12T15:14:13.384Z  INFO 1 --- [           main] c.i.s.f.f.FaceRegisterApplication        : The following 1 profile is active: "production"
face-register  | 2024-04-12T15:14:16.452Z  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8081 (http)
face-register  | 2024-04-12T15:14:16.481Z  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
face-register  | 2024-04-12T15:14:16.486Z  INFO 1 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]
face-register  | 2024-04-12T15:14:16.631Z  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
face-register  | 2024-04-12T15:14:16.635Z  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2884 ms
face-register  | 2024-04-12T15:14:20.544Z  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8081 (http) with context path ''
face-register  | 2024-04-12T15:14:20.627Z  INFO 1 --- [           main] c.i.s.f.f.FaceRegisterApplication        : Started FaceRegisterApplication in 8.822 seconds (process running for 11.21)
face-register  | 2024-04-12T15:14:39.627Z  INFO 1 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
face-register  | 2024-04-12T15:14:39.627Z  INFO 1 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
face-register  | 2024-04-12T15:14:39.629Z  INFO 1 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
face-register  | [ WARN:0@19.859] global /home/runner/work/opencv/opencv/opencv-4.6.0/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Gracefully stopping... (press Ctrl+C again to force)

well, exactly this error

face-register  | [ WARN:0@19.859] global /home/runner/work/opencv/opencv/opencv-4.6.0/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

I think we can agree that a general compose up command doesn’t really share much about how you actually start the container, so please, share a compose file too.

this is the docker-compose file

version: '3'

services:
  your-application:
    build:
      context: .
      dockerfile: Dockerfile
    image: face-register
    container_name: face-register
    ports:
      - "8081:8081"
    environment:
      - SPRING_PROFILES_ACTIVE=production
    restart: always

and using the dockerfile and the command docker-compose up – build i start the container.

Thank you for the compose file. So I was curious what I would see in the compose file that suggest Docker Desktop supports what you want. But even without Docker Desktop, you can’t access all devices on the host. /dev/ is a special folder for devices, not regular files. You can’t mount one from the host into a virtual machine. On Windows, WSL2 has some additional features, but even if a camera device is available somehow in the Docker Desktop distribution, it would not be available in the container.

I never trie dit, but you would probably need to do something like this:

devices:
  - /dev/video0:/dev/video0

Since it has to be available in the WSL2 distribution, it becomes a WSL2 issue as well. You can find issues like this:

with a similar error message