如何在docker-compose中访问私有GitHub代码仓库?

3
这是我的docker-compose.yml文件:
version: '2.1'

services:

  users-db:
    container_name: users-db
    build: git@github.com:lukalopusina/flask-microservices-users.git#master:project/db
    volumes:
        - '~/.ssh/github:/root/.ssh/id_rsa'
    ports:
        - 5435:5432  # expose ports - HOST:CONTAINER
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    healthcheck:
      test: exit 0

这是Dockerfile文件:
FROM postgres

# Disable checking for known_hosts (maybe not working)
RUN mkdir /root/.ssh && echo "StrictHostKeyChecking no " > /root/.ssh/config

# run create.sql on init
ADD create.sql /docker-entrypoint-initdb.d

当我运行 docker-compose up 命令时,出现以下错误:
Building users-db
ERROR: Error trying to use git: exit status 128 (Cloning into '/var/lib/docker/tmp/docker-build-git576570106'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
)

问题可能与ssh权限有关,但我将我的ssh密钥作为挂载卷添加到容器中(或者可能是我在那里犯了一些错误):
volumes:
    - '~/.ssh/github:/root/.ssh/id_rsa'

但它仍然无法正常工作。怎么解决这个问题呢?
这是我的本地机器上~/.ssh目录的权限:
drwx------   2 llopusina llopusina     4096 јун  7 14:22 .ssh

以下是我主机上 ~/.ssh 文件夹下文件的权限:

-rw-------  1 llopusina llopusina 3243 јун  7 14:15 github
-rw-r--r--  1 llopusina llopusina  749 јун  7 14:15 github.pub
-rw-r--r--  1 llopusina llopusina 1326 јун  7 14:35 known_hosts

问题在于构建时卷没有被挂载。只有在运行容器时才会使它们可用。这里有更多的信息:https://github.com/docker/compose/issues/6440 - Alexis Wilke
1个回答

1
请确保您挂载到容器中的.ssh文件夹和密钥具有正确的权限(文件夹为700,密钥文件为600),并且所有者设置为docker:docker
编辑: 看起来是docker守护程序和主机之间的密钥和上下文问题。我在docker-compose中找到了这个未解决的问题: https://github.com/docker/compose/issues/2856 最终的建议是:

提醒任何人报告:这是一个已知问题。 <...> 解决方法是在客户端上进行git克隆。我们不认为这是高优先级的,但欢迎PR。


我在问题描述中添加了权限列表。我的权限有什么问题吗? - Luka Lopusina

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