36

I'm trying to dockerize a text to speech application for sharing the code with other developers, however the issue I am having right now is the docker container cannot find the sound card on my host machine.

When I try to play a wav file in my docker container

root@3e9ef1e869ea:/# aplay Alesis-Fusion-Acoustic-Bass-C2.wav
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4738:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default
aplay: main:722: audio open error: No such file or directory

I guess that the main problem is docker container is unable reach the sound card on my host.

So far I have

  1. I installed alsa-utils and most of the alsa dependencies within my docker container.
  2. Added --group-add audio while running the container by specifying docker run --group-add audio -t -i self/debian /bin/bash

I am not sure if this is even possible with docker(I'm not exactly sure of how hardware resources such as sound cards are shared with containers). I'm using a debian container on a Mac OS Yosemite host.

Jeremiah Rose
  • 3,865
  • 4
  • 27
  • 31
Anoop
  • 5,540
  • 7
  • 35
  • 52

1 Answers1

35

It is definitely possible, you need to mount /dev/snd, see how Jess Frazelle launches a Spotify container, from

https://blog.jessfraz.com/post/docker-containers-on-the-desktop/

you will notice

docker run -it \
    -v /tmp/.X11-unix:/tmp/.X11-unix \ # mount the X11 socket
    -e DISPLAY=unix$DISPLAY \ # pass the display
    --device /dev/snd \ # sound
    --name spotify \
    jess/spotify

or for Chrome, at the end

docker run -it \
    --net host \ # may as well YOLO
    --cpuset-cpus 0 \ # control the cpu
    --memory 512mb \ # max memory it can use
    -v /tmp/.X11-unix:/tmp/.X11-unix \ # mount the X11 socket
    -e DISPLAY=unix$DISPLAY \ # pass the display
    -v $HOME/Downloads:/root/Downloads \ # optional, but nice
    -v $HOME/.config/google-chrome/:/data \ # if you want to save state
    --device /dev/snd \ # so we have sound
    --name chrome \
    jess/chrome
Rob
  • 12,659
  • 4
  • 39
  • 56
user2915097
  • 30,758
  • 6
  • 57
  • 59
  • 3
    Would you know how to get this working on Windows as a host? Specifically, what the SND device alternative is on Windows? – readytotaste Jun 05 '18 at 22:01
  • 1
    Any suggestions how to do the same on windows? – Ievgen Dec 03 '18 at 07:34
  • Will this work with JACK? Will I need to install any of that audio software (ALSA, pulseaudio or JACK) in the container, or is it sufficient to have them installed in the host machine? – Jeffrey Benjamin Brown Dec 17 '18 at 16:12
  • What is the equivalent of `--device /dev/snd` on Windows? – Wlad Nov 14 '19 at 01:01
  • No - this is not possible on WSL at the moment, see here https://github.com/microsoft/WSL/issues/689 – Ani Dec 13 '19 at 19:13
  • 7
    As for the OP, I am also running MacOS and there is no `/dev/snd` file on my mac. The docker answer is: `docker: Error response from daemon: error gathering device information while adding custom device "/dev/snd": no such file or directory.` – Kubuntuer82 May 01 '20 at 09:38
  • There is still no sound when I try to play videos with MPV player. – JulianLai May 09 '20 at 10:00
  • If you just want to **work around** this issue, map `--device /dev/null:/dev/snd`. This won't give you sound, but it will help you get the container running so you at least have visual output. So the first step to a solution is: Map *something*. – kaiser May 09 '20 at 22:43
  • For macOS you can try this, https://devops.datenkollektiv.de/running-a-docker-soundbox-on-mac.html – dragon788 Jul 02 '20 at 22:25
  • Dare you try this on kubernetes with Containerd – mr haven Dec 11 '20 at 16:09