2

I want to play sound on Ubuntu 16.04 on Docker using PulseAudio and ALSA. However it outputs an error Device or resource busy. I think this is because the default device of ALSA is not set to the PulseAudio sound server (Note: it's set to the PulseAudio on the host Ubuntu). How can I change the default device to it? For example, can I do so by creating ~/.asoundrc or by modifying /etc/pulse/client.conf?

Steps to reproduce

$ # On the host Ubuntu...
$ aplay -L | head -n9
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
$ aplay /usr/share/sounds/alsa/Front_Center.wav 
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
$ paplay /usr/share/sounds/alsa/Front_Center.wav  # Success
$ # On the Docker container...
$ sudo docker run -it --device /dev/snd ubuntu:16.04 /bin/bash
root@81af4bf99890:/# apt update
root@81af4bf99890:/# apt install alsa-base alsa-utils pulseaudio
root@81af4bf99890:/# aplay -L | head -n10
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
default:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
root@81af4bf99890:/# aplay /usr/share/sounds/alsa/Front_Center.wav 
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
aplay: main:722: audio open error: Device or resource busy
root@81af4bf99890:/# paplay /usr/share/sounds/alsa/Front_Center.wav 
Connection failure: Connection refused
pa_context_connect() failed: Connection refused

Environment

  • Host: Ubuntu 17.04
  • Docker: 17.09.0-ce

Additional notes

  • On my environment, Jess's images don't work, too. Here is an example log.

    $ # When I play some sounds on host, `jess/spotify` fails to play music.
    $ sudo docker run -it \
        -v /tmp/.X11-unix:/tmp/.X11-unix \
        -e DISPLAY=unix$DISPLAY \
        --device /dev/snd \
        --name spotify \
        jess/spotify
    Gtk-Message: Failed to load module "canberra-gtk-module"
    libGL error: MESA-LOADER: failed to retrieve device information
    libGL error: Version 4 or later of flush extension not found
    libGL error: failed to load driver: i915
    libGL error: failed to open drm device: No such file or directory
    libGL error: failed to load driver: i965
    [1115/043835.051841:ERROR:sandbox_linux.cc(344)] InitializeSandbox() called with multiple threads in process gpu-process.
    [1115/043840.945653:ERROR:web_plugin_impl.cc(38)] Widevine registration is not supported after context initialization
    
    (spotify:1): GLib-GIO-CRITICAL **: g_dbus_connection_send_message: assertion 'G_IS_DBUS_CONNECTION (connection)' failed
    ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
    

    The last line says that there is an error related to ALSA. (Note that jess/spotify can play music when there is no other software that plays sound. This may be because the sound device is not busy in such a case.)

  • Although I self-answered this question, I still want more sophisticated solution or detailed explanation of the reason that some options are needed. Any comments would be appreciated. Thank you.
2

Thanks to fsmunoz's this comment, I can play sounds on the container using pulseaudio, without modifying any config files. The points are the following:

And here is a working example:

$ sudo docker run -it \
    --device /dev/snd \
    -e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \
    -v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native \
    -v ~/.config/pulse/cookie:/root/.config/pulse/cookie \
    --group-add $(getent group audio | cut -d: -f3) \
    ubuntu:16.04 /bin/bash
root@9c9f7e0db4e3:/# apt update
root@9c9f7e0db4e3:/# apt install alsa-base alsa-utils pulseaudio
root@eed016c1fb61:/# aplay -L | head -n9
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
root@9c9f7e0db4e3:/# aplay /usr/share/sounds/alsa/Front_Center.wav 
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
root@9c9f7e0db4e3:/# paplay /usr/share/sounds/alsa/Front_Center.wav  # Success!

Note: Still, I don't know why the cookie is needed. Without the cookie, an error ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Access denied occurred.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.