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/
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://git@github.com/.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.
The above is the detailed content of 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?. For more information, please follow other related articles on the PHP Chinese website!