Using Audio in Docker Container
In this post I’m going to show an example of playing audio in a Docker container.
Test Environment Setup
I’m going to use dotnet core 3.1 runtime image:
docker pull mcr.microsoft.com/dotnet/core/runtime:3.1
Here’s my Dockerfile:
RUN apt-get update -y
RUN apt-get install mpg123 -y
RUN apt-get install wget -y
COPY ./play-audio.sh .
RUN chmod +x ./play-audio.sh
and the script (play-audio.sh) that plays the audio looks like this:
if [ ! -f $filename ]; then
echo "File doesn't exist. Downloading."
# check if the audio player program exists. helpful to test the script individually on macOS
if hash mpg123 2>/dev/null; then
echo "Playing file using mpg123"
elif hash afplay 2>/dev/null; then
echo "Playing file using afplay"
echo "No player could be found."
I built the image with the following command:
docker build -t audio-test .
Testing the audio
Initially I rana container like this:
docker run audio-test https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_5MG.mp3
After running the container like this I got the error shown below:
The trick is ro run with the following parameter:
So the full Docker run command looks like this:
docker run --rm --device /dev/snd audio-test https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_5MG.mp3
This was a long-winded setup for a very short solution but I enjoyed practicing with Bash scripting and Docker.
Unfortunately this solution works on Raspberry Pi only and not on Mac. Every resource I found points to installing Pulse Audio Server on macOS and Pulse Audio client in the Docker image. I haven’t tried it yet as it was beyond the scope of my requirements but I might need to implement it later in which case I will post about it.