Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created January 29, 2014 23:32
Show Gist options
  • Save d11wtq/8699521 to your computer and use it in GitHub Desktop.
Save d11wtq/8699521 to your computer and use it in GitHub Desktop.
How to SSH agent forward into a docker container
docker run -rm -t -i -v $(dirname $SSH_AUTH_SOCK) -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK ubuntu /bin/bash
@nocanstillbb
Copy link

run docker -p  222:22 && apt install openssh-server &&  $(edit /etc/ssh/sshdconfig to enable root login)

on your mac of git bash

eval $(ssh-agent -s)
ssh-add 
ssh -A  toDockerContainer

@x85446
Copy link

x85446 commented Dec 5, 2024

Thanks! You pointed me in the right direction for a very similar problem. Here’s my take on it, implemented within a Makefile. This is very much specific to a mac os problem with a Docker Desktop solution.

# izumanetworks.com ai-edge-runner
run:
    @if [ -z "$(WORKSPACE_PATH)" ]; then \
        echo "Error: Please specify the path to map using MAP=/path/to/map"; \
        exit 1; \
    fi
    @if [ -z "$(SSH_AUTH_SOCK)" ]; then \
        echo "Error: SSH agent is not running. Please start it with 'eval $$(ssh-agent -s)' and add your key with 'ssh-add'."; \
        exit 1; \
    fi
    docker run -it \
        --name $(CONTAINER_NAME) \
        -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock \
        -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock \
        -v $(WORKSPACE_PATH):/izuma \
        --entrypoint /bin/bash \
        $(IMAGE_NAME)

The magic is that even though macOS doesn’t have a /run/blah/blah path, Docker Desktop creates /run/host-services/ssh-auth.sock as a special bridge to your host system’s SSH_AUTH_SOCK.

To test, run ssh-add -l inside the container to list your keys and ssh -T [email protected] to verify connectivity. This approach works seamlessly with Docker Desktop on macOS.

@hannylicious
Copy link

the latest official documentation helped me with docker-compose setup https://docs.docker.com/desktop/networking/#ssh-agent-forwarding

That seems to be specific to Docker Desktop. What about Colima and/or Podman?

Did you ever figure this out on Podman specifically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment