# Dev Container Git Hooks: How Code in the Container Runs on Your Host

> A git hook and a core.fsmonitor setting written inside a dev container ran on the host as your user. Run git in the container, or turn both off on the host.

Published: 2026-11-20. Author: Milan Jovanović.

Canonical: https://milanjovanovic.tech/blog/devcontainer-git-hooks-escape

When a dev container shares your repo folder with the host, code in the container can write to the hidden `.git` folder.
A hook or a git setting planted there ran on the host, as the host user, the next time git ran.
Run git inside the container, or pass `-c core.hooksPath=/dev/null -c core.fsmonitor=false` to git on the host.

I tested with git 2.43.0 on a 2 vCPU / 8 GB Ubuntu 24.04 VPS, Docker 29.10, and Claude Code 2.1.329, on November 20, 2026.
The container image is based on Anthropic's [**reference dev container**](https://github.com/anthropics/claude-code/tree/main/.devcontainer).
I wrote a hook from inside the container, then made a normal commit on the host:

![Terminal showing a pre-commit hook written inside the container as the claude user. A normal git commit on the host then runs it, and the hook](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/hook_ran_on_host.png)

The hook ran as `agent`, the host user, with the `docker` group.
Members of the `docker` group can start any container, which is the same as having root on the host.

## Why Can a Dev Container Change Your Git Repo?

Your repo gets into the container through a **bind mount**, which shows a host folder inside the container without copying it.
Every write in the container lands in the host's files, including `.git`.
The [**Claude Code dev container docs**](https://code.claude.com/docs/en/devcontainer) warn: "Claude can still modify any file in the bind-mounted workspace, which appears directly on your host".

![Inside the container, the claude user with UID 1000 writes a pre-commit hook, a core.hooksPath setting, and a core.fsmonitor command into the bind-mounted repo. On the host, the agent user, also UID 1000 and in the docker group, runs git commit or git status, and git runs the planted command. Running git with core.hooksPath set to /dev/null and core.fsmonitor set to false stopped all of them.](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/hook_escape_path.png)

Nothing under `.git` shows up in `git diff` or a pull request.
Claude Code normally asks before it writes to `.git`, but in bypass mode its Write tool created `.git/hooks/pre-commit` without a prompt.
[**What still applies with --dangerously-skip-permissions**](https://milanjovanovic.tech/blog/claude-code-dangerously-skip-permissions) covers that mode.

## Can a Git Setting Run Code Without a Hook File?

Yes.
The [**`core.fsmonitor`**](https://git-scm.com/docs/git-config) setting names a helper command that git runs to speed up commands like `git status`.
Git runs whatever the setting holds:

![Terminal showing a core.fsmonitor command set with git config inside the container. On the host, git diff and git status run with no commit and no hook file, and the marker file holds the host user](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/fsmonitor_ran_on_host.png)

One of those two read-only commands triggered it, and neither was a commit.
`git status` exited with `0` and showed nothing.

**`core.hooksPath`** works too.
It points git at a hook folder among your project files, so `.git/hooks` can stay empty.
A hook planted at `tools/hooks/pre-commit` ran on the next host commit, with the same id line.

## Why Doesn't safe.directory Stop This?

Git's `safe.directory` check refuses to use a repo owned by another user.
Linux tracks users by a number called a **UID**, and both users here are UID 1000:

![Terminal showing the container user claude with uid 1000. On the host, stat lists .git, .git/config, .git/hooks, and the planted tools/hooks/pre-commit file as owned by agent, uid 1000.](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/same_uid_owner.png)

The Dev Containers CLI gives the container user the repo owner's UID on purpose, so the agent can write to the repo.
[**Running the dev container with the CLI**](https://milanjovanovic.tech/blog/claude-code-devcontainer-cli) shows the errors you get without that match.
Git sees a repo you own, so it runs the hooks and settings without a warning.

## How Do You Stop Planted Hooks From Running on the Host?

Run git for that repo inside the container.
The same commit there ran the hook as `claude`, and no file appeared in the host's `/tmp`.

When you have to run git on the host, turn off hooks and fsmonitor for each command:

![Terminal on the host. git config still lists the planted core.hookspath and core.fsmonitor values. A commit and a status run with core.hooksPath set to /dev/null and core.fsmonitor set to false, and none of the three marker files exists afterwards.](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/safe_commit.png)

Setting `core.hooksPath` to `/dev/null` turns off every hook for that command, including your own.
The `?? tools/` line is the only visible trace of the planted hook folder.
Git has other settings that run commands, such as `core.sshCommand`, `diff.external`, `core.pager`, and filter drivers.
These two options don't cover them, and I didn't test them.

For review, let the agent push a branch, then pull it into a fresh clone outside the agent's folder.
Build and test it in a container or in CI, not as your host user, because that runs the agent's code.
Give the container a GitHub token scoped to that one repo.

## Why Mount .devcontainer Read-Only?

The `.devcontainer` folder holds the `Dockerfile`, `devcontainer.json`, and `init-firewall.sh`, which set the container's limits.
The firewall script runs as root on every start, and Anthropic's reference container leaves the folder writable.
My `devcontainer.json` adds a read-only mount over it:

```json
"source=${localWorkspaceFolder}/.devcontainer,target=/workspace/.devcontainer,type=bind,readonly"
```

![Terminal inside the container with .devcontainer mounted read-only. Appending to init-firewall.sh, creating a file, and deleting devcontainer.json all fail with Read-only file system, while the rest of the workspace stays writable.](https://milanjovanovic.tech/blogs/articles/devcontainer-git-hooks-escape/readonly_devcontainer.png)

The mount doesn't cover `.git`.
The agent can also read its own login token and use the open paths in the [**firewall guide**](https://milanjovanovic.tech/blog/claude-code-devcontainer-firewall).

## Summary

A bind-mounted repo shares its `.git` folder with the host, so a hook or setting planted there runs as your user, with your `docker` group.
Git's ownership check doesn't help, because the container user has your UID.
Run git inside the container, or pass `-c core.hooksPath=/dev/null -c core.fsmonitor=false` on the host.
Mount `.devcontainer` read-only, and build agent branches in a container or CI.

Thanks for reading.

And stay awesome!

---

## Frequently asked questions

### Can a dev container run code on the host through git hooks?

Yes, when the repo is bind-mounted and you run git on the host. In testing, a pre-commit hook written from inside the container ran on the host as the host user, who was in the docker group, as soon as that user made a commit.

### Is core.fsmonitor a security risk?

It can be. The setting names a command that git runs to speed up commands like git status. A value set from inside a dev container ran on the host when the host user ran git diff and git status, with no commit and no hook file.

### Does safe.directory protect against hooks from a dev container?

No. Git only refuses repos owned by another user. The container user had the same UID as the host user, so every file it wrote in the repo was owned by the host user and the check never triggered.

### How do you run git safely on a repo an agent worked on?

Run git inside the container, or pass -c core.hooksPath=/dev/null -c core.fsmonitor=false on the host. For review, have the agent push a branch, review the pull request, and pull it into a fresh clone outside the agent folder. Run its build and tests in a container or in CI, not as your host user.

### Does a Claude Code dev container protect the host?

Only partly. The repo is shared with the host, so hooks and git settings written there can run on the host. The agent can also read its own login token, send data to any allowed host, and reach services running on the VPS.
