VSCode docker dev container can't access ~/.ssh

Viewed 1292

I need access to /home/vscode/.ssh/ so the tools I am using can use my ssh key, however I can't seem to change the permissions.

I build the container like so:

# other steps

COPY id_rsa /home/vscode/.ssh/id_rsa
RUN chmod 600 /home/vscode/.ssh/id_rsa \
  && touch /home/vscode/.ssh/known_hosts \
  && ssh-keyscan bitbucket.org >> /home/vscode/.ssh/known_hosts

# other steps 

This grabs my id_rsa from my local directory and adds it into the docker container so I can keep using my existing SSH key.

I then try and use a tool like terraform to execute a command that clones in some code from a repository that is setup with my SSH key.

$ terraform plan

ERRO[0001] 1 error occurred:
        * error downloading 'ssh://git@bitbucket.org/example/example.git?ref=master': /usr/bin/git exited with 128: Cloning into '/workspaces/example'...
Failed to add the RSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts (/home/vscode/.ssh/known_hosts).
Load key "/home/vscode/.ssh/id_rsa": Permission denied
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

How can I correctly setup access in my Dockerfile? I can't even run ssh-kegyen!

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vscode/.ssh/id_rsa): 
/home/vscode/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Saving key "/home/vscode/.ssh/id_rsa" failed: Permission denied
1 Answers

After several attempts at overriding user permissions in the Dockerfile I came to find out the user is set from the devcontainer.json file.

Simply go to the bottom where it sets the"remoteUser": "vscode" and comment it out.

This will now set the user as root. For local development this is perfect and should cause no issues. I can now utilise the SSH key as well as anything else previously locked off.

// .devcontainer/devcontainer.json

{
    "name": "Ubuntu",
    "build": {
        "dockerfile": "Dockerfile",
        // Update 'VARIANT' to pick an Ubuntu version: focal, bionic
        "args": { "VARIANT": "focal" }
    },

    "settings": {},

    "extensions": [],

    // "remoteUser": "vscode"
}
Related