When adding host device (--device /dev/snd
) to a Docker container, I sometimes encounter Device or resource busy
errors.
Example
I have reproduced the issue with a minimal example involving audio (alsa
). Here's my Dockerfile
(producing an image docker-device-example
) :
FROM debian:buster
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
alsa-utils \
&& rm -rf /var/lib/apt/lists/*
I am running the following command (speaker-test
is a tool to generate a tone that can be used to test the speakers), with /dev/snd
shared :
docker run --rm \
-i -t \
--device /dev/snd \
docker-device-example \
speaker-test
Issue
When running the previous command, a pink noise is played, but only under some conditions :
- 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
- if I am not running another container accessing the
/dev/snd
device
It looks like the /dev/snd
is "locked" when used, and if that is the case, I got the following output (the error is represented by the last 2 lines) :
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
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 aplay test.wav
to play a simple sound) is blocked indefinitely (even when the container is shutdown afterwards).
I tried to debug by running strace aplay test.way
, and the command seems to be blocked on the poll
system call :
poll([{fd=3, events=POLLIN|POLLERR|POLLNVAL}], 1, 4294967295
Question
How can I play sounds from 2 (or more) different containers, or from my host and a container, at the same time?
Additional info
I've reproduced the issue with /dev/snd
, but I don't know if similar things happen when using other devices, or if it's just related to sound devices or to alsa
.
Note also that when running multiple speaker-test
or aplay
commands simultaneously and all on my host (no containers involved), then all sounds are played.
pulseaudio
is not a problem for me. FI, the 2nd solution doesn't work (command for playing sound hangs and crash with aPlayback open error: -2,No such file or directory
), but since I'm running containers on the same host, the 1st solution fits better. I'll accept this answer soon if there is no more answers to my question with only ALSA. – Durance