将SSH代理与Docker组成和Dockerfile一起使用
在我的一个Nestjs应用程序中,我使用私人GitHub存储库遇到问题。当我使用Docker build
命令创建Docker Image时,该图像将成功创建并且一切正常。但是,我无法将Dockerfile与Docker-Compose
一起使用。
这是dockerfile
的一部分,其中我使用buildKit
登录功能:
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh npm install
使用dockerfile
单独构建图像时,我通过- SSH默认
参数,像这样,它成功安装了私人仓库:
docker build --ssh default -t CONTAINER_NAME .
跟随 “ noreferrer”>“ noreferrer”>本文,在docker-compose.yml
文件中,我包含了$ ssh_auth_sock
这样:
environment:
- NODE_ENV:${NODE_ENV}
- SSH_AUTH_SOCK:${SSH_AUTH_SOCK}
volumes:
- $SSH_AUTH_SOCK:${SSH_AUTH_SOCK}
但是,每当我尝试运行docker-compose
#11 44.97 npm ERR! code 128
#11 44.97 npm ERR! An unknown git error occurred
#11 44.97 npm ERR! command git --no-replace-objects ls-remote ssh://[email protected]/organization/repo.git
#11 44.97 npm ERR! [email protected]: Permission denied (publickey).
#11 44.97 npm ERR! fatal: Could not read from remote repository.
#11 44.97 npm ERR!
#11 44.97 npm ERR! Please make sure you have the correct access rights
#11 44.97 npm ERR! and the repository exists.
知道我在做什么错吗?
I am having issues using a private github repo in one of my NestJS apps. When I create the docker image using the docker build
command, the image is successfully created and everything works fine. However I can't use the Dockerfile with docker-compose
.
Here's the part of Dockerfile
where I use the BuildKit
mount feature:
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh npm install
When building the image with Dockerfile
alone I pass the --ssh default
argument, like this and it successfully installs the private repo:
docker build --ssh default -t CONTAINER_NAME .
Following this article, inside the docker-compose.yml
file I have included the $SSH_AUTH_SOCK
like this:
environment:
- NODE_ENV:${NODE_ENV}
- SSH_AUTH_SOCK:${SSH_AUTH_SOCK}
volumes:
- $SSH_AUTH_SOCK:${SSH_AUTH_SOCK}
However I get this error whenever I try to run docker-compose up
#11 44.97 npm ERR! code 128
#11 44.97 npm ERR! An unknown git error occurred
#11 44.97 npm ERR! command git --no-replace-objects ls-remote ssh://[email protected]/organization/repo.git
#11 44.97 npm ERR! [email protected]: Permission denied (publickey).
#11 44.97 npm ERR! fatal: Could not read from remote repository.
#11 44.97 npm ERR!
#11 44.97 npm ERR! Please make sure you have the correct access rights
#11 44.97 npm ERR! and the repository exists.
Any idea what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们已经在组合中添加了SSH标志作为选项: https:// https:// github.com/compose-pec/compose-pec/pull/234
They have added the ssh flag as option to the build key in compose: https://github.com/compose-spec/compose-spec/pull/234
您的
环境
语法不正确。环境
块可以是name = value
Pairs的列表:或者它可以是字典:
您的都不是这些东西,因此您的容器没有
> ssh_auth_sock
环境变量。如果我使用此
docker-compose.yaml
文件:我可以
exec
进入容器(等待包装安装完成后)并验证它能够与我的代理人:另外,关于您的
卷的一个无关评论:
block:您在参考变量的方式上不一致。这不是问题,但它会伤害我的大脑(这样的不一致有时会在其他情况下导致奇怪的问题)。您不妨始终使用$ {varname}
语法,当时参考环境变量:Your
environment
syntax is incorrect. Theenvironment
block can either be a list ofNAME=VALUE
pairs:Or it can be a dictionary:
Yours is neither of those things, so your container has no
SSH_AUTH_SOCK
environment variable.If I use this
docker-compose.yaml
file:I can
exec
into the container (after waiting for the package installation to complete) and verify that it is able to talk to my agent:Also, one unrelated comment about your
volumes:
block: you're being inconsistent in how you refer to variables. This isn't a problem, but it hurts my brain (and inconsistencies like this can sometimes lead to weird problems in other contexts). You might as well just always use the${varname}
syntax when referring to environment variables:我想对过程中涉及的不同组件给出更完整的答案,还包括GitHub Actions Workflow,以防有人需要。
我还制作了一个“ nofollow noreferrer” 。
docker compose(
docker-compose.yaml
):dockerfile(只是相关部分):
github工作流程:
I'd like to give a more complete answer with the different components involved in the process, also including a GitHub Actions workflow in case someone needs it.
I also made a gist on GitHub for Python, I'll just adapt it to your scenario here.
Docker Compose (
docker-compose.yaml
):Dockerfile (just the relevant part):
GitHub workflow: