【问题标题】:Build with docker and --privileged使用 docker 和 --privileged 构建
【发布时间】:2018-06-14 09:13:50
【问题描述】:

我正在使用this guide 使用 Amazon Lex 和 Raspberry Pi 构建语音工具包,但我需要使用 Docker。 问题是指南卷曲和运行的脚本需要访问 /dev/tty。我可以在 running docker 容器时授予对 /dev/tty 的访问权限,但在构建容器时我不知道该怎么做。

我的 Dockerfile 看起来像这样:

FROM resin/rpi-raspbian

WORKDIR /app

ADD . /app

#The script requires these
RUN apt-get update
RUN apt-get install iputils-ping

#The script has to be run with sudo priviliges but not as root
USER root
ADD /sudoers.txt /etc/sudoers
RUN chmod 440 /etc/sudoers


RUN useradd -ms /bin/bash lex
RUN echo 'lex:test' | chpasswd

RUN curl https://get.pimoroni.com/phatdac | bash 

USER lex

EXPOSE 80

#Comment the last RUN command and uncomment this
#CMD curl https://get.pimoroni.com/phatdac | bash 

当我尝试用

构建容器时
docker build -t raspi1 .

它在脚本上崩溃,因为它无法访问 /dev/tty。

运行容器时,我可以使用此脚本授予对 /dev/tty 和 /dev/snd 的访问权限

#!/bin/sh

 docker run -ti --rm \
     -v /dev/snd:/dev/snd \
      --privileged \
     raspi7 

然后尝试在启动时使用 Dockerfile 中的 CMD 脚本。但是如果我这样做了,那么我每次运行时都需要使用脚本,并且在脚本完成后我还需要在其他东西上运行,这在构建时在 Dockerfile 上会很好。

TLDR; 构建 docker 镜像时如何授予 /dev/tty 和 /dev/snd 权限?

【问题讨论】:

标签: linux amazon-web-services docker dockerfile amazon-lex


【解决方案1】:

Docker 目前不支持暴露设备,或者构建时的特权操作。

根据@cpuguy83,您现在正在做的事情——在不访问主机的情况下构建可移植映像并在容器首次启动时完成配置——是正确的做法:

在第一次容器启动时做这种事情是正确的方法。 这是一个运行时配置,它不应该出现在图像中。

bountysource

还有一个老的但仍然打开moby的问题。

【讨论】:

    【解决方案2】:

    您的问题可能是 /dev/snd 在您的 docker 映像中不存在。当你运行你的容器时,你实际上是在容器内挂载你的主机操作系统 /dev/snd 以便你的脚本可以运行。看看以下内容:

    [INSERT] > cat Dockerfile
    FROM resin/rpi-raspbian
    
    RUN ls /dev && ls /dev/tty && ls /dev/snd
    
    
    [INSERT] > docker build .
    Sending build context to Docker daemon  39.94kB
    Step 1/2 : FROM resin/rpi-raspbian
     ---> d008ca006edc
    Step 2/2 : RUN ls /dev && ls /dev/tty && ls /dev/snd
     ---> Running in 0b738007c71c
    core
    fd
    full
    mqueue
    null
    ptmx
    pts
    random
    shm
    stderr
    stdin
    stdout
    tty
    urandom
    zero
    /dev/tty
    /bin/ls: cannot access /dev/snd: No such file or directory
    The command '/bin/sh -c ls /dev && ls /dev/tty && ls /dev/snd' returned a non-zero code: 2
    

    如您所见,/dev/tty 存在,您可以访问它。 /dev/snd 不存在,并且您不在正在运行的容器中,因此您无法将其安装为卷(您在运行容器时正在执行此操作)。我建议您尝试更全面地了解您正在运行的脚本正在做什么,评估它是否需要访问主机的 /dev/snd,如果需要,您只能在正在运行的容器中运行脚本,因为图像不需要对主机有任何概念。

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2021-02-27
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      相关资源
      最近更新 更多