Playing Sound in Docker Containers: A Step-by-Step Guide

Prateek Srivastava
3 min readJan 1, 2024

Introduction:

Docker containers have become an essential part of modern development workflows, allowing developers to package and distribute applications with all their dependencies. While containers are commonly used for server-side applications, they can also be utilized for multimedia tasks, such as playing audio files. In this blog post, we’ll explore a simple yet effective method to play sound within a Docker container.

Setting up the Dockerfile:

Let’s begin by creating a Dockerfile that sets up a minimal Debian-based container with the necessary packages for audio playback.

#Dockerfile

# Use a minimal base image
FROM debian:bullseye-slim

# Install required packages
RUN apt-get update && apt-get install -y alsa-utils mpg123

# Copy your favorite song into the container
COPY bhajan.mp3 /home/user/

# Set the default command to play the audio file with mpg123
CMD ["mpg123", "/home/user/bhajan.mp3"]

In this Dockerfile, we use the debian:bullseye-slim base image, install the alsa-utils and mpg123 packages, and copy an example audio file (bhajan.mp3) into the container. The default command runs the mpg123 player to play the specified audio file.

Building the Docker Image:

Once the Dockerfile is ready, build the Docker image using the following command:

docker build -t audio-container:v1 .

Running the Docker Container:

To run the Docker container and play the audio file, use the following command:

docker run --rm -it --privileged=true --device=/dev/snd:/dev/snd audio-container:v1

Here, the --rm flag removes the container when it exits, and --privileged=true --device=/dev/snd:/dev/snd grants necessary privileges and devices for interacting with the host's sound system.

Conclusion:

Playing sound within a Docker container is achievable by configuring the container to interact with the host’s audio subsystem. In this example, we used the mpg123 player to play an audio file within a minimal Debian-based container.

Feel free to adapt this approach to your specific needs, and explore additional audio tools or configurations based on your application requirements. Docker’s flexibility allows developers to seamlessly integrate multimedia functionality into containerized applications, opening up new possibilities for diverse use cases.

🙏 Thank You for Reading!

Your time and attention mean a lot to us. We greatly appreciate your engagement with our content and would love to hear your thoughts and ideas. Let’s stay connected and continue our journey through the digital world together! 🌐📚💡

Let’s Stay in Touch 🪂 Connect with us on: LinkedIn, Instagram

Sign up to discover human stories that deepen your understanding of the world.

Prateek Srivastava
Prateek Srivastava

Written by Prateek Srivastava

Motivated and innovative professional with 6 years of versatile experience spanning Site Reliability Engineering (SRE) and Research & Development (R&D).

Responses (1)

Write a response

This works like a charm!, but audio goes to pc, I want to send to hdmi. Any idea?