How do I make ALSA playback available in docker?

before pull, there is a ros:kinetic-robot image, which is based on ubuntu, and then hang up the previous ALSA playback wav code to execute

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

error occurred when playing audio. There was a problem when opening the sound card device

ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave -sharpsnd_pcm_open()
cannot open audio device default (No such file or directory) -sharperr_num=snd_pcm_open()snd_strerror(err_num)

looked at some related problems run apps using audio in a docker container
but still cannot be solved.
in the container apt search alsa , there is a library of alsa, and the code can also be compiled in the container.

root@72b53ba14730:/-sharp 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?

Mar.18,2021

found
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 packages

apt update && apt install alsa-base pulseaudio

well, it can be played normally now

Menu