Docker使用构build过程中使用的SSH密钥构builddockerfile(用于私人Git仓库)

Docker-Compose v3

我正在努力安装由我的公司制作的简单的GitHub存储库。 我们正在把我们的项目转移到Docker。 所以,我们目前正在使用Docker和Docker-Compose来构build和运行我们的容器。

但是,我们90%的项目使用私人回购,定制gem,我们最终将使用私人容器。

尽pipe通过Habitus将我的SSH密钥移动到容器上,在构build过程中手动复制它们,并试图装入卷,但似乎无法安装我的回购站。 但是我没有做的似乎工作。

例如。 DockerFile

FROM ubuntu # Installing tons of libraries. Not all of these might be needed RUN apt-get -y update && apt-get -y upgrade && apt-get -y install nodejs npm ruby curl openssh-server git php RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # pulling id_rsa from local habitus network (this SUCCEEDS) ARG host RUN wget -O ~/.ssh/id_rsa http://$host:8080/v1/secrets/file/id_rsa && chmod 600 ~/.ssh/id_rsa # checking the ssh files are installed (this FAILS) RUN ssh -vvv -T git@git.my.company.com RUN gem install bundler RUN bundle install 

通过Habitus命令

 sudo habitus --build host=192.168.99.100 --secrets=true 

产量

 OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * Pseudo-terminal will not be allocated because stdin is not a terminal. debug1: Connecting to 192.168.99.100 [192.168.99.100] port 22. debug1: Connection established. debug1: permanently_set_uid: 0/0 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_rsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2 debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000 debug1: Authenticating to git.my.company.com:22 as 'git' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256@libssh.org debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:OkseSPnItLVT0phkACs7TwGA1CZb9nMBSwp5UxdkIf4 debug1: Host 'git.my.company.com' is known and matches the ECDSA host key. debug1: Found key in /root/.ssh/known_hosts:1 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /root/.ssh/id_rsa debug1: Server accepts key: pkalg rsa-sha2-512 blen 535 debug1: read_passphrase: can't open /dev/tty: No such device or address debug1: Trying private key: /root/.ssh/id_dsa debug1: Trying private key: /root/.ssh/id_ecdsa debug1: Trying private key: /root/.ssh/id_ed25519 debug1: No more authentication methods to try. Permission denied (publickey). 

现在,我尝试了很多其他的方法。 每一种方法,我都有同样的问题。 我有时会把我的构buildhibernate几分钟,而我SSH进入服务器,并检查我的〜/ .ssh /文件夹。 每次,所有的文件都匹配我的本地, 成功地捆绑安装gem。 那么,为什么它告诉我“无法打开/ dev / tty:…”

它正在寻找一个密码。 这是因为docker容器作为“根”运行? 我可以绕过这个吗?

你们在构build过程中如何将SSH密钥添加到DockerFile 中,以便可以运行“Bundle install”或“npm install”等命令? 我在互联网上search了几天,尝试了所有我find的解决scheme,但是我最终总是从SSH尝试中获得相同的响应。

对于npm专用模块,您可以查看官方文档。 Docker和私有模块

至于ruby,你可以在这里find类似的解决scheme。

在你想调用ssh的每一行添加标识到ssh代理。

 RUN eval $(ssh-agent);ssh-add;ssh -vvv -T git@git.my.company.com 

就像一个持续碰到Mac OS X的其他人一样,在他们的SSH尝试中导致这个错误:

 debug1: read_passphrase: can't open /dev/tty: No such device or address debug1: Trying private key: /root/.ssh/id_dsa debug1: Trying private key: /root/.ssh/id_ecdsa debug1: Trying private key: /root/.ssh/id_ed25519 debug1: No more authentication methods to try. Permission denied (publickey). 

在进行任何SSH调用之前,您必须包含一个特殊的ENVvariables。 否则,它会一直在寻找密码。 我相信这是以root身份运行SSH命令的一些奇怪的缺省。

 ENV DEBIAN_FRONTEND noninteractive 

在你的命令结束时,你必须运行:

 RUN unset DEBIAN_FRONTEND 

这是我的示例docker文件:

 FROM ubuntu RUN apt-get -y update && apt-get -y upgrade && apt-get -y install nodejs-legacy npm ruby curl openssh-server git php RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer #### THE SETTING OF DEBIAN noninteractive #### ENV DEBIAN_FRONTEND noninteractive RUN gem install bundler RUN mkdir /website WORKDIR /website # I made this temporary SSH key folder in the relative path of my Dockerfile because that is a requirement of DockerFile. However, I plan on using Habitus to safely and conveniently wget my keys from a local server RUN mkdir /root/.ssh COPY dockerkeys /root/.ssh RUN ssh -v -T git@git.my.company.com COPY . . RUN bundle install RUN composer install RUN npm install #### UNSETTING DEBIAN ENV #### RUN unset DEBIAN_FRONTEND RUN bundle install RUN composer install RUN npm install CMD [ "npm", "start" ] 

我还没有用其他图片进行testing,但是原理是一样的。 看起来你需要明确地设置一个非交互式shell。 否则,每次都会提示您inputSSH密码。

尝试使用OnVault秘密存储在Docker镜像构build中使用。 它能够在运行时绑定ssh密钥,恰好在所需的构build步骤中,并在完成之后立即将其删除(在将图层提交到图像之前)。 这不会在docker映像中留下任何ssh密钥的痕迹,从而增强安全性。

一些优点:

  • 消除了添加和删除SSH密钥的需求
  • 使用dockerfile减less复杂性
  • 由于它绑定了整个.ssh文件夹,因此可以在本地configuration多个GitHub帐户。
  • 它是一个二进制文件,可以与关键字ONVAULT一起使用。 例如: RUN ONVAULT bundle install

注意:它的作品对我来说是awsome .. !!,试试看吧;-)