adapted from this

setup host

run setup_pulse.sh from the host environment, as your normal user (i.e. probably not root unless that is how your environment is set up)

  • only needs to happen once (per login session)

setup_pulse.sh

#!/bin/bash
set -e
 
# remove existing files (e.g. from prior run)
rm /tmp/pulseaudio.socket && true
rm /tmp/pulseaudio.client.conf && true
 
pactl load-module module-native-protocol-unix socket=/tmp/pulseaudio.socket
 
cat <<EOT >> /tmp/pulseaudio.client.conf
default-server = unix:/tmp/pulseaudio.socket
# Prevent a server running in the container
autospawn = no
daemon-binary = /bin/true
# Prevent the use of shared memory
enable-shm = false
EOT

modify container run command

# add to env variables
-e PULSE_SERVER=/tmp/pulseaudio.socket
-e PULSE_COOKIE=/tmp/pulseaudio.cookie
 
# add to volume mounts
-v /tmp/pulseaudio.socket:/tmp/pulseaudio.socket
-v /tmp/pulseaudio.client.conf:/etc/pulse/client.conf