Git Clone Not Working With Npm

- 1 answer

I am trying to clone a private Git repo from npm inside a Docker container but am getting an error Permission denied (publickey).

I generated a SSH key with this command on my host machine:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Then I added this to Bitbucket, ssh-add-ed it, and tested it with git clone and everything worked fine (on the host machine). I also tried npm install in my project folder and all git cloning was fine.

I copied the id_rsa and files to another folder and I mount this folder to /root/.ssh when doing docker run. The problem now is that npm install gives a permission denied error when trying to clone from that private Git repo.

I went into the container with bash and tried npm install manually, but got the same error. I made sure that ssh-agent was up by running eval "$(ssh-agent -s)" and then made sure the key was added by running ssh-add ~/.ssh/id_rsa. Then I tried it again, but the same issue.

The thing though is that if, in the same container, I try just a regular git clone ... then it clones fine! No problems or anything. I know it is using the SSH key since if I remove it from Bitbucket, I get a permission denied error. Then if I re-add it to Bitbucket, it clones fine.

How do I get npm to clone properly? The working (inside the container) git clone command I use is:

git clone [email protected]:org/repo.git

and the npm repo dependency line is:

"repo": "git+ssh://"

The full npm error is:

npm ERR! git clone ssh:// Cloning into bare repository '/root/.npm/_git-remotes/ssh-bitbucket-org-org-repo-git-3f459951'...
npm ERR! git clone ssh:// Permission denied (publickey).
npm ERR! git clone ssh:// fatal: Could not read from remote repository.
npm ERR! git clone ssh:// 
npm ERR! git clone ssh:// Please make sure you have the correct access rights
npm ERR! git clone ssh:// and the repository exists.


"git+ssh://" does not look like a valid ssh url.

"git+ssh://[email protected]/{user}/{repository}.git" is.

Check if you can change the npm repo dependency url.
(and/or npm install -S "git+https://[email protected]/orgname/repositoryname.git")