How to use alsa sound and/or snd_pcm_open in docker?

I am running an Ubuntu 12.04 Docker container on an Ubuntu 16.04 host. Some test code I have exercises ‘snd_pcm_open’/’snd_pcm_close’ operations with the SND_PCM_STREAM_PLAYBACK and SND_PCM_STREAM_CAPTURE stream types.

I do not need any actual sound/audio capabilities but just getting the ‘snd_pcm_open’ return 0 with a valid handle, then ‘snd_pcm_close’ to return 0 on the same handle would be good enough for my purposes. I do not want to modify the code as it’s already got some not-so-nice platform dependent switches and I am not the maintainer.

  • Docker with a Virtual Sound Card for Stream Altering
  • Access Windows GPU in Boot2Docker
  • chrome fake audioinput with real audio output?
  • How to play sound in a docker container
  • Record Sound on Ubuntu Docker Image
  • Running MPD server with Mopidy docker container, how do I play on Mac?
  • I am using the simple code and compiling it as ‘g++ alsa_test.cpp -lasound’

    #include <stdio.h>
    #include <alsa/asoundlib.h>
    int main() {
        snd_pcm_t* handle;
        snd_pcm_stream_t stream_type[]= {SND_PCM_STREAM_PLAYBACK, SND_PCM_STREAM_CAPTURE};
        for (unsigned char i = 0; i < sizeof(stream_type) / sizeof(stream_type[0]); ++i) {
            int deviceResult = snd_pcm_open(&handle, "default" , stream_type[i], 0);
            printf("\n%d open: %d\n", stream_type[i], deviceResult);
            if (deviceResult >= 0) {
                printf("attempting to close %d\n", stream_type[i]);
                deviceResult = snd_pcm_close(handle);
                printf("%d close: %d\n\n", stream_type[i], deviceResult);
        return 0;

    It works just fine on the host but despite all the different things I tried, ‘snd_pcm_open’ returns ‘-2’ for both stream types in the container.

    I tried installing the ‘’ but ‘modinfo soundcore’ is empty and ‘/dev/snd’ does not exist.

    Also tried running the container with the options below, even though it feels like a massive over kill for such a simple purpose

    --privileged --cap-add=ALL -v /dev:/dev -v /lib/modules:/lib/modules

    After these extra parameters to the container, following commands generate the same output both in the host and the container.

    root@31142791f82d:/export# modinfo soundcore
    filename:       /lib/modules/4.4.0-59-generic/kernel/sound/soundcore.ko
    alias:          char-major-14-*
    license:        GPL
    author:         Alan Cox
    description:    Core sound module
    srcversion:     C941364F5CD0B525693B243
    intree:         Y
    vermagic:       4.4.0-59-generic SMP mod_unload modversions 
    parm:           preclaim_oss:int
    root@31142791f82d:/export# ls -l /dev/snd/
    total 0
    drwxr-xr-x  2 root root      100 Feb  2 21:10 by-path
    crw-rw----+ 1 root audio 116,  2 Feb  2 07:42 controlC0
    crw-rw----+ 1 root audio 116,  7 Feb  2 07:42 controlC1
    crw-rw----+ 1 root audio 116, 12 Feb  2 21:10 controlC2
    crw-rw----+ 1 root audio 116,  6 Feb  2 07:42 hwC0D0
    crw-rw----+ 1 root audio 116, 11 Feb  2 07:42 hwC1D0
    crw-rw----+ 1 root audio 116,  3 Feb  2 07:42 pcmC0D3p
    crw-rw----+ 1 root audio 116,  4 Feb  2 07:42 pcmC0D7p
    crw-rw----+ 1 root audio 116,  5 Feb  2 07:42 pcmC0D8p
    crw-rw----+ 1 root audio 116,  9 Feb  2 10:44 pcmC1D0c
    crw-rw----+ 1 root audio 116,  8 Feb  2 07:42 pcmC1D0p
    crw-rw----+ 1 root audio 116, 10 Feb  2 21:30 pcmC1D1p
    crw-rw----+ 1 root audio 116, 14 Feb  2 21:10 pcmC2D0c
    crw-rw----+ 1 root audio 116, 13 Feb  2 21:10 pcmC2D0p
    crw-rw----+ 1 root audio 116,  1 Feb  2 07:42 seq
    crw-rw----+ 1 root audio 116, 33 Feb  2 07:42 timer

    The container only has the ‘root’ user by the way, so, access rights shouldn’t be an issue either.

    What would be the easiest and least hacky way to get this working? I’d rather get rid off the privileged mode and dev/modules mapping to the container however, these containers are not accessed from the outside world and are only created/destroyed for some short lived tasks. So, safety isn’t exactly a massive concern.

    Thanks in advance.

  • How to mimic Travis CI locally?
  • Kubernetes replication controller in CrashLoopBackOff state
  • Is there any way to make sure you read line by line from docker-py logs when in stream mode?
  • docker - NGINX Proxy + Let's encrypt
  • docker commit mysql doesn't save
  • Docker on Google Compute f1-micro instance
  • One Solution collect form web for “How to use alsa sound and/or snd_pcm_open in docker?”

    If you don’t actually need the device to work correctly, use the null device instead of default.

    To make the null plugin the default one, put this into the container’s /etc/asound.conf, or into the user’s ~/.asoundrc:

    pcm.!default = null;
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.