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
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
anddocker images
should display what you already had in Docker Desktop,docker-machine ls
should not raise an error
- Ensure that
brew install socat
brew install xquartz
Setting: XQuartz Preferences > Security > check allow all (Allow connections from network clients)
defaults write org.macosforge.xquartz.X11 enable_iglx -bool true
IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
xhost + $IP
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
docker-machine stop default
Open Virtual Box app & configure the VirtualBox VM that has just been created with docker-machine
- Display > Video memory (max)
- Display > Acceleration > Enable 3D acceleration (check)
- Ports > USB > Enable USB controller (check) > USB 2.0 (select)
- Shared folders > Add > Folder Path = / & Folder name = host-root
Reboot macOS
Open a terminal (T1), and type
open -a XQuartz
- If it does not open another terminal, focus XQuartz app, and Applications > Terminal
- Now a new terminal is opened (T2)
- 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)
- if it complains about "Address already in use", check with
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
)
- Identify in the list your webcam (eg mine was
vboxmanage controlvm default webcam attach .1
(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:
- https://github.com/GzuPark/boot2docker-webcam-mac
- https://medium.com/@jijupax/connect-the-webcam-to-docker-on-mac-or-windows-51d894c44468
- https://askubuntu.com/a/106773
- https://apple.stackexchange.com/a/277029
Additional notes: