How do I make ALSA playback available in docker?

  docker, question

Pull a ros:kinetic-robot image before, this image is based on ubuntu, and then hang the wav code played by ALSA to execute it.

docker run -it \
 --device /dev/snd:/dev/snd \
 -v /media/dyan/project/projects/voice/:/data/voice/ \
 ros:kinetic-robot

There was an error in playing the audio, and there was a problem opening the sound card device.

Alsallibpcm _ dmix.c: 1029: (snd _ pcm _ dmix _ open) unattble to openslave # this is an exception to the output of snd_pcm_open ()
 Cannot open audio device default (nosuchfile or directory) # this line was printed after err_num=snd_pcm_open () failed, and the information in parentheses is snd_strerror(err_num)

I checked some related issuesrun apps using audio in a docker container
But it still cannot be solved.
Inside the containerapt search alsa, alsa has a library, and the code can be compiled through in the container.

root@72b53ba14730:/# apt search alsa
 Sorting... Done
 Full Text Search... Done
 libasound2/now 1.1.0-0ubuntu1 amd64 [installed,local]
 shared library for ALSA applications
 
 libasound2-data/now 1.1.0-0ubuntu1 all [installed,local]
 Configuration files and profiles for ALSA drivers
 
 libasound2-dev/now 1.1.0-0ubuntu1 amd64 [installed,local]
 shared library for ALSA applications -- development files
 
 libgcrypt20/now 1.6.5-2ubuntu0.4 amd64 [installed,local]
 LGPL Crypto library - runtime library
 
 libsdl1.2debian/now 1.2.15+dfsg1-3 amd64 [installed,local]
 Simple DirectMedia Layer

I don’t know what the problem is, is there something wrong with the device mapping?

Found it
Link description
Link description

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) \
 -v /media/dyan/project/projects/voice/:/data/voice/ \
 ros:kinetic-robot

Then you need to install some more packages.

apt update && apt install alsa-base pulseaudio

Well, it can now be played normally.