title: How to connect and use the camera in a docker container on MacOS with Docker Desktop
body: <p>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.</p>
  <p>Note: docker-machine operations can take a while, please be patient :)</p>
  <p>Note 2: Only tested with the built-in camera</p>
  <ol>
  <li><p>Install legacy docker virtualization engine for Docker Desktop on Mac (which uses Oracle Virtual Box)</p>
  <ul>
  <li>Install <a href="https://www.virtualbox.org/" rel="nofollow noreferrer">Virtual Box</a></li>
  <li>Install <a href="https://download.virtualbox.org/virtualbox/6.1.16/Oracle_VM_VirtualBox_Extension_Pack-6.1.16.vbox-extpack" rel="nofollow noreferrer">Virtual Box Extension pack</a></li>
  <li>Install <a href="https://docs.docker.com/docker-for-windows/docker-toolbox/" rel="nofollow noreferrer">Docker Toolbox</a> (reading <a href="https://webcache.googleusercontent.com/search?q=cache:YZ8ksEZDEaYJ:https://docs.docker.com/docker-for-mac/docker-toolbox/%20&amp;cd=1&amp;hl=fr&amp;ct=clnk&amp;gl=fr&amp;client=firefox-b-d" rel="nofollow noreferrer">this</a> is strongly recommended &amp; backuping your <code>/usr/local/bin/docker*</code> before is also recommended)
  <ul>
  <li>Ensure that <code>/usr/local/bin/docker</code> and <code>/usr/local/bin/docker-compose</code> link to Docker Desktop binaries (<code>/Applications/Docker.app/Contents/Resources</code>), and not Docker Toolbox</li>
  <li>Test everything is still working: <code>docker ps -a</code> and <code>docker images</code> should display what you already had in Docker Desktop, <code>docker-machine ls</code> should not raise an error</li>
  </ul>
  </li>
  </ul>
  </li>
  <li><p><code>brew install socat</code></p>
  </li>
  <li><p><code>brew install xquartz</code></p>
  </li>
  <li><p>Setting: XQuartz Preferences &gt; Security &gt; check allow all (Allow connections from network clients)</p>
  </li>
  <li><p><code>defaults write org.macosforge.xquartz.X11 enable_iglx -bool true</code></p>
  </li>
  <li><p><code>IP=$(ifconfig en0 | grep inet | awk '$1==&quot;inet&quot; {print $2}')</code></p>
  </li>
  <li><p><code>xhost + $IP</code></p>
  </li>
  <li><p><code>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</code></p>
  </li>
  <li><p><code>docker-machine stop default</code></p>
  </li>
  <li><p>Open Virtual Box app &amp; configure the VirtualBox VM that has just been created with docker-machine</p>
  <ol>
  <li>Display &gt; Video memory (max)</li>
  <li>Display &gt; Acceleration &gt; Enable 3D acceleration (check)</li>
  <li>Ports &gt; USB &gt; Enable USB controller (check) &gt; USB 2.0 (select)</li>
  <li>Shared folders &gt; Add &gt; Folder Path = / &amp; Folder name = host-root</li>
  </ol>
  </li>
  <li><p>Reboot macOS</p>
  </li>
  <li><p>Open a terminal (<strong>T1</strong>), and type</p>
  <ol>
  <li><code>open -a XQuartz</code></li>
  <li>If it does not open another terminal, focus XQuartz app, and Applications &gt; Terminal</li>
  <li>Now a new terminal is opened (<strong>T2</strong>)</li>
  <li>On <strong>T2</strong>: <code>socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\&quot;$DISPLAY\&quot;</code>
  <ul>
  <li>if it complains about &quot;Address already in use&quot;, check with <code>lsof -i tcp:6000</code> that owning process is X11.bin, otherwise kill owning process and try running socat again (always on <strong>T2</strong>)</li>
  </ul>
  </li>
  </ol>
  </li>
  <li><p>On <strong>T1</strong> again</p>
  <ul>
  <li><code>IP=$(ifconfig en0 | grep inet | awk '$1==&quot;inet&quot; {print $2}')</code></li>
  <li><code>xhost + $IP</code></li>
  <li><code>docker-machine start default</code></li>
  <li><code>eval $(docker-machine env default)</code></li>
  <li><code>vboxmanage list webcams</code>
  <ul>
  <li>Identify in the list your webcam (eg mine was <code>.1</code>)</li>
  </ul>
  </li>
  <li><code>vboxmanage controlvm default webcam attach .1</code></li>
  </ul>
  </li>
  <li><p>(You may need to open VirtualBox again, double click on your VM, which will open a view of your system, and then Devices &gt; Webcam &gt; &lt;select your camera&gt;)</p>
  </li>
  </ol>
  <p>You should now be able to access your camera in a container.</p>
  <p><strong>Test XQuartz with</strong></p>
  <pre><code>docker run --rm -it -e DISPLAY=$IP:0 gns3/xeyes
  </code></pre>
  <p><strong>Test camera with</strong></p>
  <pre><code>docker run --rm -it -e DISPLAY=$IP:0 --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix ubuntu
  apt update &amp;&amp; apt install -y streamer
  streamer -f jpeg -o image.jpeg
  </code></pre>
  <p><strong>Tip</strong></p>
  <p>Shutdown your greedy VM process with: <code>VBoxManage controlvm thevm acpipowerbutton</code></p>
  <p><strong>Big thanks to:</strong></p>
  <ul>
  <li><a href="https://github.com/GzuPark/boot2docker-webcam-mac" rel="nofollow noreferrer">https://github.com/GzuPark/boot2docker-webcam-mac</a></li>
  <li><a href="https://medium.com/@jijupax/connect-the-webcam-to-docker-on-mac-or-windows-51d894c44468" rel="nofollow noreferrer">https://medium.com/@jijupax/connect-the-webcam-to-docker-on-mac-or-windows-51d894c44468</a></li>
  <li><a href="https://askubuntu.com/a/106773">https://askubuntu.com/a/106773</a></li>
  <li><a href="https://apple.stackexchange.com/a/277029">https://apple.stackexchange.com/a/277029</a></li>
  </ul>
  <p><strong>Additional notes:</strong></p>
  <ul>
  <li><a href="https://apple.stackexchange.com/a/277029">https://apple.stackexchange.com/a/277029</a></li>
  <li><a href="https://docs.docker.com/docker-for-mac/docker-toolbox/#docker-toolbox-and-docker-desktop-coexistence" rel="nofollow noreferrer">https://docs.docker.com/docker-for-mac/docker-toolbox/#docker-toolbox-and-docker-desktop-coexistence</a></li>
  <li><a href="https://docs.docker.com/docker-for-mac/docker-toolbox/" rel="nofollow noreferrer">https://docs.docker.com/docker-for-mac/docker-toolbox/</a></li>
  </ul>