Docker Container Audio
Introduction
In some applications, we would like to record and play audio in a Docker container. In this blog post, I would like to discuss how to setup such a Docker container and how to record and play audio inside the Docker container.
Docker Container Audio
Build Docker Image
The audio.Dockerfile
is a minimum Docker file for building an environment that allows recording and playing audio.
1 | FROM ubuntu:22.04 |
To build the Docker image, please run the following command.
1 | $ docker build -f docker/audio.Dockerfile --no-cache --tag audio:0.0.1 . |
Run Docker Container
To run the Docker container, please run the following command. Notice that --device /dev/snd
is necessary for Docker container to have access to the audio device on the host machine.
1 | $ docker run -it --rm --device /dev/snd audio:0.0.1 |
List Audio Devices
In the Docker container, we could list the audio devices that are available using the following command.
1 | $ arecord -l |
Here the ALC1220
device on card 0
is the audio device on my ROG Z390 motherboard which does not have a microphone and the C930e
on card 2
is my Logitech webcam which has a microphone that we will use for recording audio.
Record Audio
To record a piece of audio using the webcam, please run the following command. Notice that --device="hw:2,0"
specifies the recording to use the C930e
webcam.
1 | $ arecord -f S16_LE -c 2 -d 10 -r 48000 --device="hw:2,0" /tmp/test-mic.wav |
Play Audio
To play the audio we just recorded, please run the following command.
1 | $ aplay /tmp/test-mic.wav |
Docker Container Audio