Jenkins Agent Dockerfile .ssh keys

Hi,
I have Jenkins running on an agent launching a Docker container.
One part of the pipeline is to call a shell script that scp’s a file inside that container.

How can I manage .ssh keys inside that container? One way I found is to copy the key via Dockerfile, however, in that case the key needs to be part of the repo which I’d like to avoid.

How can the .ssh key be added without it being part of the git repo?

thanks

is there any reason that you can’t make the location of that file to an external drive/folder? Volumes | Docker Docs
in the “docker run” add “-v /dir/on/host:/dir/in/container”

You can freely move files between them, even after the container is shutdown. Once the file is created inside the container, it exists outside the container. Then you can scp from the host machine.

If you want that volume mapping to happen all the time, you can set-up the docker-compose file to persistently have that defined? it’s in the link I provided.

I don’t write Docker files; but deploy out using them so I try to understand the capabilities as I do.

Thank you! I was ultimately able to solve it by using
withCredentials([sshUserPrivateKey(credentialsId: 'ABCD', keyFileVariable: 'SSH_KEY')])
inside the pipeline, and have the credentials managed by Jenkins.

1 Like