Skip to content

Commit b9a7c53

Browse files
fix: run cognee in Docker [COG-1961] (#775) (#779)
<!-- .github/pull_request_template.md --> ## Description Resolve issue with .venv being broken when using docker compose with Cognee ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- <!-- .github/pull_request_template.md --> ## Description <!-- Provide a clear description of the changes in this PR --> ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. Co-authored-by: Igor Ilic <30923996+dexters1@users.noreply.github.com>
1 parent 49f2e51 commit b9a7c53

File tree

9 files changed

+134
-14
lines changed

9 files changed

+134
-14
lines changed

.github/workflows/dockerhub-mcp.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ jobs:
3636
id: build
3737
uses: docker/build-push-action@v5
3838
with:
39-
context: cognee-mcp
39+
context: .
4040
platforms: linux/amd64,linux/arm64
4141
push: true
42+
file: cognee-mcp/Dockerfile
4243
tags: ${{ steps.meta.outputs.tags }}
4344
labels: ${{ steps.meta.outputs.labels }}
4445
cache-from: type=registry,ref=cognee/cognee-mcp:buildcache

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ENV DEBUG=${DEBUG}
2525
ENV PIP_NO_CACHE_DIR=true
2626
ENV PATH="${PATH}:/root/.poetry/bin"
2727

28-
RUN apt-get update && apt-get install
28+
RUN apt-get update
2929

3030
RUN apt-get install -y \
3131
gcc \
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Add default user
2+
3+
Revision ID: 482cd6517ce4
4+
Revises: 8057ae7329c2
5+
Create Date: 2024-10-16 22:17:18.634638
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
11+
from sqlalchemy.util import await_only
12+
13+
from cognee.modules.users.methods import create_default_user, delete_user
14+
15+
16+
# revision identifiers, used by Alembic.
17+
revision: str = "482cd6517ce4"
18+
down_revision: Union[str, None] = "8057ae7329c2"
19+
branch_labels: Union[str, Sequence[str], None] = None
20+
depends_on: Union[str, Sequence[str], None] = "8057ae7329c2"
21+
22+
23+
def upgrade() -> None:
24+
await_only(create_default_user())
25+
26+
27+
def downgrade() -> None:
28+
await_only(delete_user("default_user@example.com"))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Initial migration
2+
3+
Revision ID: 8057ae7329c2
4+
Revises:
5+
Create Date: 2024-10-02 12:55:20.989372
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
from sqlalchemy.util import await_only
11+
from cognee.infrastructure.databases.relational import get_relational_engine
12+
13+
# revision identifiers, used by Alembic.
14+
revision: str = "8057ae7329c2"
15+
down_revision: Union[str, None] = None
16+
branch_labels: Union[str, Sequence[str], None] = None
17+
depends_on: Union[str, Sequence[str], None] = None
18+
19+
20+
def upgrade() -> None:
21+
db_engine = get_relational_engine()
22+
await_only(db_engine.create_database())
23+
24+
25+
def downgrade() -> None:
26+
db_engine = get_relational_engine()
27+
await_only(db_engine.delete_database())

cognee-mcp/Dockerfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN apt-get update && apt-get install -y \
2222
libpq-dev
2323

2424
# Copy pyproject.toml and lockfile first for better caching
25-
COPY pyproject.toml uv.lock ./
25+
COPY ./cognee-mcp/pyproject.toml ./cognee-mcp/uv.lock ./cognee-mcp/entrypoint.sh ./
2626

2727
# Install the project's dependencies using the lockfile and settings
2828
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -31,19 +31,25 @@ RUN --mount=type=cache,target=/root/.cache/uv \
3131
# Copy .env file first if it exists (for environment variables)
3232
COPY .env* /app/
3333

34+
# Copy Alembic configuration
35+
COPY alembic.ini /app/alembic.ini
36+
COPY alembic/ /app/alembic
37+
3438
# Then, add the rest of the project source code and install it
3539
# Installing separately from its dependencies allows optimal layer caching
36-
COPY . /app
40+
COPY ./cognee-mcp /app
3741
RUN --mount=type=cache,target=/root/.cache/uv \
3842
uv sync --frozen --no-dev --no-editable
3943

4044
FROM python:3.12-slim-bookworm
4145

4246
WORKDIR /app
43-
47+
4448
COPY --from=uv /root/.local /root/.local
4549
COPY --from=uv /app /app
4650

51+
RUN chmod +x /app/entrypoint.sh
52+
4753
# Place executables in the environment at the front of the path
4854
ENV PATH="/app/.venv/bin:$PATH"
4955

@@ -54,4 +60,4 @@ ENV PYTHONPATH=/app
5460

5561
# Use the application name from pyproject.toml for normal operation
5662
# For testing, we'll override this with a direct command
57-
ENTRYPOINT ["cognee"]
63+
ENTRYPOINT ["/app/entrypoint.sh"]

cognee-mcp/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,22 @@ To apply new changes while developing cognee you need to do:
8484
1. `poetry lock` in cognee folder
8585
2. `uv sync --dev --all-extras --reinstall`
8686
3. `mcp dev src/server.py`
87+
88+
### Development
89+
In order to use local cognee build, run in root of the cognee repo:
90+
```bash
91+
poetry build -o ./cognee-mcp/sources
92+
```
93+
After the build process is done, change the cognee library dependency inside the `cognee-mcp/pyproject.toml` from
94+
```toml
95+
cognee[postgres,codegraph,gemini,huggingface]==0.1.38
96+
```
97+
to
98+
```toml
99+
cognee[postgres,codegraph,gemini,huggingface]
100+
```
101+
After that add the following snippet to the same file (`cognee-mcp/pyproject.toml`).
102+
```toml
103+
[tool.uv.sources]
104+
cognee = { path = "sources/cognee-0.1.38-py3-none-any.whl" }
105+
```

cognee-mcp/entrypoint.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -e # Exit on error
4+
echo "Debug mode: $DEBUG"
5+
echo "Environment: $ENVIRONMENT"
6+
7+
# Run Alembic migrations with proper error handling.
8+
# Note on UserAlreadyExists error handling:
9+
# During database migrations, we attempt to create a default user. If this user
10+
# already exists (e.g., from a previous deployment or migration), it's not a
11+
# critical error and shouldn't prevent the application from starting. This is
12+
# different from other migration errors which could indicate database schema
13+
# inconsistencies and should cause the startup to fail. This check allows for
14+
# smooth redeployments and container restarts while maintaining data integrity.
15+
echo "Running database migrations..."
16+
MIGRATION_OUTPUT=$(uv run alembic upgrade head 2>&1) || {
17+
if [[ $MIGRATION_OUTPUT == *"UserAlreadyExists"* ]] || [[ $MIGRATION_OUTPUT == *"User default_user@example.com already exists"* ]]; then
18+
echo "Warning: Default user already exists, continuing startup..."
19+
else
20+
echo "Migration failed with unexpected error:"
21+
echo "$MIGRATION_OUTPUT"
22+
exit 1
23+
fi
24+
}
25+
26+
echo "Starting Cognee MCP Server"
27+
28+
# Add startup delay to ensure DB is ready
29+
sleep 2
30+
31+
# Modified Gunicorn startup with error handling
32+
if [ "$ENVIRONMENT" = "dev" ] || [ "$ENVIRONMENT" = "local" ]; then
33+
if [ "$DEBUG" = "true" ]; then
34+
echo "Waiting for the debugger to attach..."
35+
exec python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m cognee
36+
else
37+
exec cognee
38+
fi
39+
else
40+
exec cognee
41+
fi

cognee-mcp/src/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ async def list_tools() -> list[types.Tool]:
3636
},
3737
"graph_model_file": {
3838
"type": "string",
39-
"description": "The path to the graph model file",
39+
"description": "The path to the graph model file (Optional)",
4040
},
4141
"graph_model_name": {
4242
"type": "string",
43-
"description": "The name of the graph model",
43+
"description": "The name of the graph model (Optional)",
4444
},
4545
},
4646
"required": ["text"],

docker-compose.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
context: .
88
dockerfile: Dockerfile
99
volumes:
10-
- .:/app
11-
- /app/cognee-frontend/ # Ignore frontend code
10+
- ./cognee:/app/cognee
11+
- .env:/app/.env
1212
environment:
1313
- DEBUG=false # Change to true if debugging
1414
- HOST=0.0.0.0
@@ -26,10 +26,8 @@ services:
2626
cpus: "4.0"
2727
memory: 8GB
2828

29-
# NOTE: Frontend is a work in progress and is not intended to be used by users yet.
30-
# If you want to use Cognee with a UI environment you can run the cognee-gui.py script or
31-
# integrate the Cognee MCP Server to Cursor / Claude Desktop / Visual Studio Code ( through Cline/Roo )
32-
29+
# NOTE: Frontend is a work in progress and supports minimum amount of features required to be functional.
30+
# If you want to use Cognee with a UI environment you can integrate the Cognee MCP Server into Cursor / Claude Desktop / Visual Studio Code (through Cline/Roo)
3331
frontend:
3432
container_name: frontend
3533
profiles:

0 commit comments

Comments
 (0)