title: Docker : sharing /dev/snd on multiple containers leads to "device or resource busy"
body: <p>When <a href="https://docs.docker.com/engine/reference/commandline/run/#add-host-device-to-container---device" rel="noreferrer">adding host device</a> (<code>--device /dev/snd</code>) to a Docker container, I sometimes encounter <code>Device or resource busy</code> errors.</p>
  
  <h2>Example</h2>
  
  <p>I have reproduced the issue with a minimal example involving audio (<a href="http://www.alsa-project.org/main/index.php/Main_Page" rel="noreferrer"><code>alsa</code></a>). Here's my <code>Dockerfile</code> (producing an image <code>docker-device-example</code>) :</p>
  
  <pre><code>FROM    debian:buster
  
  RUN     apt-get update \
   &amp;&amp;     apt-get install -y --no-install-recommends \
              alsa-utils \
   &amp;&amp;     rm -rf /var/lib/apt/lists/*
  </code></pre>
  
  <p>I am running the following command (<a href="http://www.alsa-project.org/main/index.php/SoundcardTesting" rel="noreferrer"><code>speaker-test</code></a> is a tool to generate a tone that can be used to test the speakers), with <code>/dev/snd</code> shared :</p>
  
  <pre><code>docker run --rm \
      -i -t \
      --device /dev/snd \
      docker-device-example \
      speaker-test
  </code></pre>
  
  <h2>Issue</h2>
  
  <p>When running the previous command, a pink noise is played, but <strong>only under some conditions</strong> :</p>
  
  <ul>
  <li>if I am not playing any sound on my host : for example, if I'm playing a video, and that even if the video is paused, the command fails</li>
  <li>if I am not running another container accessing the <code>/dev/snd</code> device</li>
  </ul>
  
  <p>It looks like the <code>/dev/snd</code> is "locked" when used, and if that is the case, I got the following output (the error is represented by the last 2 lines) :</p>
  
  <pre><code>speaker-test 1.1.6
  
  Playback device is default
  Stream parameters are 48000Hz, S16_LE, 1 channels
  Using 16 octaves of pink noise
  ALSA lib pcm_dmix.c:1099:(snd_pcm_dmix_open) unable to open slave
  Playback open error: -16,Device or resource busy
  </code></pre>
  
  <p>And, vice versa, if the pink noise is played (on the container), then I cannot play any sound on my host (Ubuntu). But commands on my host does not fail with the same message. Instead, the command on the host (like <code>aplay test.wav</code> to play a simple sound) is blocked indefinitely (even when the container is shutdown afterwards).</p>
  
  <p>I tried to debug by running <code>strace aplay test.way</code>, and the command seems to be blocked on the <a href="http://man7.org/linux/man-pages/man2/poll.2.html" rel="noreferrer"><code>poll</code></a> system call :</p>
  
  <pre><code>poll([{fd=3, events=POLLIN|POLLERR|POLLNVAL}], 1, 4294967295
  </code></pre>
  
  <h2>Question</h2>
  
  <p>How can I play sounds from 2 (or more) different containers, or from my host and a container, at the same time?</p>
  
  <h2>Additional info</h2>
  
  <p>I've reproduced the issue with <code>/dev/snd</code>, but I don't know if similar things happen when using other devices, or if it's just related to sound devices or to <code>alsa</code>.</p>
  
  <p>Note also that when running multiple <code>speaker-test</code> or <code>aplay</code> commands simultaneously and <strong>all on my host</strong> (no containers involved), then all sounds are played.</p>