Playing sound in Docker container on WSL in Windows 11 - windows

Newer versions of Windows (build 21364 or later) enable GUI support in WSL2, including full audio support. This is pretty amazing.
Given that WSL2 now supports audio, how do we then get audio to work inside a Docker container running on Windows, using Docker's WSL2 engine?
On a Linux host, you can simply invoke a Docker image with e.g --device /dev/snd.
What is the equivalent command to run a sound-enabled Docker image from the Windows command line?

There is no /dev/snd or direct audio hardware emulation available in WSL, even in the new Windows 11 setup. Instead, the newest release of WSL achieves audio output by pointing PulseAudio clients to an external PulseAudio server via a socket in /mnt/wslg/. This server runs on yet another Linux distro calleed WSLg that runs inside Windows for the sole purpose of plumbing audio and graphical data between your WSL distro and Windows.
It looks a bit like this:
Docker container <-> WSL <-> WSLg <-> Windows 11
The actual PulseAudio server is located in WSLg. The location of the socket is stored in the environment variable PULSE_SERVER:
# On WSL
echo $PULSE_SERVER
/mnt/wslg/PulseServer
So, aside from making sure your Dockerfile has a basic PulseAudio installation (RUN apt-get install -y pulseaudio or equivalent), all you need to do is invoke the image like:
In WSL:
docker run -t -i -e "PULSE_SERVER=${PULSE_SERVER}" -v /mnt/wslg/:/mnt/wslg/ image_name
Or from the Windows command line like:
wsl docker run -t -i -e "PULSE_SERVER=/mnt/wslg/PulseServer" -v /mnt/wslg/:/mnt/wslg/ image_name
Or
docker run -t -i -e "PULSE_SERVER=/mnt/wslg/PulseServer" -v \\wsl$\Ubuntu\mnt\wslg:/mnt/wslg/ image_name

Related

bluetooth in docker for windows host

My docker version is:
docker --version
Docker version 20.10.2, build 2291f61
My windows version is:
systeminfo
Nom du système d’exploitation: Microsoft Windows 10 Professionnel
Version du système: 10.0.17763 N/A version 17763
Type du système: x64-based PC
My Dockerfile is:
FROM ubuntu:21.04
RUN apt update
RUN apt-get install -y bluez bluetooth usbutils
When I run the following command, I start the 'bluetooth_in_docker' container:
docker build -t bluetooth_in_docker . & docker run --privileged --net=host -it bluetooth_in_docker bash
Inside the container when I run the following, I get an error:
hciconfig dev
Can't open HCI socket.: Address family not supported by protocol
I got it working on Windows from inside WSL2, but it takes a lot of steps.
Follow https://github.com/dorssel/usbipd-win/discussions/310 to get
your bluetooth working inside WSL2. Verify that you can scan for
bluetooth devices inside your WSL2 distro.
modify your dockerfile to install bluetooth as you did (bluez and usb-utils might not be needed)
Now there are 2 options. First option shares bluetooth with container. Second option gives container exclusive control.
Sharing bluetooth between the host and the container is possible by making a volume mount of /var/run/dbus and running it with --privileged:
docker run -v /var/run/dbus/:/var/run/dbus/:z --privileged {containerImage}
Make sure that the dbus and bluetooth services are working in your host when running the container this way.
Giving the container exclusive control: in WSL2 (the host), run a docker container according to https://stackoverflow.com/a/64126744/1114918
run sudo service bluetooth stop to make your bluetooth not "claimed" by the host (the linked answer uses killall, I think sudo service ... stop is cleaner)
use a sh script to start dbus and bluetooth inside the container
run the container using
docker run --rm --net=host --privileged myimage:mytag

Fabric-Composer on Windows 10 linux sub system?

Has anyone succeeded in running fabric-composer on windows 10 linux sub-system running ubuntu 16.04 ?
You can run composer on windows 10 WSL (windows Subsystem for linux) but you will not be able to run docker containers in it. Linux Docker containers require a linux kernel and WSL doesn't have a linux kernel. It is a clever piece of technology that converts user space Linux API calls dynamically to windows API calls.
Therefore you will have to run hyperledger fabric either by using docker for windows (which runs it for you in hyper-v) or you run your own hypervisor.
It is possible to have the docker commands run in WSL but it will need to configure it to interact with the docker daemon running inside a hypervisor.
Yes, you can use Hyperledger-fabric-composer on windows 10, but as david said in above answer you will not be able to run docker containers directly from Ubuntu sub-system.
To do that you have to do following things:
METHOD:- 1
You will need to install docker CE Client & docker-compose in Ubuntu Subsystem and install Docker(version v17.09) on Windows as well. But those dockers won’t connect together out of the box.
So you need to expose the daemon to port 2375 first by right-clicking the docker icon on task-bar then click setting then check the Expose daemon box.
Now the docker server will be able to connect via Windows network including Ubuntu subsystem. We need to set environment variables in Ubuntu by running below command:
echo "export DOCKER_HOST='tcp://0.0.0.0:2375'" >> ~/.bashrc
source ~/.bashrc
These commands will add DOCKER_HOST to the variables every time we start a new Bash.
METHOD:- 2
If you don't want to use ubuntu sub-system, then you can simply install Git Bash and Docker(version v17.09).
Then install Hyperledger-Fabric by using Git Bash.

Connecting Docker Windows WSL Ubuntu to VMware Ubuntu

I am trying to connect my Windows 10 Home system to be able to run full Linux OS Docker containers. I have installed Docker on both WSL Ubuntu 18.04 as well as a VMware Ubuntu 18.04.
I was trying to follow this guide.
However, I get stuck trying to configure the Daemon as per the instructions.
Can’t use Docker for Windows?
This is only necessary if you are NOT running Docker for Windows!
No problem, just configure your Docker daemon to use -H tcp://0.0.0.0:2375 and --tlsverify=false. Then you can follow along with the rest of this guide exactly.
If you go down this route, I highly recommend rolling your own VM with VMware Player instead of using the Docker Toolbox because VirtualBox has crazy edge case shared folder bugs that will ruin your life at some point. Don’t worry, VMware Player is free. Just Google how to set up Ubuntu 16 server on VMware Player.
When I try to change the Docker Daemon by making a daemon.json file I get errors. I've also tried editing the .profile files and the .bashrc as per other guides (another guide), with no luck.
I am unable to check the DOCKER_HOST variable on the VM Ubuntu.
Don't make things complicated. In your case, why WSL if you just want to connect to a remote daemon? Why not simply use the windows docker client?
Setup you favorite local VM with docker.
Example: I have installed a CentOS distro running on local VMWare Workstation. All Hyper-V of course deinstalled/deactivated.
In this VM, enable tcp access for the daemon.
If you have a systemd linux distro (like mine CentOS), execute this:
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo echo '[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375' >> /etc/systemd/system/docker.service.d/options.conf
sudo systemctl daemon-reload
sudo systemctl restart docker`
Test if the port is open with docker info. You should get an API access warning at the bottom result.
Download the Windows docker cli zip from here: https://download.docker.com/win/static/stable/x86_64/
Move the docker.exe to any folder, for ex. your Documents folder.
Then put this folder path into your Windows PATH variable.
Set the docker host: Open PowerShell, execute setx DOCKER_HOST <VM-IP>:2375 and close it.
Open a new PowerShell and call docker info.
You should see the docker and daemon infos.
Do what ever you like now... :-)

Running MPD server with Mopidy docker container, how do I play on Mac?

I'm running the Docker container stilllman/mopidy. I can access the Mopidy UI at http://localhost:6680 and can also control the music that is playing after installing mpc with homebrew. However, I cannot figure out how to hear the music that is playing. The container acknowledges the new MPD connection when I try to use the mpc command on my local mac terminal. I reach the same situation with Theremin (can control music but not hear).
I was successful by installing pulseaudio via homebrew on my Mac host and connecting my Docker container to it:
docker run -it -e PULSE_SERVER=docker.for.mac.localhost \
-v ~/.config/pulse:/home/pulseaudio/.config/pulse \
--entrypoint speaker-test \
--rm jess/pulseaudio -c 2 -l 1 -t wav
You could build a similar container running Mopidy and change output to pulse server output (should be in the documentation how to do it).
An alternative solution to using the brittle audio routing through Pulseaudio would be to use the Icecast output of Mopidy: https://docs.mopidy.com/en/latest/audio/#streaming-through-icecast
Once you setup the Icecast stream, on your host, you could use a browser or MPV to play the stream generated by Icecast.

How to expose audio from Docker container to a Mac?

I know it's possible by using pulse audio on a Linux host system But paprefs is built for linux not mac.
The Docker-for-Mac VM doesn't have any sound passthrough device, so there isn't anything that you could take advantage of from that angle. By contrast, a virtualbox or vmware fusion VM does have the ability to do passthrough audio.
I was able to get pulseaudio installed and working on OSX with the following command:
brew install pulseaudio
I was able to verify this worked by running the following, hearing sound come out of my speakers:
paplay cockatiel.wav
My next step is to find an image that has a copy of paplay. I found jess/pulseaudio, which appears to be intended to be a pulseaudio server, but I should be able to use it as a client as well.
I found the following guide on the Archlinux Wiki discussing setting up pulseaudio network sound: https://wiki.archlinux.org/index.php/PulseAudio/Examples#PulseAudio_over_network
I was able to adapt it to this situation by doing the following. I edited /usr/local/Cellar/pulseaudio/9.0/etc/pulse/default.pa on my mac, and uncommented the following two lines:
load-module module-esound-protocol-tcp
load-module module-native-protocol-tcp
I reran paplay cockatiel.wav on my mac to make sure my changes still worked. the pulseaudio daemon seems to start on demand, and passes its complaints back to paplay to be printed on my screen if I made a typo. I still have sound with those changes to default.pa, so I'm satisfied that my changes didn't break anything.
Next, I ran the pulseaudio client in a container like this:
docker run --rm -v $HOME:$HOME -w $HOME -it \
-e PULSE_SERVER=192.168.10.23 \
-e HOME=$HOME --entrypoint paplay \
jess/pulseaudio $HOME/cockatiel.wav
What this does is run a container with my local home directory as a volume. This serves two purposes. The first is the fact that my cockatiel.wav is located inside $HOME. The second is because both the client and the server need to have a copy of the same ~/.config/pulse/cookie file (per that archlinux wiki guide).
The PULSE_SERVER environment variable is the en0 IP address of my OSX host, so paplay knows what to connect to.
The HOME environment variable is necessary so paplay can find the same ~/.config/pulse/cookie file.
I was able to play sound from a container running on my docker-for-mac via pulseaudio.
As long as you get the ~/.config/pulse/cookie file to appear in the correct location, you should be able to play sound. You don't have to use a host volume to accomplish this-- you could also do a 'docker cp', or even COPY it into an image.
Install PulseAudio on the Mac:
brew install pulseaudio
Run the daemon:
pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon
In your Docker container:
Install PulseAudio, e.g., apt-get install pulseaudio.
set the following environment variable: ENV PULSE_SERVER=docker.for.mac.localhost
When you run it, share your ~/.config/pulse directory with the container for authentication.
You can run a test to see if it's working like this:
docker run -it -e PULSE_SERVER=docker.for.mac.localhost -v ~/.config/pulse:/home/pulseaudio/.config/pulse --entrypoint speaker-test --rm jess/pulseaudio -c 2 -l 1 -t wav

Resources