Skip to content

Commit 60ef6fe

Browse files
cliffhallclaude
andcommitted
fix(docker): run every server image as its non-root user
The fetch, git and time images created an app user and chowned the virtualenv to it but never switched to it, and the filesystem, memory and sequentialthinking images never used the node user their base image ships, so all six ran their server as root. Each now ends on USER app or USER node. The Python images install the uv-managed interpreter under /python instead of /root/.local, which the app user cannot read, and give app a home directory. The git image trusts every mounted repository (safe.directory '*'), since a bind mount is rarely owned by app. The memory image gives /app/dist, where its default graph lives, to node. The READMEs cover the gitconfig path, --user on Linux hosts, and the one-time chown of a memory volume an older image created. Closes modelcontextprotocol#4741 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 281b167 commit 60ef6fe

9 files changed

Lines changed: 78 additions & 13 deletions

File tree

‎src/fetch/Dockerfile‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ ENV UV_COMPILE_BYTECODE=1
1010
# Copy from the cache instead of linking since it's a mounted volume
1111
ENV UV_LINK_MODE=copy
1212

13+
# Install the Python that .python-version pins under /python rather than the
14+
# default /root/.local, which the unprivileged runtime user cannot read (/root
15+
# is mode 700). The virtualenv's interpreter links into this directory.
16+
ENV UV_PYTHON_INSTALL_DIR=/python
17+
1318
# Install the project's dependencies using the lockfile and settings
1419
COPY uv.lock pyproject.toml ./
1520
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -25,13 +30,19 @@ FROM python:3.12-slim-bookworm
2530

2631
WORKDIR /app
2732

28-
COPY --from=uv /root/.local /root/.local
33+
COPY --from=uv /python /python
2934

35+
# The server runs as the unprivileged `app` user, not root (#4741), so files it
36+
# writes into a bind mount are not root-owned on the host and the image passes
37+
# `runAsNonRoot` admission policies. The user gets a home directory, where
38+
# HOME points, for any per-user configuration a tool reads.
3039
RUN if ! id -u app >/dev/null 2>&1; then \
31-
useradd -rUM -s /usr/sbin/nologin app; \
40+
useradd -rUm -d /home/app -s /usr/sbin/nologin app; \
3241
fi
3342
COPY --from=uv --chown=app:app /app/.venv /app/.venv
3443

44+
USER app
45+
3546
# Place executables in the environment at the front of the path
3647
ENV PATH="/app/.venv/bin:$PATH"
3748

‎src/filesystem/Dockerfile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ ENV NODE_ENV=production
2222

2323
RUN npm ci --ignore-scripts --omit-dev
2424

25-
ENTRYPOINT ["node", "/app/dist/index.js"]
25+
# Run as the base image's unprivileged `node` user (UID 1000), not root
26+
# (#4741), so the image passes `runAsNonRoot` admission policies and files
27+
# it writes into a bind mount are not root-owned on the host.
28+
USER node
29+
30+
ENTRYPOINT ["node", "/app/dist/index.js"]

‎src/filesystem/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ Note: you can provide sandboxed directories to the server by mounting them to `/
365365
### Docker
366366
Note: all directories must be mounted to `/projects` by default.
367367
368+
Note: the image runs as the unprivileged `node` user (UID 1000), not root. On a Linux host, where a bind mount keeps the host's file ownership, the server can write only where UID 1000 may, and files it creates are owned by UID 1000. If your own UID differs, add `"--user", "<uid>:<gid>"` (your `id -u` and `id -g`) before `mcp/filesystem` so it reads and writes as you. Docker Desktop on macOS and Windows maps ownership to your user and needs neither.
369+
368370
```json
369371
{
370372
"mcpServers": {

‎src/git/Dockerfile‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ ENV UV_COMPILE_BYTECODE=1
1010
# Copy from the cache instead of linking since it's a mounted volume
1111
ENV UV_LINK_MODE=copy
1212

13+
# Install the Python that .python-version pins under /python rather than the
14+
# default /root/.local, which the unprivileged runtime user cannot read (/root
15+
# is mode 700). The virtualenv's interpreter links into this directory.
16+
ENV UV_PYTHON_INSTALL_DIR=/python
17+
1318
# Install the project's dependencies using the lockfile and settings
1419
COPY uv.lock pyproject.toml ./
1520
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -23,18 +28,30 @@ RUN --mount=type=cache,target=/root/.cache/uv \
2328

2429
FROM python:3.12-slim-bookworm
2530

31+
# `safe.directory '*'`: a bind-mounted repository shows up owned by whatever UID
32+
# the host or Docker Desktop maps it to, rarely the unprivileged `app` user the
33+
# server runs as, so git's ownership check would refuse every repository the
34+
# container is given (#4741). The only repositories the container sees are the
35+
# ones its user chose to mount, which is the trust that check stands in for.
2636
RUN apt-get update && apt-get install -y git git-lfs && rm -rf /var/lib/apt/lists/* \
27-
&& git lfs install --system
37+
&& git lfs install --system \
38+
&& git config --system --add safe.directory '*'
2839

2940
WORKDIR /app
3041

31-
COPY --from=uv /root/.local /root/.local
42+
COPY --from=uv /python /python
3243

44+
# The server runs as the unprivileged `app` user, not root (#4741), so files it
45+
# writes into a bind mount are not root-owned on the host and the image passes
46+
# `runAsNonRoot` admission policies. The user gets a home directory, where
47+
# HOME points, for any per-user configuration a tool reads.
3348
RUN if ! id -u app >/dev/null 2>&1; then \
34-
useradd -rUM -s /usr/sbin/nologin app; \
49+
useradd -rUm -d /home/app -s /usr/sbin/nologin app; \
3550
fi
3651
COPY --from=uv --chown=app:app /app/.venv /app/.venv
3752

53+
USER app
54+
3855
# Place executables in the environment at the front of the path
3956
ENV PATH="/app/.venv/bin:$PATH"
4057

‎src/git/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ A tool call's `repo_path` must be inside that repository, and so must the direct
182182
1. The server process's environment: `GIT_AUTHOR_NAME` and `GIT_AUTHOR_EMAIL` for the author, `GIT_COMMITTER_NAME` and `GIT_COMMITTER_EMAIL` for the committer. The author and the committer are resolved separately, so set all four (usually to the same name and email).
183183
2. `user.name` and `user.email` from Git config: the repository's `.git/config`, then `~/.gitconfig`, then `$XDG_CONFIG_HOME/git/config` (by default `~/.config/git/config`), then `/etc/gitconfig`.
184184

185-
Unlike `git commit`, the server does not refuse to commit when neither is set. It falls back to the operating-system user and host name, so a commit made in a container is recorded as something like `root <root@3f2a1b9c8d7e>`. A local install usually finds your own `~/.gitconfig`; a Docker container has none, so give it an identity explicitly.
185+
Unlike `git commit`, the server does not refuse to commit when neither is set. It falls back to the operating-system user and host name, so a commit made in a container is recorded as something like `app <app@3f2a1b9c8d7e>`. A local install usually finds your own `~/.gitconfig`; a Docker container has none, so give it an identity explicitly.
186186

187187
For a local command (`uvx`, `python -m mcp_server_git`), add an `env` block to the server's entry in your client configuration:
188188

@@ -201,12 +201,12 @@ For a local command (`uvx`, `python -m mcp_server_git`), add an `env` block to t
201201
}
202202
```
203203

204-
For Docker, pass the variables to `docker run` with `-e`, as the Docker examples below do. Alternatively, mount your gitconfig read-only in place of the four `-e` pairs. The image runs as `root`, so it goes at `/root/.gitconfig`:
204+
For Docker, pass the variables to `docker run` with `-e`, as the Docker examples below do. Alternatively, mount your gitconfig read-only in place of the four `-e` pairs. The image runs as the unprivileged `app` user, whose home is `/home/app`, so it goes at `/home/app/.gitconfig`:
205205

206206
```json
207207
"args": [
208208
"run", "--rm", "-i",
209-
"--mount", "type=bind,src=/Users/username/.gitconfig,dst=/root/.gitconfig,readonly",
209+
"--mount", "type=bind,src=/Users/username/.gitconfig,dst=/home/app/.gitconfig,readonly",
210210
"--mount", "type=bind,src=/Users/username,dst=/Users/username",
211211
"mcp/git"
212212
]
@@ -234,6 +234,7 @@ Add this to your `claude_desktop_config.json`:
234234

235235
* Note: replace '/Users/username' with a path that you want to be accessible by this tool
236236
* Note: the `-e` lines set the identity `git_commit` records; see [Commit identity](#commit-identity)
237+
* Note: the image runs as the unprivileged `app` user, not root, and trusts every repository mounted into it (`safe.directory '*'`), since a bind mount rarely shows up owned by that user. On a Linux host, where a bind mount keeps the host's file ownership, add `"--user", "1000:1000"` (your own `id -u` and `id -g`) before `mcp/git` so the server can write to the repository and its commits leave files you own; with `--user`, mount a gitconfig at `/.gitconfig` rather than `/home/app/.gitconfig`, or use the `-e` lines. Docker Desktop on macOS and Windows needs neither.
237238

238239
```json
239240
"mcpServers": {

‎src/memory/Dockerfile‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ RUN --mount=type=cache,target=/root/.npm-production npm ci --ignore-scripts --om
1111

1212
FROM node:24-alpine AS release
1313

14-
COPY --from=builder /app/dist /app/dist
14+
# The default memory.jsonl, its lock and its temporary save files live in
15+
# /app/dist (the README mounts a named volume there), so the `node` user the
16+
# server runs as must own it. A new named volume takes this ownership from the
17+
# image when it is first mounted.
18+
COPY --from=builder --chown=node:node /app/dist /app/dist
1519
COPY --from=builder /app/package.json /app/package.json
1620
COPY --from=builder /app/package-lock.json /app/package-lock.json
1721

@@ -21,4 +25,8 @@ WORKDIR /app
2125

2226
RUN npm ci --ignore-scripts --omit-dev
2327

24-
ENTRYPOINT ["node", "dist/index.js"]
28+
# Run as the base image's unprivileged `node` user (UID 1000), not root
29+
# (#4741), so the image passes `runAsNonRoot` admission policies.
30+
USER node
31+
32+
ENTRYPOINT ["node", "dist/index.js"]

‎src/memory/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ Docker:
381381
docker build -t mcp/memory -f src/memory/Dockerfile .
382382
```
383383

384+
The image runs as the unprivileged `node` user, not root. A new named volume mounted at `/app/dist` takes its ownership from the image, but a volume created by an older image, which ran as root, is owned by root, and the server fails to save with `EACCES`. Give such a volume to `node` once (the graph in it is kept):
385+
386+
```sh
387+
docker run --rm -v claude-memory:/app/dist --user root --entrypoint chown mcp/memory -R node:node /app/dist
388+
```
389+
384390
For Awareness: a prior mcp/memory volume contains an index.js file that could be overwritten by the new container. If you are using a docker volume for storage, delete the old docker volume's `index.js` file before starting the new container.
385391

386392
## License

‎src/sequentialthinking/Dockerfile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ WORKDIR /app
2121

2222
RUN npm ci --ignore-scripts --omit-dev
2323

24+
# Run as the base image's unprivileged `node` user (UID 1000), not root
25+
# (#4741), so the image passes `runAsNonRoot` admission policies.
26+
USER node
27+
2428
ENTRYPOINT ["node", "dist/index.js"]

‎src/time/Dockerfile‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ ENV UV_COMPILE_BYTECODE=1
1010
# Copy from the cache instead of linking since it's a mounted volume
1111
ENV UV_LINK_MODE=copy
1212

13+
# Install the Python that .python-version pins under /python rather than the
14+
# default /root/.local, which the unprivileged runtime user cannot read (/root
15+
# is mode 700). The virtualenv's interpreter links into this directory.
16+
ENV UV_PYTHON_INSTALL_DIR=/python
17+
1318
# Install the project's dependencies using the lockfile and settings
1419
COPY uv.lock pyproject.toml ./
1520
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -25,13 +30,19 @@ FROM python:3.12-slim-bookworm
2530

2631
WORKDIR /app
2732

28-
COPY --from=uv /root/.local /root/.local
33+
COPY --from=uv /python /python
2934

35+
# The server runs as the unprivileged `app` user, not root (#4741), so files it
36+
# writes into a bind mount are not root-owned on the host and the image passes
37+
# `runAsNonRoot` admission policies. The user gets a home directory, where
38+
# HOME points, for any per-user configuration a tool reads.
3039
RUN if ! id -u app >/dev/null 2>&1; then \
31-
useradd -rUM -s /usr/sbin/nologin app; \
40+
useradd -rUm -d /home/app -s /usr/sbin/nologin app; \
3241
fi
3342
COPY --from=uv --chown=app:app /app/.venv /app/.venv
3443

44+
USER app
45+
3546
# Place executables in the environment at the front of the path
3647
ENV PATH="/app/.venv/bin:$PATH"
3748

0 commit comments

Comments
 (0)