【问题标题】:Sending SSH key into docker using env variables not working使用 env 变量将 SSH 密钥发送到 docker 不起作用
【发布时间】:2019-09-23 14:13:11
【问题描述】:

我正在尝试在我的 docker 文件中安装一些私有 gem(bitbucket git repo)并且 SSH 权限被拒绝

我所做的是:

  1. 将我的私钥设置为“ssh_key”环境变量
  2. 将“ssh_config”环境变量设置为“IdentityFile /.ssh/id_rsa Host User”
  3. 在 docker-compose 中:
    args:
            ssh_key: ${ssh_key}
            ssh_config: ${ssh_config}

  1. 在 Dockerfile 中:
    ARG ssh_key
    ARG ssh_config
    RUN mkdir /.sshRUN echo "${ssh_key}" > /.ssh/id_rsa
    RUN echo "${ssh_config}" > /etc/ssh/ssh_config
    RUN chmod 600 /.ssh/id_rsa
    bundle install

我得到了错误

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

但是当我注释掉私有 gem 并构建镜像时,ssh 进入容器,删除 id_rsa 文件并使用文本编辑器手动粘贴主机私钥并运行 chmod 600(或者只是挂载我的主机 ~/.ssh dir ),然后就可以安装私有 gem 而不会出现任何错误。

我不知道究竟是什么导致了错误。有没有更简单的方法来实现这一点?

谢谢

【问题讨论】:

    标签: docker ssh docker-compose dockerfile ssh-keys


    【解决方案1】:

    如果您在运行docker build 之前在主机上运行bundle package,您将拥有安装应用程序所需的所有gem 文件的本地副本。然后bundle install--local 选项将使用本地目录而不是尝试远程获取文件。您的 Dockerfile 可能部分看起来像

    COPY Gemfile Gemfile.lock .
    COPY vendor/cache vendor/cache
    RUN bundle install --frozen --local --system
    

    这避免了您尝试使用 ssh 密钥进行相当危险的操作:拥有该映像的任何人都可以查看其 docker history 或只运行容器并轻松获取您的 ssh 私钥。

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      相关资源
      最近更新 更多