无法在docker-compose中克隆存储库。

huangapple go评论118阅读模式
英文:

Unable to clone repository in docker-compose

问题

我正在尝试使用docker-compose在localstack上配置一个应用程序。当我尝试克隆存储库时,出现了一个错误。

setup-resources_1  | make[1]: [/app/ProjectRecipes.mk:35: clean] Error 2 (ignored)
setup-resources_1  | ServiceName=Tests make -C /app/ -f /app/ProjectRecipes.mk deploy
setup-resources_1  | git clone https://github.com/MyGithOrg/TestProject /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject
setup-resources_1  | Cloning into '/Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject'...
localstack_1       | Waiting for all LocalStack services to be ready
setup-resources_1  | fatal: could not read Username for 'https://github.com': No such device or address
setup-resources_1  | make[4]: *** [/app/ProjectRecipes.mk:24: /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject] Error 128

docker-compose.yml

version: '3'
services:
  localstack:
    image: localstack/localstack
    ports:
      - "53:53"
      - "443:443"
      - "4510-4520:4510-4520"
      - "4566-4620:4566-4620"
      - "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
    environment:
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY}
      - SERVICES=serverless,rds,lambda,sqs,dynamodb,s3,apigateway,stepfunctions,cloudformation,appsync,firehose,es
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOST_TMP_FOLDER=${TMPDIR}
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - default

  setup-resources:
    image: golang:1.16.4-alpine3.13
    entrypoint: /bin/sh -c
    working_dir: /app
    command: >
      "
        ls
        apk add --update alpine-sdk
        make Tests
      "
    networks:
      - default
    volumes:
      - type: bind
        source: ~/go
        target: /go
      - type: bind
        source: ${HOME}
        target: /root
      - type: bind
        source: .
        target: /app
      - ~/.ssh:/root/.ssh
      - ~/.gitconfig:/root/.gitconfig
    depends_on:
      - localstack

请注意,这是一个错误消息和docker-compose.yml文件的示例。

英文:

I'm trying to configure an app to run on localstack using docker-compose. I'm getting an error when attempting to clone the resository.

setup-resources_1  | make[1]: [/app/ProjectRecipes.mk:35: clean] Error 2 (ignored)
setup-resources_1  | ServiceName=Tests make -C /app/ -f /app/ProjectRecipes.mk deploy
setup-resources_1  | git clone https://github.com/MyGithOrg/TestProject /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject
setup-resources_1  | Cloning into '/Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject'...
localstack_1       | Waiting for all LocalStack services to be ready
setup-resources_1  | fatal: could not read Username for 'https://github.com': No such device or address
setup-resources_1  | make[4]: *** [/app/ProjectRecipes.mk:24: /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject] Error 128

docker-compose.yml

version: '3'
services:
  localstack:
    image: localstack/localstack
    ports:
      - "53:53"
      - "443:443"
      - "4510-4520:4510-4520"
      - "4566-4620:4566-4620"
      - "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
    environment:
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY}
      - SERVICES=serverless,rds,lambda,sqs,dynamodb,s3,apigateway,stepfunctions,cloudformation,appsync,firehose,es
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOST_TMP_FOLDER=${TMPDIR}
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - default

  setup-resources:
    image: golang:1.16.4-alpine3.13
    entrypoint: /bin/sh -c
    working_dir: /app
    command: >
      "
        ls
        apk add --update alpine-sdk
        make Tests
      "
    networks:
      - default
    volumes:
      - type: bind
        source: ~/go
        target: /go
      - type: bind
        source: ${HOME}
        target: /root
      - type: bind
        source: .
        target: /app
      - ~/.ssh:/root/.ssh
      - ~/.gitconfig:/root/.gitconfig
    depends_on:
      - localstack

答案1

得分: 1

问题:

以下命令在容器中失败,因为它是一个交互式命令,需要用户输入GitHub的用户名密码。尽管在本地机器上不是这样,但在容器内是这样的。我不知道这种差异的原因 😕。

git clone https://github.com/MyGithOrg/TestProject /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject

解决方案:

您可以使用以下命令通过SSH克隆您的存储库:

git clone ssh://git@github.com/MyGithOrg/TestProject.git /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject

步骤:

  1. 更新您的克隆命令以使用上述SSH。

  2. 如果您尚未在Github配置SSH密钥,可以按照此文档进行配置。

    > ​⚠️​ 确保在此步骤中,您能够在本地机器上使用SSH克隆存储库。

  3. 更新docker-compose文件中的command以安装SSH客户端

    command: >
      "
        ls
        apk add --update alpine-sdk openssh
        make Tests
      "
    
  4. 如果您使用的是Mac,请确保在~/.ssh/config文件中将IgnoreUnknown UseKeychain放在UseKeychain yes之前。

    > ℹ️ 原因是您在Golang Alpine镜像中安装的openssh版本不识别UseKeychain选项,并且会抛出Bad configuration option: usekeychain错误。您可以在此文档中了解更多信息。

    正确的~/.ssh/config文件示例:

    Host *
      AddKeysToAgent yes
      IgnoreUnknown UseKeychain
      UseKeychain yes
      IdentityFile ~/.ssh/id_rsa
    
  5. 然后按照以下方式更新您的volumes

    volumes:
      - type: bind
        source: ~/go
        target: /go
      - type: bind
        source: .
        target: /app
      - ~/.ssh:/root/.ssh
    

    请注意,您不再需要~/.gitconfig:/root/.gitconfig卷。我还删除了将HOME目录映射到容器的/root的映射,因为我仍然怀疑它可能会引起问题。

我相信现在这应该对您有用。祝您好运 🍻 !!!

英文:

Problem:

The following command fails in your container because it is an interactive command that expects the user to enter username and password for GitHub. Although this is not the case in your local machine, that is the case inside the container. I don't know the reason for the difference 😕.

git clone https://github.com/MyGithOrg/TestProject /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject

Solution:

You can clone your repository through SSH using the following command:

git clone ssh://git@github.com/MyGithOrg/TestProject.git /Users/antarr.byrd/go/src/github.com/MyGithOrg/TestProject

Steps:

  1. Update your clone command to use SSH like above.

  2. If you haven't configured an SSH key in your Github profile, you can follow this documentation to do so.

    > ​⚠️​ Make sure at this step you are able to clone the repo using SSH in your local machine.

  3. Update your command in docker-compose file to install an SSH client

    command: >
      "
        ls
        apk add --update alpine-sdk openssh
        make Tests
      "
    
  4. If you're using a Mac, then make sure you have IgnoreUnknown UseKeychain above UseKeychain yes in your ~/.ssh/config file.

    > ℹ️ The reason is that openssh version that is being installed in your Golang Alpine image does not recognize UseKeychain option and will throw you Bad configuration option: usekeychain error. You can read more about that in this document.

    Example of a correct ~/.ssh/config file:

    Host *
      AddKeysToAgent yes
      IgnoreUnknown UseKeychain
      UseKeychain yes
      IdentityFile ~/.ssh/id_rsa
    
  5. Then update your volumes like below:

    volumes:
      - type: bind
        source: ~/go
        target: /go
      - type: bind
        source: .
        target: /app
      - ~/.ssh:/root/.ssh
    

    Note that you no longer need ~/.gitconfig:/root/.gitconfig volume. I also removed your mapping of HOME directory to /root of the container because I still doubt that it might cause issues.

I believe this should work for you now. Cheers 🍻 !!!

huangapple
  • 本文由 发表于 2021年5月20日 22:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/67622753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定