dockeraudiodocker-composedockerfilealsa

How to connect alsa audio to docker


I have a docker container running on a raspberry pi 5 and my goal is to access the alsa audio devices of the raspy inside of the container.

Currently I get the following error when executing

aplay someaudio.wav

aplay: main:831: audio open error: Unknown error 524

Outside of the container audio is working

My dockerfile looks like this:

FROM python:3.12.4-slim-bookworm

ARG TARGETARCH

RUN apt update && apt upgrade -y && \
apt install -y wget gcc swig libmariadb-dev libasound2-dev alsa-utils

RUN if [ "$TARGETARCH" == "armhf" ]; then\
apt install build-essentials cmake -y ; \
else \
    apt install build-essential -y ; \
fi

COPY src/app /app

RUN pip install -r /app/requirements.txt

WORKDIR /app
ENTRYPOINT [ "uvicorn", "app:app", "--port", "8000", "--host", "0.0.0.0" ]

and this is the docker-compose

backend:
    image: pythonbasedimage
    volumes:
      - ${PWD}/.env:/app/.env
    devices:
      - "/dev/snd:/dev/snd"
    privileged: true
    ports:
      - "8000:8000"
    deploy:
      resources:
        limits:
          cpus: "0.5"

alsactl info shows the following:

#
# Sound card
#
- card: 0
  id: vc4hdmi0
  name: vc4-hdmi-0
  longname: vc4-hdmi-0
  driver_name: vc4-hdmi
  mixer_name:
  components:
  controls_count: 5
  pcm:
    - stream: PLAYBACK
      devices:
        - device: 0
          id: MAI PCM i2s-hifi-0
          name: MAI PCM i2s-hifi-0
          subdevices:
            - subdevice: 0
              name: subdevice #0
alsactl: rawmidi_device_list:105: snd_ctl_rawmidi_next_device

Solution

  • I was able to fix it using pipewire, as described in this stackoverflow answer: Answer

    My changes to the Dockerfile were:

    RUN apt update && apt upgrade -y && \
    apt install -y wget gcc swig libmariadb-dev pipewire-alsa pipewire libasound2-dev alsa-utils
    

    and in the docker compose I changed the following:

    backend:
        image: someimage
        depends_on:
          - db
          - asterisk
        volumes:
          - ${PWD}/.env:/app/.env
          - /run/user/1000/pipewire-0:/tmp/pipewire-0
        environment:
          - XDG_RUNTIME_DIR=/tmp
        privileged: true
        ports:
          - "8000:8000"
        deploy:
          resources:
            limits:
              cpus: "0.5"