Skip to content

Instantly share code, notes, and snippets.

@zoka123
Last active November 28, 2022 12:41
Show Gist options
  • Save zoka123/7f4f4d5b2bde458c6eb89a651fd5a03a to your computer and use it in GitHub Desktop.
Save zoka123/7f4f4d5b2bde458c6eb89a651fd5a03a to your computer and use it in GitHub Desktop.
Docker SSH agent forwarding for macOS

Add SSH key to your host agent and verify it's present with ssh-add -L.

If you're using a passphrase and you face issues, try adding your key with ssh-add -K {path}, docs:

-K      When adding identities, each passphrase will also be stored in
             the user's keychain.  When removing identities with -d, each
             passphrase will be removed from it.

Configure your container like this (docker-compose.yaml)

service:
    environment:
        SSH_AUTH_SOCK: '/run/host-services/ssh-auth.sock'
    volumes:
        - /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock

Fixing permissions issue

You need to ensure the container user has access to the mounted .sock file. Change ownerhsip if you face permission denied issues.

docker-compose run --rm --user root {service} chown {user} /run/host-services/ssh-auth.sock

Now, with this configuration, you should be able to attach to the container and run ssh-add -L inside it to see the same key from your host.

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