【问题标题】:SSH-forwarding during build only works for root构建期间的 SSH 转发仅适用于 root
【发布时间】:2020-07-12 12:33:00
【问题描述】:

这是一个有效的 Dockerfile:

# syntax=docker/dockerfile:1.0.0-experimental
FROM debian:buster-slim as base

# setup APT operation for noninteractive use
# This avoids a bunch of warnings like
# "debconf: unable to initialize frontend: Dialog"
ENV DEBIAN_FRONTEND=noninteractive

# install requirements
RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y --no-install-recommends \
        git \
        openssh-client

# add a user
# RUN adduser --disabled-password app-user
# WORKDIR /home/app-user
# USER app-user

RUN mkdir --mode=0700 ~/.ssh
RUN printf "Host <bitbucket host>\n  StrictHostKeyChecking no\n  CheckHostIP no\n" >> ~/.ssh/config
RUN chmod 600 ~/.ssh/config
RUN --mount=type=ssh ssh-keyscan -t rsa <bitbucket host> >> ~/.ssh/known_hosts
RUN chmod 600 ~/.ssh/known_hosts

RUN --mount=type=ssh git clone --no-checkout 'ssh://git@<bitbucket host>/my/project.git'

我唯一删除的是实际的 bitbucket 主机。现在,不起作用的是激活“添加用户”评论后的三个逗号。如果激活了这三个命令,则构建失败并显示:

#20 [13/13] RUN --mount=type=ssh git clone --no-checkout 'ssh://git@bitbucke...
#20       digest: sha256:2ca1...
#20         name: "[13/13] RUN --mount=type=ssh git clone --no-checkout 'ssh://git@<bitbucket host>/my/project.git'"
#20      started: 2020-03-31 20:12:44.957895838 +0000 UTC
#20 0.648 Cloning into 'project'...
#20 1.170 git@<bitbucket host>: Permission denied (publickey).
#20 1.171 fatal: Could not read from remote repository.
#20 1.171 
#20 1.171 Please make sure you have the correct access rights
#20 1.171 and the repository exists.
#20    completed: 2020-03-31 20:12:46.235264455 +0000 UTC
#20     duration: 1.277368617s
#20        error: "executor failed running [/bin/sh -c git clone --no-checkout 'ssh://git@<bitbucket host>/my/project.git']: exit code: 128"

rpc error: code = Unknown desc = executor failed running [/bin/sh -c git clone --no-checkout 'ssh://git@<bitbucket host>/my/project.git']: exit code: 128

这是 Docker 中的错误吗?我是否错过了这不应该以某种方式工作的暗示?我是否需要在 root 帐户和新用户帐户之间设置额外的转发级别? git/ssh 首先如何建立与代理的通信?我检查了/tmp/run、安装和环境,但找不到管道/套接字。

显而易见的解决方法是以 root 身份克隆,然后在其上运行 chown -R,但这似乎非常不雅。另外,我显然想了解发生了什么。

【问题讨论】:

  • 添加子选项--mount=type=ssh,uid=1000(匹配app-user 的数字用户ID)是否有效?看起来像 the uid defaults to 0 这会使非 root 用户无法访问转发的代理套接字。
  • 我相信这就是答案,@DavidMaze。但是,我宁愿使用mode 子选项。不过,我必须从 18.09 升级,因为该版本不理解这两个变体中的任何一个。

标签: git docker ssh bitbucket


【解决方案1】:

Buildkit --mount=type=ssh 指令将主机的 ssh 代理套接字重新挂载到具有特定权限的容器中。默认值归 root (uid=0, gid=0) 所有,其他任何用户都无法读取 (mode=0600)。

您可以添加一个选项,使套接字由某个已知的非 root 用户拥有:

# When you create the user specify its uid
RUN adduser --disabled-password --uid 999 app-user
USER app-user

# Also specify the uid as a mount option
RUN --mount=type=ssh,uid=999 ssh-keyscan ...

您还可以使其他用户可以访问套接字(这不是一个很大的安全问题,因为它只能在单个 RUN 命令的生命周期内访问,并且您可以控制它正在运行的内容)

RUN --mount=type=ssh,mode=0666 ssh-keyscan ...

【讨论】:

  • 你能不能给我指出解释关于添加modeuid 选项的部分的文档?我一直在寻找这个解决方案,你的答案是我找到的唯一线索。
  • 我更正了答案第一行的链接。这记录了其他选项,但您需要查看其他一些示例才能了解语法。
猜你喜欢
  • 2016-05-21
  • 2021-11-14
  • 1970-01-01
  • 2015-09-03
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多