I created a Docker container and run it via shell script which contains this execution:
docker run --rm -it \
--user $(id -u):$(id -g) \
--network host \
-e USER=$USER \
-e HOME=$HOME \
-h $HOSTNAME \
-v /tmp:/tmp \
-v /home/$USER:/home/$USER \
-v ~/.ssh:/home/$USER/.ssh:ro \
-e USERNAME=$USERNAME \
-w /home/$USER \
$IMAGE_NAME \
/bin/bash
Therefore, I think there is neither right access nor key existence issue. However, if I go to my git working directory which has been cloned with ssh before. I cannot authenticate.
git pull -v
git@mygitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Printing out the output of ssh -tv git@mygitlab.com I see these differences:
Without Docker container
...
debug1: Will attempt key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx agent
debug1: Will attempt key: gitlab ED25519 SHA256:fEqoFK agent
debug1: SSH2_MSG_EXT_INFO received
....
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx agent
debug1: Authentications that can continue: publickey
debug1: Offering public key: gitlab ED25519 SHA256:fEqoFK agent
debug1: Server accepts key: gitlab ED25519 SHA256:fEqoFK agent
debug1: Authentication succeeded (publickey).
In the Docker container:
...
debug1: Will attempt key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx
debug1: Will attempt key: /home/xxx/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/xxx/.ssh/id_rsa
debug1: Offering public key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/xxx/.ssh/id_xmss
debug1: No more authentication methods to try.
git@mygitlab.com: Permission denied (publickey).
Shall I need something to do with the ssh-agent?