Manage Multiple SSH Keys with Docker

The Problem

You've had that problem where you have a work Bitbucket / Github account and a personal bitbucket account and you can reuse your SSH key? So have I. Thanks to docker we can have multiple ssh keys on our machine, mount those keys as the default keys inside of a docker container, and our problem is solved.

The Solution

docker run -ti --rm -v `pwd`:/git -v ~/.ssh/ -v ~/.ssh/bitbucket_personal:/root/.ssh/id_rsa alpine/git push origin master

This command is running docker push via the alpine/git docker image. Alpine linux has a very small footprint and is becoming more and more common in reducing the size of docker images. We use the git image for running git commands. That magic here is the use of volumes which are the "-v" options being passed. Volumes allow you to mount files and directories into a container like a shared file system. I am mounting specific files because I don't want it to pick up (or overwrite) id_rsa and on my host machine.


docker run -ti -rm
For option descriptions, see docker docs. I'm invoking docker to run a container.

-v `pwd`:/git
Mount my code into the container so git and push it up.

-v ~/.ssh/
Mount my alternative public key as the default public ssh key.

-v ~/.ssh/bitbucket_personal:/root/.ssh/id_rsa
Mount my alternative private key as the default private ssh key.

alpine/git push origin master
The container being run is using the alpine/git image and running "git push origin master" inside of it.

comments powered by Disqus

© Balanced Scale Media, LLC. All right reserved.