Skip to content

Commit 105bcaa

Browse files
phernandezclaude[bot]groksrc
authored
feat: implement non-root Docker container to fix file ownership issues (#277)
Signed-off-by: Drew Cain <groksrc@gmail.com> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Paul Hernandez <phernandez@users.noreply.github.com> Co-authored-by: Drew Cain <groksrc@gmail.com>
1 parent 74e12eb commit 105bcaa

2 files changed

Lines changed: 62 additions & 18 deletions

File tree

Dockerfile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
FROM python:3.12-slim-bookworm
22

3+
# Build arguments for user ID and group ID (defaults to 1000)
4+
ARG UID=1000
5+
ARG GID=1000
6+
37
# Copy uv from official image
48
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
59

610
# Set environment variables
711
ENV PYTHONUNBUFFERED=1 \
812
PYTHONDONTWRITEBYTECODE=1
913

14+
# Create a group and user with the provided UID/GID
15+
# Check if the GID already exists, if not create appgroup
16+
RUN (getent group ${GID} || groupadd --gid ${GID} appgroup) && \
17+
useradd --uid ${UID} --gid ${GID} --create-home --shell /bin/bash appuser
18+
1019
# Copy the project into the image
1120
ADD . /app
1221

1322
# Sync the project into a new environment, asserting the lockfile is up to date
1423
WORKDIR /app
1524
RUN uv sync --locked
1625

17-
# Create data directory
18-
RUN mkdir -p /app/data
26+
# Create necessary directories and set ownership
27+
RUN mkdir -p /app/data /app/.basic-memory && \
28+
chown -R appuser:${GID} /app
1929

2030
# Set default data directory and add venv to PATH
2131
ENV BASIC_MEMORY_HOME=/app/data \
2232
PATH="/app/.venv/bin:$PATH"
2333

34+
# Switch to the non-root user
35+
USER appuser
36+
2437
# Expose port
2538
EXPOSE 8000
2639

docs/Docker.md

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Basic Memory provides pre-built Docker images on GitHub Container Registry that
1515
--name basic-memory-server \
1616
-p 8000:8000 \
1717
-v /path/to/your/obsidian-vault:/app/data:rw \
18-
-v basic-memory-config:/root/.basic-memory:rw \
18+
-v basic-memory-config:/app/.basic-memory:rw \
1919
ghcr.io/basicmachines-co/basic-memory:latest
2020
```
2121

@@ -30,7 +30,7 @@ Basic Memory provides pre-built Docker images on GitHub Container Registry that
3030
- "8000:8000"
3131
volumes:
3232
- /path/to/your/obsidian-vault:/app/data:rw
33-
- basic-memory-config:/root/.basic-memory:rw
33+
- basic-memory-config:/app/.basic-memory:rw
3434
environment:
3535
- BASIC_MEMORY_DEFAULT_PROJECT=main
3636
restart: unless-stopped
@@ -67,7 +67,7 @@ docker build -t basic-memory .
6767
docker run -d \
6868
--name basic-memory-server \
6969
-v /path/to/your/obsidian-vault:/app/data:rw \
70-
-v basic-memory-config:/root/.basic-memory:rw \
70+
-v basic-memory-config:/app/.basic-memory:rw \
7171
-e BASIC_MEMORY_DEFAULT_PROJECT=main \
7272
basic-memory
7373
```
@@ -86,19 +86,19 @@ Basic Memory requires several volume mounts for proper operation:
8686
8787
2. **Configuration and Database** (Recommended):
8888
```yaml
89-
- basic-memory-config:/root/.basic-memory:rw
89+
- basic-memory-config:/app/.basic-memory:rw
9090
```
9191
Persistent storage for configuration and SQLite database.
9292
93-
You can edit the basic-memory config.json file located in the /root/.basic-memory/config.json after Basic Memory starts.
93+
You can edit the basic-memory config.json file located in the /app/.basic-memory/config.json after Basic Memory starts.
9494
9595
3. **Multiple Projects** (Optional):
9696
```yaml
9797
- /path/to/project1:/app/data/project1:rw
9898
- /path/to/project2:/app/data/project2:rw
9999
```
100100
101-
You can edit the basic-memory config.json file located in the /root/.basic-memory/config.json
101+
You can edit the basic-memory config.json file located in the /app/.basic-memory/config.json
102102
103103
## CLI Commands via Docker
104104
@@ -123,7 +123,7 @@ When using Docker volumes, you'll need to configure projects to point to your mo
123123

124124
1. **Check current configuration:**
125125
```bash
126-
docker exec basic-memory-server cat /root/.basic-memory/config.json
126+
docker exec basic-memory-server cat /app/.basic-memory/config.json
127127
```
128128

129129
2. **Add a project for your mounted volume:**
@@ -184,16 +184,47 @@ environment:
184184

185185
### Linux/macOS
186186

187-
Ensure your knowledge directories have proper permissions:
187+
The Docker container now runs as a non-root user to avoid file ownership issues. By default, the container uses UID/GID 1000, but you can customize this to match your user:
188188

189189
```bash
190-
# Make directories readable/writable
191-
chmod -R 755 /path/to/your/obsidian-vault
190+
# Build with custom UID/GID to match your user
191+
docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) -t basic-memory .
192192
193-
# If using specific user/group
194-
chown -R $USER:$USER /path/to/your/obsidian-vault
193+
# Or use docker-compose with build args
195194
```
196195

196+
**Example docker-compose.yml with custom user:**
197+
```yaml
198+
version: '3.8'
199+
services:
200+
basic-memory:
201+
build:
202+
context: .
203+
dockerfile: Dockerfile
204+
args:
205+
UID: 1000 # Replace with your UID
206+
GID: 1000 # Replace with your GID
207+
container_name: basic-memory-server
208+
ports:
209+
- "8000:8000"
210+
volumes:
211+
- /path/to/your/obsidian-vault:/app/data:rw
212+
- basic-memory-config:/app/.basic-memory:rw
213+
environment:
214+
- BASIC_MEMORY_DEFAULT_PROJECT=main
215+
restart: unless-stopped
216+
```
217+
218+
**Using pre-built images:**
219+
If using the pre-built image from GitHub Container Registry, files will be created with UID/GID 1000. You can either:
220+
221+
1. Change your local directory ownership to match:
222+
```bash
223+
sudo chown -R 1000:1000 /path/to/your/obsidian-vault
224+
```
225+
226+
2. Or build your own image with custom UID/GID as shown above.
227+
197228
### Windows
198229

199230
When using Docker Desktop on Windows, ensure the directories are shared:
@@ -217,7 +248,7 @@ When using Docker Desktop on Windows, ensure the directories are shared:
217248
```
218249

219250
2. **Configuration Not Persisting:**
220-
- Use named volumes for `/root/.basic-memory`
251+
- Use named volumes for `/app/.basic-memory`
221252
- Check volume mount permissions
222253

223254
3. **Network Connectivity:**
@@ -243,10 +274,10 @@ docker-compose logs -f basic-memory
243274
## Security Considerations
244275

245276
1. **Docker Security:**
246-
The container runs as root for simplicity. For production, consider additional security measures.
277+
The container runs as a non-root user (UID/GID 1000 by default) for improved security. You can customize the user ID using build arguments to match your local user.
247278

248279
2. **Volume Permissions:**
249-
Ensure mounted directories have appropriate permissions and don't expose sensitive data.
280+
Ensure mounted directories have appropriate permissions and don't expose sensitive data. With the non-root container, files will be created with the specified user ownership.
250281

251282
3. **Network Security:**
252283
If using HTTP transport, consider using reverse proxy with SSL/TLS and authentication if the endpoint is available on
@@ -288,7 +319,7 @@ For Docker-specific issues:
288319
1. Check the [troubleshooting section](#troubleshooting) above
289320
2. Review container logs: `docker-compose logs basic-memory`
290321
3. Verify volume mounts: `docker inspect basic-memory-server`
291-
4. Test file permissions: `docker exec basic-memory-server ls -la /root`
322+
4. Test file permissions: `docker exec basic-memory-server ls -la /app`
292323

293324
For general Basic Memory support, see the main [README](../README.md)
294325
and [documentation](https://memory.basicmachines.co/).

0 commit comments

Comments
 (0)