How to expose audio from Docker container to a Mac

2019-02-06 10:18发布

问题:

I know it's possible by using pulse audio on a Linux host system But paprefs is built for linux not mac.

回答1:

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.



回答2:

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:

  1. Install PulseAudio, e.g., apt-get install pulseaudio.
  2. 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