【问题标题】:Docker + Nodejs + Private Repo + Private NPM Module - Access ProblemsDocker + Nodejs + Private Repo + Private NPM Module - 访问问题
【发布时间】:2015-01-04 07:54:46
【问题描述】:

我正在使用 Docker 设置 Node.js 服务的部署。

我拥有的 Dockerfile 是从网上的各种示例拼凑而成的。 Dockerfile 的目录包括:

  • Dockerfile
  • id_rsa
  • start.sh

这是 Dockerfile:

FROM ubuntu:13.10

# make sure apt is up to date
RUN apt-get update

# install npm, git, ssh, curl
RUN apt-get install -y npm git git-core ssh curl

RUN mkdir /nodejs && curl http://nodejs.org/dist/v0.10.31/node-v0.10.31-linux-x64.tar.gz | tar xvzf - -C /nodejs --strip-components=1

# Fixes empty home
ENV PATH $PATH:/nodejs/bin

ENV HOME /root

# SSH SETUP
RUN mkdir -p /root/.ssh
ADD id_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "IdentityFile /root/.ssh/id_rsa" >> /root/.ssh/ssh_config
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts

ADD start.sh /tmp/

RUN chmod +x /tmp/start.sh

CMD ./tmp/start.sh

设置完成后,start.sh 运行,我遇到了私有 Node.js 服务所具有的私有 NPM 依赖项的问题。这就是 start.sh 正在做的事情:

cd /tmp

# try to remove the repo if it already exists
rm -rf MediaFX; true

git clone https://<username>:<password>@github.com/company/ExampleRepo.git

cd RepoName

node --version

ls

npm install

NODE_ENV=test DEBUG=* PORT=3000 node server.js

在 ExampleRepo 的 package.json 中,我们导入了一个私有模块,如下所示:

"dependencies": {
    "scribe": "git+ssh://git@github.com:Company/PrivateDep.git"
},

当 npm install 到达这个 repo 时,它会输出这些日志:

npm ERR! git clone git@github.com:InboxAppCo/scribe.git Cloning into bare repository '/root/.npm/_git-remotes/git-github-com-InboxAppCo-scribe-git-abae334a'...
npm ERR! git clone git@github.com:InboxAppCo/scribe.git
npm ERR! git clone git@github.com:InboxAppCo/scribe.git Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
npm ERR! git clone git@github.com:InboxAppCo/scribe.git Permission denied (publickey).
npm ERR! git clone git@github.com:InboxAppCo/scribe.git fatal: Could not read from remote repository.
npm ERR! git clone git@github.com:InboxAppCo/scribe.git
npm ERR! git clone git@github.com:InboxAppCo/scribe.git Please make sure you have the correct access rights
npm ERR! git clone git@github.com:InboxAppCo/scribe.git and the repository exists.
npm ERR! Error: `git "clone" "--mirror" "git@github.com:InboxAppCo/scribe.git" "/root/.npm/_git-remotes/git-github-com-InboxAppCo-scribe-git-abae334a"` failed with 128
npm ERR!     at ChildProcess.cpclosed (/usr/share/npm/lib/utils/exec.js:59:20)
npm ERR!     at ChildProcess.EventEmitter.emit (events.js:98:17)
npm ERR!     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR!     reportbug --attach /tmp/MediaFX/npm-debug.log npm

npm ERR! System Linux 3.16.4-tinycore64
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /tmp/MediaFX
npm ERR! node -v v0.10.15
npm ERR! npm -v 1.2.18

我认为由于私有 Node 服务的 git clone 工作正常,它的任何私有 NPM 依赖项都可以顺利安装。

我相当肯定我的 SSH 设置存在缺陷(并且在 git 克隆私人父母仓库时它没有表现出来),因为我在链接中添加了用户名和密码。不过,我不确定,希望能得到一些有关如何正确执行此操作的指导。

【问题讨论】:

  • 一方面,您需要 openssh 和 ca-certificate 包。此外,尝试以交互方式运行您的容器并逐步解决所有问题以解决这些问题。 docker run -it /bin/bash

标签: node.js ssh npm docker boot2docker


【解决方案1】:

git clone https://&lt;username&gt;:&lt;password&gt;@github.com/company/ExampleRepo.git

有效,因为您正在传递 username 和 password 并通过 https 完成它

"dependencies": {
    "scribe": "git+ssh://git@github.com:Company/PrivateDep.git"
},

失败,因为您直接通过 ssh 连接,而 Docker 不会从主机进行任何 ssh agent 转发。

不幸的是,npm 似乎不支持任何 url 格式来发送用户名和密码,例如您的克隆行:https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

您必须将 ssh 密钥添加到 docker 容器(不推荐)

或者做一些时髦的事情,比如从主机分享你的 SSH_SOCKET,比如:

https://gist.github.com/d11wtq/8699521

【讨论】:

    【解决方案2】:

    这是我今晚要尝试实施的一种方法:

    docker create --build-arg TOKEN <my priv token> <dockerFile>
    

    也许在 docker 文件中声明 arg?

    ARG TOKEN  
    

    然后在脚本中让 npm install 在依赖项中使用该 TOKEN

    "privModule": "git+https://${TOKEN}:x-oauth-basic@github.com/<githubID>/<privateModule>.git"
    

    如果这不起作用,请以某种方式替换 package.json 中的该 var(使用 sed)或让 npm 使用环境变量。

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 2018-07-27
      • 2021-02-07
      • 2011-06-01
      • 2023-03-23
      • 2017-07-13
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多