"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Resolve "fatal: could not read Username for 'https://github.com': No such device or address" Error When Cloning Private GitHub Repos in Docker?

How to Resolve "fatal: could not read Username for 'https://github.com': No such device or address" Error When Cloning Private GitHub Repos in Docker?

Published on 2024-11-21
Browse:305

How to Resolve

Docker: Resolving GitHub SSH Key Issues for Private Repo Cloning

When attempting to run a container that utilizes a golang service from a private GitHub repository, you may encounter errors during the go get process. One such error is related to problems reading the GitHub SSH public key.

Problem:
When running go get github.com// within a Dockerfile, the process fails with the error: "fatal: could not read Username for 'https://github.com': No such device or address."

Cause:
This error indicates that the Dockerfile is not properly configured to authenticate with GitHub using SSH. The ssh-keyscan command reveals a mismatch between the public key and the remote host's record.

Solution:
To resolve this issue, you need to add a git config command to your Dockerfile that forces the use of SSH instead of the default HTTPS protocol for GitHub cloning. Here's an example Dockerfile that incorporates this fix:

FROM golang

RUN apt-get update && apt-get install -y ca-certificates git-core ssh

ADD keys/my_key_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
RUN git config --global url.ssh://[email protected]/.insteadOf https://github.com/

ADD . /go/src/github.com/myaccount/myprivaterepo

RUN go get github.com/myaccount/myprivaterepo
RUN go install github.com/myaccount/myprivaterepo

This revised Dockerfile ensures that SSH is used for GitHub cloning, addressing the error related to reading the public key. It also includes the necessary steps to install SSH and configure the identity key.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3