この記事はプログラミング初心者が自分のメモ用として書いている記事です。
そのため、誤った情報が記述されている可能性があります。
自己判断で読み進めてください
Dockerコンテナ内で git がインストールされているか確認
root@be96f8ff3ea8:/# docker exec -it password_manager_container bash
bash: docker: command not found
インストールされていないことを確認
なければコンテナ内でインストール
apt update && apt install -y git
GitHub で SSH 設定を行う
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
SSHエージェントを起動
root@be96f8ff3ea8:/# eval "$(ssh-agent -s)"
Agent pid ⚫︎⚫︎⚫︎⚫︎
SSHキーをGitHubに登録
公開鍵を表示する。
cat ~/.ssh/id_rsa.pub
表示されたキーを GitHub の設定 > SSH and GPG keys で追加。
SSH接続の確認
以下のコマンドを実行し、GitHubとの接続を確認。
ssh -T git@github.com
Gitの初期設定を行う
git config --global user.name "Githubで登録した名前"
git config --global user.email "Githubで登録したメール"
コンテナ内でスクリプトを管理するディレクトリに移動してgit init
あとはいつも通り、ブランチの切り替え
root@be96f8ff3ea8:/# git checkout -b pullrequest
GitHubリモートリポジトリを追加
git remote add origin git@github.com:kb8864/DockerTest.git
gitpush
エラーが発生
root@be96f8ff3ea8:/bin# git push origin pullrequest
The authenticity of host 'github.com (20.27.177.113)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Dockerコンテナ内でGitHubのSSHホストキーが認識されていないために発生しているらしい
対策
まず、GitHubの公式ホストキーをDockerコンテナ内に追加して、github.com のホストキーが信頼できるものとして登録
ssh-keyscan github.com >> ~/.ssh/known_hosts
次に、GitHubとSSH接続できるかを確認
ssh -T git@github.com
再度gitpushを行う
remote: Resolving deltas: 100% (163/163), done.
To github.com:kb8864/DockerTest.git
* [new branch] pullrequest -> pullrequest
branch 'pullrequest' set up to track 'origin/pullrequest'.