I'm using Docker and I'm running a Jenkins image. The image I'm trying: https://github.com/orchardup/docker-jenkins

I need to generate an ssh key pair so I may use it to authenticate to bitbucket. This is so that jenkins may pull my source code and build it. Normally I would just su to the jenkins user and run ssh-keygen to achieve this.

I've edited the run file:

su jenkins

This gives me access to the shell where I may run ssh-keygen to install ssh keys into jenkins user home directory under ~/.ssh. After this I exit and the Dockerfile continues. This home directory is /var/lib/jenkins which is also mounted as a volume on the host server.

Problem is that the ssh keys appear here but they are not working. Any ideas how to achieve this in better way ?


  • Which plugin are you using for the SCM repository, is it Git? – c4urself Feb 1 '14 at 20:11

I just realised you are using git from the question, which I had managed to overlook...

Starting from version 2.0 of "Git Plugin" the credentials for specific repositories can be added in the "Source Code Management" section of the test itself. There should be a label called "Credentials" which allow you to use either a username/password or username/private key combination.

If you create a deploy key for each repository you're interested in running tests add this deploy key as the private key in credentials and you should be good to go.

  • Hallo. I got this working in the way I've explained above - it needed a known_hosts entry. I would prefer the method you're describing but I can't get it to work. I've generated a private + public key (id_rsa and id_rsa.pub). I've setup the deploy key on the repo (id_rsa.pub). I've tried adding both keys and tried with my git username and jenkins. Not sure where I'm going wrong here. – Jason Prawn Feb 6 '14 at 12:24

Your approach should work.

You can generate the ssh keys on your host machine (ssh-keygen). The user doesn't have to be 'jenkins', it can be any other user.

After generating the keys, make sure to share it as a volume with the Jenkins Docker so /var/lib/jenkins/.ssh/ (in the container) will contain the host's keys (id_rsa).

You also have to make sure to add the generated public key (id_rsa.pub) as an 'SSH Key' to any user in Bitbucket that have the privileges to do what you like to do using Jenkins.

  • Hi, welcome to ServerFault! It may be helpful to also explain how to extract the key out of the container, e.g. by starting an interactive shell, and then running ssh-keygen and copying the public key out. Can you expand on your answer? – Rouben Jun 19 '20 at 3:09
  • i am using a jenkins docker image i installed the plugin and generate the pair of keys when i configure Publish over SSH in jenkins i get "No matching file" in my path to key (private one and it's content is copied in the next "key" input).Is there a way to correctly get the location of the keys in jenkins image ? – Youssef Boudaya Apr 21 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.