How to access mac os x microphone inside docker container?
Asked Answered
P

1

10

I am running a docker container with the python code that records the sound for few seconds using the host MacBook Pro build in microphone.

Part of my challenge is to determine how to provide access/share Macbook Pro microphone inside the Docker container. Most of the existing questions on stack overflow or elsewhere provide solutions for Linux based distros but I don't have much luck to do the same with mac os.

The OS version details:

System Version: macOS 10.14.2 (18C54)
Kernel Version: Darwin 18.2.0

Docker:

Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        6247962
 Built:             Sun Feb 10 04:12:39 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       6247962
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     false

The python code is simple and working as expected on the host.

import sounddevice as sd
import numpy as np
fs = 48000
duration=5
rec = sd.rec(int(duration * fs), samplerate=fs, channels=1, blocking=True)
print(rec)

Here is the docker file:

FROM python:3

RUN apt-get -y update
RUN apt-get -y install libasound-dev
RUN apt-get -y install portaudio19-dev

WORKDIR /usr/src/app
COPY requirements.txt ./
COPY AudioRecordingTest.py ./
RUN pip install -r requirements.txt
CMD [ "python", "./AudioRecordingTest.py" ]
docker build -t audiorecording:1.0 .

docker run -it --rm --device --privileged=true DONT_KNOW_ABOUT_THIS_PART:/dev/snd  audiorecording:1.0 

Ideally should able to mount/share the microphone as you do when Linux is the host. If not what are the other possible solutions?

docker run -it --rm --device --privileged=true DONT_KNOW_ABOUT_THIS_PART:/dev/snd  audiorecording:1.0 
Platitudinize answered 15/2, 2019 at 3:8 Comment(0)
F
1

Update Aug 2024


Try running the pulseaudio server on the host and connect to it from the docker.

You will need to install it with brew install pulseaudio.

Update the configuration file /usr/local/Cellar/pulseaudio/17.0/etc/pulse/default.pa. You will need to uncomment and change the following lines:

### Network access (may be configured with paprefs, so leave this commented
### here if you plan to use paprefs)
load-module module-esound-protocol-tcp auth-anonymous=1 
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.0.0/16

Start the server with brew services start pulseaudio

Now build the docker with pulseaudio support:

RUN apt-get update && apt-get install -y \
    pulseaudio \
    portaudio19-dev \        
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* 

You will need to authorize Docker container to work with the pulseaudio server by copying cookie into the docker (or sharing it with -v option):

docker run -it \  
           -e PULSE_SERVER=host.docker.internal \
           -v ~/.config/pulse:/home/pulseaudio/.config/pulse \
  ... + other options you need

You should be able to test the connection from the docker to host with pactl info.

P.S. The "--device" flag is not relevant anymore.

Tested on Sonoma 14.5

Ferdelance answered 1/11, 2020 at 19:31 Comment(6)
How can I get ~/.config/pulse folder? I have already install pulseaudio using homebrew but that directory doesn't exists.Precocity
You need not only install but also run pulseaudio server ;) Do it either by brew services start pulseaudio (and relogin) or pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon. After you start the server config directory would be created automatically.Ferdelance
Please note that latest docker changed docker.for.mac.localhost to host.docker.internalFerdelance
I've faced some issue here when trying to run pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon. I was downloading pulseaudio via homebrew and run exactly what you told but there's error: ` dyld: Library not loaded: /usr/local/opt/gdbm/lib/libgdbm.6.dylib Referenced from: /usr/local/bin/pulseaudio Reason: image not found [1] 13266 abort pulseaudio `Precocity
Update, so i have try your method on my another computer. Pulseaudio seems to work well. However, the docker run command fails when adding device giving the following error: docker: Error response from daemon: error gathering device information while adding custom device "/dev/snd": no such file or directory.Precocity
@ChompakornCChaichot I know this is slightly older question but did you ever solve this? I'm also having the same issue "/dev/snd": no such file or directoryRiane

© 2022 - 2024 — McMap. All rights reserved.