-->

Using Audio in Docker Container

dockerdev audio

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:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1

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

ENTRYPOINT ["/play-audio.sh"]

and the script (play-audio.sh) that plays the audio looks like this:

#!/bin/bash

url=$1
filename="${url##*/}"

if [ ! -f $filename ]; then
    echo "File doesn't exist. Downloading."
    wget $url
fi

# 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"
    mpg123 $filename
elif hash afplay 2>/dev/null; then
    echo "Playing file using afplay"
    afplay $filename
else 
    echo "No player could be found."    
fi

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:

Solution

The trick is ro run with the following parameter:

--device /dev/snd

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

Conclusion

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.

Resources