How to set ALSA default device to pulseaudio sound server on Docker?

12,396

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
[email protected]:/# apt update
[email protected]:/# apt install alsa-base alsa-utils pulseaudio
[email protected]:/# 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
[email protected]:/# 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
[email protected]:/# 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.

Share:
12,396

Related videos on Youtube

nekketsuuu
Author by

nekketsuuu

Updated on September 18, 2022

Comments

  • nekketsuuu
    nekketsuuu 2 months

    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
    [email protected]:/# apt update
    [email protected]:/# apt install alsa-base alsa-utils pulseaudio
    [email protected]:/# 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
    [email protected]:/# 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
    [email protected]:/# 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.
    • Socrates
      Socrates about 5 years
    • nekketsuuu
      nekketsuuu about 5 years
      @Socrates Thanks for your suggestion, but I already tried it. The key of that answer is to use --device /dev/snd.
  • Web User
    Web User about 3 years
    Can I achieve a similar goal with a Ubuntu Docker container running on a Windows 10 host?
  • nekketsuuu
    nekketsuuu about 3 years
    @WebUser No. I don't know the details but some configuration, such as file paths, must be different. In addition, you should install PulseAudio to Windows if you want to follow this answer. I recommend that you ask a new question about the configuration.
  • LIH over 1 year
    In Ubuntu server 20.04 I get this error: WARNING: The XDG_RUNTIME_DIR variable is not set. Defaulting to a blank string.