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://[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.
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