在具有私有存储库依赖项的 docker 容器内使用 Bower

Kat*_*son 7 git ssh bower docker

我正在尝试在 docker 容器内运行bower install,作为命令传递docker-compose.yml

相关代码在docker-compose.yml

services:
  assets:
    build: ./src
    command: >
      sh -c '
      bower install --allow-root;
      '
Run Code Online (Sandbox Code Playgroud)

具有bower.json以下依赖性:

{
  "name": "projectname",
  "version": "version",
  "dependencies": {
    "remote-repo": "ssh://git@remoterepo.url/repo.git#branch"
  }
}
Run Code Online (Sandbox Code Playgroud)

这个远程仓库是私有的。主机具有可以从该远程提取的正确 SSH 凭据。

我尝试过 4 或 5 种不同的方式将 SSH 凭据从我的主机传递到 docker 容器,但每次尝试都会出现相同的错误消息:

docker_1   | bower repo#branch          ECMDERR Failed to execute "git 
ls-remote --tags --heads ssh://git@remoterepo.url/repo.git", exit code 
of #128 Host key verification failed. fatal: Could not read from 
remote repository.  Please make sure you have the correct access 
rights and the repository exists.
Run Code Online (Sandbox Code Playgroud)

当我exec直接进入容器并尝试 git 克隆时,它会询问我是否确定要将远程添加到known_hosts,然后它会询问我的 ssh 密钥的密码(正如第一次尝试时所预期的那样)连接到远程)。

我已按照此 stackoverflow 响应中的步骤尝试绕过提示:/sf/answers/1638781301/

甚至通过 ssh 将所有内容都扔给它,在 RUN 命令下将这些步骤添加到我的 Dockerfile 中:https://serverfault.com/questions/132970/can-i-automatically-add-a-new -主机到已知主机/316100#316100

就目前而言,我的安装脚本(运行docker-compose up等)包括这一行:

cp $HOME/.ssh/id_rsa src/id_rsa
Run Code Online (Sandbox Code Playgroud)

我已经确认 id_rsa 已正确复制到 Dockerfile 所在的目录中(具体来说,src在我的应用程序内)

我的 Dockerfile 包含以下内容:

# Make ssh dir
RUN mkdir /root/.ssh/

# Copy over private key, and set permissions
ADD id_rsa /root/.ssh/id_rsa

# Create known_hosts
RUN touch /root/.ssh/known_hosts

# Add remote's key
RUN ssh-keygen -R remoterepo.url
RUN ssh-keygen -R remoterepoIP
RUN ssh-keygen -R remoterepo.url,remoterepoIP
RUN ssh-keyscan -H remoterepo.url,remoterepoIP >> /root/.ssh/known_hosts
RUN ssh-keyscan -H remoterepoIP >> /root/.ssh/known_hosts
RUN ssh-keyscan -H remoterepo.url >> /root/.ssh/known_hosts
Run Code Online (Sandbox Code Playgroud)

有没有办法让 Bower 进入 docker 容器来访问私有远程仓库?我觉得我已经尝试了一切(而且我整个星期都在尝试不同的事情)。

Kat*_*son 0

此配置似乎有效的唯一方法是:

1) 在私有仓库中为docker容器创建只读密钥
2) 将只读凭证和known_hosts放入Dockerfile目录下的仓库中
3) 调整id_rsa文件的权限

我尝试调整从主机复制的凭据的权限,但这仍然给出权限被拒绝错误。解决这个问题的唯一方法是在我们的私有存储库中为此容器创建一个特殊的只读密钥。

这并没有真正解决最初的问题,但它让我们到达了我们需要的地方。我们的实现是.gitignore凭证并以另一种方式将它们传递给用户,这可以防止秘密存储在存储库中。

我从安装脚本中取出了复制命令,因此最终结果如下Dockerfile

# Make ssh dir
RUN mkdir /root/.ssh/

# Copy over private key, and set permissions
COPY id_rsa /root/.ssh/id_rsa
COPY known_hosts /root/.ssh/known_hosts
RUN chmod 600 /root/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)

并且私钥与 位于同一目录中Dockerfile

我怀疑问题仍然在于需要输入密码 - 2014 年之前的 StackOverflow 文章中有一条小评论说:显然无法使用受密码保护的密码

希望这对某人有帮助。