【问题标题】:How to send a password to `sudo` from a Dockerfile如何从 Dockerfile 向 `sudo` 发送密码
【发布时间】:2017-06-19 12:02:58
【问题描述】:

我正在为本地开发需求创建一个 docker 文件。该文件创建一个user 帐户,密码为user。我认为应该起作用的行是:

# allow writes to the home directory
RUN echo "user" | sudo -S chmod 777 ~

但是,当我以交互方式运行图像时,它似乎失败了,我看到了这条消息:

mkdir: cannot create directory ‘/home/.meteor-install-tmp’: Permission 
denied

当我在容器中运行 sudo -S chmod 777 ~ 时,它可以工作。

这是完整的脚本:

# docker build -t timebandit/meteor-1-5 --rm .
# docker run -v /host/path:/home/code -it timebandit/meteor-1-5 bash

FROM ubuntu:xenial

# update the system
RUN apt-get update && apt-get -y install curl \
sudo \
apt-utils \
locales \
nano

# Set the locale
RUN sudo sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' 
/etc/locale.gen && \
locale-gen
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8 

# set the root password
RUN echo "root:root" | chpasswd


# create a user
RUN useradd -ms /bin/bash user
RUN adduser user sudo
RUN echo 'user:user' | chpasswd

ENV HOME=/home
WORKDIR $HOME/user

USER user
# allow writes to the home directory
ARG user_pass
RUN echo $user_pass | sudo --stdin chmod 777 /home

# install meteor
RUN echo $user_pass | sudo curl https://install.meteor.com/ | sh

【问题讨论】:

  • 通常在docker的镜像中sudo是没有安装的。你试过没有 sudo 吗?
  • @German,我在 Dockerfile 上安装了 sudo

标签: ubuntu docker dockerfile


【解决方案1】:

我建议您完全跳过 sudo,因为您可以使用 Dockerfile 更改用户:

....
# allow writes to the home directory
USER root
RUN chmod 777 /home
USER user
....

将 sudo 添加到您的图像意味着攻击者可以使用它。您可以在需要以 root 身份返回容器时随时使用 -u root 选项更改 docker rundocker exec 命令的用户。

【讨论】:

  • 不错的答案。而且您根本不需要操纵密码
  • 它取得了一些进展,但“访问”问题出现在其他地方。现在我看到Retrying after error { [Error: SQLITE_CANTOPEN: unable to open database file] errno: 14, code: 'SQLITE_CANTOPEN' } 错误。因此,脚本的当前状态是:pastebin.com/RDMK546U
  • 我无法构建您的 Dockerfile,但由于完全不同的流星错误。我对流星不熟悉,所以最好将其作为一个其他人可以看到的新问题提出。
【解决方案2】:

~ 是用户的主目录,而不是/home 目录本身。

/home 也一样:

RUN echo "user" | sudo -S chmod 777 /home

让我告诉你,你有两件事通常是不鼓励的(Dockerfile 中的密码和 777 perms)。

正如@meatspace 建议的那样,您可以使用 docker build args:

ARG user_pass
RUN echo $user_pass | sudo -S chmod 777 /home

然后用这个构建:

docker build --build-arg user_pass=user (and the rest of the command)

【讨论】:

  • 尤其是现在 build-arg 存在 (docs.docker.com/engine/reference/commandline/build),你真的没有理由将秘密放入 Dockerfile。
  • 这似乎是更优雅的解决方案@BMitch 的解决方案正在制造更多问题,尽管它可能更像是我的脚本而不是他的想法
  • 我不相信这是有效的,因为当文件构建时,从命令行传入密码的点出现在构建进度输出中 red
  • 还有其他人在尝试使用 echo 将密码传递到 sudo 时遇到问题吗?出于某种奇怪的原因,它对我不起作用。
猜你喜欢
  • 2019-03-24
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 2011-09-01
  • 2016-05-18
  • 2020-03-19
  • 1970-01-01
相关资源
最近更新 更多