The problem
An agent finishes its work on a branch and has to push it. Copying a private key into a throwaway container, or mounting ~/.ssh, hands the key to everything that runs there.
devsandbox forwards your host’s ssh-agent instead: ssh inside the sandbox asks it to sign, and the key never leaves the host. You only add GitHub’s host key and your git identity.
Let your agent set this upConfig
[sandbox.app]
folder = "../app" # your repo, next to this config dir
image = "node:22"
# Your login and settings, from the host.
mounts = ["source=${localEnv:HOME}/,target=/root/,type=bind"]
containerEnv = { = "/root/" }
[sandbox.app.postCreateCommand] # once, when an instance is created
git-identity = "git config --global user.name 'Your Name' && git config --global user.email you@example.com"
known-hosts = "mkdir -p ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts" # trust github.com
agent = "npm install -g "[sandbox.app]
folder = "../app" # your repo, next to this config dir
image = "node:22"
# Your login and settings, from the host.
mounts = ["source=${localEnv:HOME}/,target=/root/,type=bind"]
[sandbox.app.postCreateCommand] # once, when an instance is created
git-identity = "git config --global user.name 'Your Name' && git config --global user.email you@example.com"
known-hosts = "mkdir -p ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts" # trust github.com
agent = "npm install -g "On macOS keeps its login in the Keychain: run /login once in any instance.
Try it
ssh-add -l # on the host: your key is loaded
devsandbox run app --name fix-login
devsandbox exec fix-login ssh-add -l # the same key, seen from insideStart in it and have it commit and push its branch:
devsandbox exec -it fix-login devsandbox exec -it fix-login The remote must be an ssh URL (git@github.com:…); https:// remotes never use the ssh-agent.
How it works
- No config: every
devsandbox exec, dashboard terminal and lifecycle command getsSSH_AUTH_SOCKrelayed to your host’s ssh-agent. - A plain
docker execshell gets no forwarding. - Works on Linux (docker, podman) and macOS (docker; Apple
containeruntested). Native Windows: use WSL2.
Agent instructions
Make git push over ssh work from my devsandbox sandbox <sandbox name>, using my host's ssh-agent; no keys in the container.
- Read the devsandbox-cli and config-toml-spec skills first (load them if they are installed, otherwise read them from https://github.com/Nemikolh/devsandboxes/tree/main/skills). Follow them over your own assumptions: unknown config keys are hard errors.
- Find my devsandbox config dir: the folder holding devsandboxes.toml, passed to devsandbox as
-C <config dir>. If you cannot tell which one, ask me. - Check the host:
ssh-add -llists a key, and devsandbox was installed from npm or a release archive (acargo installbuild forwards only on Linux docker/podman). - Add no keys or socket mounts. In
[sandbox.<sandbox name>], add apostCreateCommandtable that runsssh-keyscan github.cominto ~/.ssh/known_hosts, sets gituser.name/user.emailto <your name> / <your email>, and installs . Mount 's host config dir (its login) into the container. - Check the repo remote is an ssh URL (git@github.com:…); ask me before changing it.
- Validate with
devsandbox -C <config dir> lsand fix every error it reports before going on. - Start an instance with
devsandbox -C <config dir> run <sandbox name> --name <task>. - Check
devsandbox -C <config dir> exec <task> ssh-add -llists my key anddevsandbox -C <config dir> exec <task> --versionworks. Push only when I ask. - Ask me before anything destructive: devsandbox rm, gc, rebuild --force, service rebuild, deleting files or branches.
Source: site/src/content/examples/ssh-and-git-push.mdxEdit this page on GitHub(opens in a new tab)