Giter Club home page Giter Club logo

docker-ssh-agent's Introduction

Docker SSH Agent

Lets you store your SSH authentication keys in a dockerized ssh-agent that can provide the SSH authentication socket for other containers. Works in OSX and Linux environments.

Why?

On OSX you cannot simply forward your authentication socket to a docker container to be able to e.g clone private repositories that you have access to. You don't want to copy your private key to all containers either. The solution is to add your keys only once to a long-lived ssh-agent container that can be used by other containers and stopped when not needed anymore.

hub.docker.com

You can pull the image from DockerHub via

docker pull nardeas/ssh-agent

How to use

Quickstart

To get up and running super fast, run the run.sh script which will build the images for you, launch the ssh-agent and add your keys. If your keys are password protected (hopefully) you will just need to input your passphrase.

Launch everything:

./run.sh

Remove your keys from ssh-agent and stop container:

./run.sh -s

Step by step

0. Build

Navigate to the project directory and launch the following command to build the image:

docker build -t docker-ssh-agent:latest -f Dockerfile .

1. Run a long-lived container

docker run -d --name=ssh-agent docker-ssh-agent:latest

2. Add your ssh keys

Run a temporary container with volume mounted from host that includes your SSH keys. SSH key id_rsa will be added to ssh-agent (you can replace id_rsa with your key name):

docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa

The ssh-agent container is now ready to use.

3. Add ssh-agent socket to other container:

If you're using docker-compose this is how you forward the socket to a container:

  volumes_from:
    - ssh-agent
  environment:
    - SSH_AUTH_SOCK=/.ssh-agent/socket
For non-root users

The above only works for root. ssh-agent socket is accessible only to the user which started this agent or for root user. So other users don't have access to /.ssh-agent/socket. If you have another user in your container you should do the following:

  1. Install socat utility in your container
  2. Make proxy-socket in your container:
sudo socat UNIX-LISTEN:~/.ssh/socket,fork UNIX-CONNECT:/.ssh-agent/socket &
  1. Change the owner of this proxy-socket
sudo chown $(id -u) ~/.ssh/socket
  1. You will need to use different SSH_AUTH_SOCK for this user:
SSH_AUTH_SOCK=~/.ssh/socket
Without docker-compose

Here's an example how to run a Ubuntu container that uses the ssh authentication socket:

docker run -it --volumes-from=ssh-agent -e SSH_AUTH_SOCK=/.ssh-agent/socket ubuntu:latest /bin/bash

Deleting keys from the container

Run a temporary container and delete all known keys from ssh-agent:

docker run --rm --volumes-from=ssh-agent -it docker-ssh-agent:latest ssh-add -D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.