I'm looking to forwarding my ssh-agent and found this https://github.com/nardeas/ssh-agent

and the steps are the following

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

in a compose file, I add step 1 to it like so:

services:
  ssh_agent:
    image: nardeas/ssh-agent

However I do not what's the equivalent syntax in compose file for step 2

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

2 Answers

0
vivekyad4v On

You can do it as below -

docker-compose -f my-docker-compose.yml run --rm ssh_agent bash -c "ssh-add /root/.ssh/id_rsa"

Ref - https://docs.docker.com/compose/reference/run/

0
Prem On

docker-compose.yml file will be

services:
  ssh_agent:
  image: docker-ssh-agent:latest
  command: ssh-add /root/.ssh/id_rsa
  volumes_from:
    - ssh-agent
  environment:
    - SSH_AUTH_SOCK=/.ssh-agent/socket
  volumes:
    - ~/.ssh:/.ssh

then run the docker-compose command as below

docker-compose -f docker-compose.yml run --rm ssh_agent