Docker + Nodejs + 私有仓库 + 私有 NPM 模块 - 访问问题

8

我正在设置使用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到这个仓库时,会输出以下日志:
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克隆运行良好,因此其任何私有NPM依赖项都应该可以顺利安装。
我相当确定我的SSH设置存在缺陷(并且在git克隆私有父级存储库时没有表现出来),因为我将用户名和密码添加到了链接中。但是,我不确定如何正确地执行此操作,并希望得到一些指导。

1
首先,您需要安装openssh和ca-certificate软件包。此外,尝试以交互方式运行容器,并逐步执行所有操作以解决这些问题。docker run -it <container> /bin/bash - user2105103
2个回答

5

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

这段代码可以正常运行,因为你传递了usernamepassword并使用了https协议。

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

由于您直接通过ssh连接,而Docker不会从主机机器进行任何ssh代理转发,因此失败了。
不幸的是,看起来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 TOKEN  

然后在脚本中使用npm install,在依赖项中使用该TOKEN。
"privModule": "git+https://${TOKEN}:x-oauth-basic@github.com/<githubID>/<privateModule>.git"

如果那样不起作用,可以使用sed在package.json文件中替换该变量,或者让npm使用环境变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接