在 docker 容器中使用音频运行应用程序 [英] run apps using audio in a docker container

查看:65
本文介绍了在 docker 容器中使用音频运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的灵感来自你能在 docker 容器中运行 GUI 应用吗?.

基本思想是运行带有音频和用户界面的应用程序(vlc、firefox、skype...)

The basic idea is to run apps with audio and ui (vlc, firefox, skype, ...)

我正在使用pulseaudio搜索docker容器,但我发现所有容器都在tcp上使用pulseaudio流.(应用程序的安全沙箱)

I was searching for docker containers using pulseaudio but all containers I found where using pulseaudio streaming over tcp. (security sandboxing of the applications)

在我的情况下,我更喜欢将容器内的应用程序中的音频直接播放到我的主机pulseaudio.(没有 ssh 隧道和臃肿的 docker 镜像)

In my case I would prefere playing audio from an app inside the container directly to my host pulseaudio. (without ssh tunneling and bloated docker images)

Pulseaudio 因为我的 qt 应用正在使用它;)

Pulseaudio because my qt app is using it ;)

推荐答案

我花了一些时间才找到需要什么.(Ubuntu)

it took me some time until i found out what is needed. (Ubuntu)

我们从 docker run 命令开始 docker run -ti --rm myContainer sh -c echo run something"

we start with the docker run command docker run -ti --rm myContainer sh -c "echo run something"

ALSA:
我们需要 /dev/snd 和一些硬件访问,就像它看起来的那样.当我们把它们放在一起时,我们有

ALSA:
we need /dev/snd and some hardware access as it looks like. when we put this together we have

docker run -ti --rm 
    -v /dev/snd:/dev/snd 
    --lxc-conf='lxc.cgroup.devices.allow = c 116:* rwm' 
    myContainer sh -c "echo run something"`

在没有 lxc 标志的新 docker 版本中,你应该使用这个:

In new docker versions without lxc flags you shoud use this:

docker run -ti --rm 
    -v /dev/snd:/dev/snd 
     --privileged 
    myContainer sh -c "echo run something"`

脉冲音频:
更新:使用 -v 选项在容器内安装pulseaudio套接字可能就足够了.这取决于您的版本和首选访问方法.请参阅套接字方法的其他答案.

PULSEAUDIO:
update: it may be enought to mount the pulseaudio socket within the container using -v option. this depends on your version and prefered access method. see other answers for the socket method.

这里我们基本上需要/dev/shm/etc/machine-id/run/user/$uid/pulse.但这还不是全部(也许是因为 Ubuntu 以及他们过去是如何做到的).环境变量 XDG_RUNTIME_DIR 在主机系统和 docker 容器中必须相同.您可能还需要 /var/lib/dbus 因为某些应用程序正在从这里访问机器 ID(可能只包含指向真实"机器 ID 的符号链接).至少你可能需要隐藏的主文件夹 ~/.pulse 来获取一些临时数据(我不确定).

Here we need basically /dev/shm, /etc/machine-id and /run/user/$uid/pulse. But that is not all (maybe because of Ubuntu and how they did it in the past). The envirorment variable XDG_RUNTIME_DIR has to be the same in the host system and in your docker container. You may also need /var/lib/dbus because some apps are accessing the machine id from here (may only containing a symbolic link to the 'real' machine id). And at least you may need the hidden home folder ~/.pulse for some temp data (i am not sure about this).

docker run -ti --rm 
    -v /dev/shm:/dev/shm 
    -v /etc/machine-id:/etc/machine-id 
    -v /run/user/$uid/pulse:/run/user/$uid/pulse 
    -v /var/lib/dbus:/var/lib/dbus 
    -v ~/.pulse:/home/$dockerUsername/.pulse 
    myContainer sh -c "echo run something"

在新的 docker 版本中,您可能需要添加 --privileged.
当然,您可以将两者结合在一起并与 xServer ui 转发一起使用,如下所示:https://stackoverflow.com/a/28971413/2835523

In new docker versions you might need to add --privileged.
Of course you can combine both together and use it together with xServer ui forwarding like here: https://stackoverflow.com/a/28971413/2835523

顺便提一下:

  • 您可以在 dockerfile
  • 中处理大部分(所有都没有使用的 id)
  • 使用 uid=$(id -u) 使用 id -g
  • 获取用户 ID 和 gid
  • 使用此 ID 创建 docker 用户

创建用户脚本:

mkdir -p /home/$dockerUsername && 
echo "$dockerUsername:x:${uid}:${gid}:$dockerUsername,,,:/home/$dockerUsername:/bin/bash" >> /etc/passwd && 
echo "$dockerUsername:x:${uid}:" >> /etc/group && 
mkdir /etc/sudoers.d && 
echo "$dockerUsername ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$dockerUsername && 
chmod 0440 /etc/sudoers.d/$dockerUsername && 
chown ${uid}:${gid} -R /home/$dockerUsername

这篇关于在 docker 容器中使用音频运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆