audiodebianvscode-devcontainer

Play audio sound output from Debian devcontainer in VSCode


In a Visual Studio Code workspace, I have a devcontainer defined via a devcontainer.json file. It is based on the python:1-3.12-bookworm image. As such, it runs Debian 12. For what it's worth, the host runs a clone of Ubuntu.

I am unable to play audio from the container. I tried via ffplay. There is no sound. I'd like the simplest approach to get audio output to work in this container. I preferably don't want to make any changes whatsoever to the host. I am assuming that setting up ALSA could be simpler than setting up PulseAudio.

I tried installing alsa-utils in the container using apt, but this did not fix the issue.


Solution

  • In devcontainer.json, add the args to provision the sound device, with this presumably getting forwarded to docker. Also add a setup script to run further commands.

    {
        "runArgs": [
            "--device=/dev/snd"
        ],
        "postCreateCommand": "./scripts/setup_debian_devcontainer.sh"
    }
    

    Define the aforementioned setup_debian_devcontainer.sh setup script as below.

    #!/usr/bin/env bash
    set -eux
    
    # Common
    sudo apt update
    ## Audio (Ref: https://stackoverflow.com/a/78750928/)
    sudo cp -v ./.devcontainer/asound.conf /etc/asound.conf  # Note: Specified device number in file is obtained via `aplay -l` on host.
    sudo apt -y install alsa-utils acl
    sudo usermod -aG audio $USER
    sudo setfacl -m u:$USER:rw /dev/snd/*  # Note: Without this, sudo is required when using ffplay to access /dev/snd/*, as the above usermod command is not expected to take effect until a restart.
    
    echo "Finished setting up devcontainer."
    

    Make the setup script executable:

    chmod +x ./scripts/setup_debian_devcontainer.sh
    

    Define the referenced ALSA sound configuration file ./.devcontainer/asound.conf as below. Feel free to improve upon it. Note that it has a device number 3 hardcoded. You can run aplay -l on the host to help decide which device number to specify in the file.

    pcm.!default {
        type hw
        card 0
        device 3
    }
    
    ctl.!default {
        type hw
        card 0
    }
    

    Rebuild the devcontainer. Audio output should now work from it.

    For audio output, be advised that the host's volume control don't apply. Specify the output volume via the container application instead.