Enabling Sound Card Access in Docker Using PulseAudio

This guide explains how to provide sound card access to applications running inside a Docker container on Red Hat Linux by sharing the host’s PulseAudio server. This method allows you to run audio-enabled applications in a container while using the host's audio resources.

Prerequisites

  • Red Hat-based OS (RHEL, CentOS, Fedora)

  • Docker installed and configured on your system

  • Basic familiarity with Docker commands

Step 1: Install PulseAudio on the Host

Ensure PulseAudio is installed on your host system. On Red Hat-based systems, install it with:

sudo dnf install pulseaudio

Step 2: Configure Docker to Use the Host's PulseAudio Socket

To share the host's PulseAudio socket with the container, mount the PulseAudio socket as a volume and set the PULSE_SERVER environment variable.

Run the container with the following command:

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 /run/user/$(id -u)/pulse:/run/user/$(id -u)/pulse \
    -e PULSE_COOKIE=${XDG_RUNTIME_DIR}/pulse/cookie \
    your_image

Explanation of Parameters:

  • --device /dev/snd: Grants the container access to the host's sound devices.

  • -e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native: Sets the PULSE_SERVER environment variable in the container to point to the host's PulseAudio server.

  • -v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native: Mounts the host’s PulseAudio socket, allowing applications in the container to connect to the host's audio server.

  • -v /run/user/$(id -u)/pulse:/run/user/$(id -u)/pulse: Shares the PulseAudio runtime directory with the container.

  • -e PULSE_COOKIE=${XDG_RUNTIME_DIR}/pulse/cookie: Provides authentication for PulseAudio by sharing the cookie file.

Step 3: Test Audio Playback in the Container

After starting the container, you can test audio playback. First, ensure you have paplay (PulseAudio play utility) installed:

yum install -y pulseaudio-utils

Then, test sound output with:

paplay /usr/share/sounds/alsa/Front_Center.wav

If everything is set up correctly, you should hear the sound playing through your host’s audio output.


By following these steps, you enable applications running inside Docker to access and use the host’s audio resources via PulseAudio on Red Hat Linux. This approach is both flexible and compatible with most modern audio applications.