docker 使用 ssh url 到 git 回购的构建命令:权限被拒绝

docker build command using ssh url to git repo: Permission denied

我正在尝试制作一个脚本来构建一堆 docker 图像并将它们推送到私有存储库。

documentationdocker build命令似乎接受giturls:确实非常好。

所有存储库都是私有的,公司中的每个人都设置了 ssh 密钥以通过 ssh 访问 git 存储库,例如 git clone git@github.com:/my-org/my-repo.git

我假设提供这样的 url 会很好用,因为它似乎是一个非常常见的用例。结果不是。

我在谷歌上搜索了一个解决方案并找到了 git ticket about url formatting 所以我尝试了以下所有方法:

此列表中的最后一个是最有希望的,因为我得到以下输出:

$ docker build -t registry.example.com:5000/my-repo:latest --ssh=default git@github.com:my-org/my-repo.git

[+] Building 0.9s (1/1) FINISHED                                                                                                                                                             
 => ERROR [internal] load git source git@github.com:my-org/my-repo.git                                                                                                     0.9s
------                                                                                                                                                                                       
 > [internal] load git source git@github.com:my-org/my-repo.git:                                                                                                                
#1 0.551 Warning: Permanently added the RSA host key for IP address '140.82.121.3' to the list of known hosts.
#1 0.896 git@github.com: Permission denied (publickey).
#1 0.898 fatal: Could not read from remote repository.
#1 0.898 
#1 0.898 Please make sure you have the correct access rights
#1 0.898 and the repository exists.
------
failed to solve with frontend dockerfile.v0: failed to read dockerfile: failed to load cache key: failed to fetch remote git@github.com:my-org/my-repo.git: exit status 128

在有人问之前:是的,存储库存在,我可以克隆它:)

我假设该过程的“克隆”部分将在发送上下文供 docker 构建之前使用我自己的 ssh 密钥“在本地”完成。显然不是这样。

它是受支持的功能吗?如果是,如何让它发挥作用?


编辑:我意识到我忘了提供一些背景信息。

我是 运行 Docker macOS big sur 桌面

Docker version 20.10.8, build 3967b7d

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
  compose: Docker Compose (Docker Inc., v2.0.0-rc.3)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 9
  Running: 8
  Paused: 0
  Stopped: 1
 Images: 28
 Server Version: 20.10.8
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e25210fe30a0a703442421b0f60afac609f950a3
 runc version: v1.0.1-0-g4144b63
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.47-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.842GiB
 Name: docker-desktop
 ID: 77LC:Z2AY:K6AA:OXAY:3JYQ:RSSL:RCJZ:GOSK:FUTG:DAPY:WIKK:BB7A
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 105
  Goroutines: 93
  System Time: 2021-09-16T08:47:27.924652162Z
  EventsListeners: 4
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  <REDACTED>
 Live Restore Enabled: false

Docker for Mac 并不 运行 在你的机器上,而是在虚拟 Machine 中。看起来 git clone 命令是在 VirtualMachine

中执行的

我的假设是基于这个日志条目: #1 0.551 Warning: Permanently added the RSA host key for IP address '140.82.121.3' to the list of known hosts.

因此,为了通过 ssh 访问您的私有存储库,您还需要将 ssh 密钥对存储在 Docker 的 VirtualMachine 中。

编辑 要连接到虚拟Machine 打开一个终端并运行 docker run -it --privileged --pid=host justincormack/nsenter1

设置--ssh=default时是否将SSH密钥添加到正在转发的ssh-agent中?要检查 运行 ssh-add -l,如果它没有显示您的密钥,则添加类似 ssh-add -k ~/.ssh/id_rsa

的内容