1

There are several tutorials around on how to do this, but I had to change some bits to make it work on my macOS Catalina 10.15.7, so I want to share with everybody what was exactly necessary to finally have camera working in docker container with Docker Desktop for Mac.

Note: docker-machine operations can take a while, please be patient :)

Note 2: Only tested with the built-in camera

  1. Install legacy docker virtualization engine for Docker Desktop on Mac (which uses Oracle Virtual Box)

    • Install Virtual Box
    • Install Virtual Box Extension pack
    • Install Docker Toolbox (reading this is strongly recommended & backuping your /usr/local/bin/docker* before is also recommended)
      • Ensure that /usr/local/bin/docker and /usr/local/bin/docker-compose link to Docker Desktop binaries (/Applications/Docker.app/Contents/Resources), and not Docker Toolbox
      • Test everything is still working: docker ps -a and docker images should display what you already had in Docker Desktop, docker-machine ls should not raise an error
  2. brew install socat

  3. brew install xquartz

  4. Setting: XQuartz Preferences > Security > check allow all (Allow connections from network clients)

  5. defaults write org.macosforge.xquartz.X11 enable_iglx -bool true

  6. IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')

  7. xhost + $IP

  8. docker-machine create -d virtualbox --virtualbox-cpu-count=4 --virtualbox-memory=4096 --virtualbox-disk-size=1000000 --virtualbox-boot2docker-url https://github.com/gzupark/boot2docker-webcam-mac/releases/download/18.06.1-ce-usb/boot2docker.iso default

  9. docker-machine stop default

  10. Open Virtual Box app & configure the VirtualBox VM that has just been created with docker-machine

    1. Display > Video memory (max)
    2. Display > Acceleration > Enable 3D acceleration (check)
    3. Ports > USB > Enable USB controller (check) > USB 2.0 (select)
    4. Shared folders > Add > Folder Path = / & Folder name = host-root
  11. Reboot macOS

  12. Open a terminal (T1), and type

    1. open -a XQuartz
    2. If it does not open another terminal, focus XQuartz app, and Applications > Terminal
    3. Now a new terminal is opened (T2)
    4. On T2: socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
      • if it complains about "Address already in use", check with lsof -i tcp:6000 that owning process is X11.bin, otherwise kill owning process and try running socat again (always on T2)
  13. On T1 again

    • IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
    • xhost + $IP
    • docker-machine start default
    • eval $(docker-machine env default)
    • vboxmanage list webcams
      • Identify in the list your webcam (eg mine was .1)
    • vboxmanage controlvm default webcam attach .1
  14. (You may need to open VirtualBox again, double click on your VM, which will open a view of your system, and then Devices > Webcam > <select your camera>)

You should now be able to access your camera in a container.

Test XQuartz with

docker run --rm -it -e DISPLAY=$IP:0 gns3/xeyes

Test camera with

docker run --rm -it -e DISPLAY=$IP:0 --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix ubuntu
apt update && apt install -y streamer
streamer -f jpeg -o image.jpeg

Tip

Shutdown your greedy VM process with: VBoxManage controlvm thevm acpipowerbutton

Big thanks to:

Additional notes:

Louis GRIGNON
  • 699
  • 6
  • 17
  • ...what's the question? – David Maze Oct 28 '20 at 12:20
  • Agreed, this would be better as an answer to a well-asked question. – rajah9 Oct 28 '20 at 12:24
  • 1
    I’m voting to close this question because the user states an answer (but has no question). – rajah9 Oct 28 '20 at 12:25
  • I wanted to share this info which I think could help others. What would you recommend to publish this properly on SO please? – Louis GRIGNON Oct 28 '20 at 15:01
  • I posted answers to the following questions which I think could benefit from this solution, therefore, we can close this post, if you think it was a bad idea. Thanks for your help. https://stackoverflow.com/questions/33985648/access-camera-inside-docker-container/64634921#64634921 https://stackoverflow.com/questions/62556566/how-to-access-builtin-webcam-from-a-docker-container-using-ffmpeg/64634956#64634956 – Louis GRIGNON Nov 01 '20 at 17:47

0 Answers0