From ed8cc65cfd781025df55d0477be2a83b8ebe8847 Mon Sep 17 00:00:00 2001 From: Mateusz Hajder <6783135+mhajder@users.noreply.github.com> Date: Sun, 3 Aug 2025 12:34:50 +0200 Subject: [PATCH] Add Docker support with Dockerfiles for standard and mcpo-enabled images --- .dockerignore | 29 +++++++++++++++++++++++++++++ Dockerfile | 34 ++++++++++++++++++++++++++++++++++ Dockerfile.mcpo | 45 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 24 ++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Dockerfile.mcpo diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..67fabfc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,29 @@ +# Docker ignore file to reduce build context size +**/__pycache__/ +*.pyc +*.pyo +*.pyd +.Python +*.so +.git +.gitignore +.github +.vscode +.idea +*.md +!README.md +.env +.env.* +.env.example +*.log +.coverage +.pytest_cache +.mypy_cache +dist/ +build/ +*.egg-info/ +docker-compose.yml +Dockerfile* +*.tar +*.zip +.venv/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1586f15 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +FROM python:3.13-alpine3.22 AS builder + +RUN pip install --no-cache-dir --upgrade pip \ + && pip install --no-cache-dir uv + +WORKDIR /app + +RUN --mount=type=cache,target=/root/.cache/uv \ + --mount=type=bind,source=uv.lock,target=uv.lock \ + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ + uv sync --locked --no-install-project --no-dev + +COPY . /app + +RUN --mount=type=cache,target=/root/.cache/uv \ + uv sync --locked --no-dev + + +FROM python:3.13-alpine3.22 +ENV PYTHONUNBUFFERED=1 + +RUN apk add --no-cache ca-certificates \ + && addgroup -g 1000 appuser \ + && adduser -D -u 1000 -G appuser appuser + +COPY --from=builder --chown=appuser:appuser /app /app + +WORKDIR /app + +USER appuser + +ENV PATH="/app/.venv/bin:$PATH" + +CMD ["python", "-u", "server.py"] \ No newline at end of file diff --git a/Dockerfile.mcpo b/Dockerfile.mcpo new file mode 100644 index 0000000..f387b77 --- /dev/null +++ b/Dockerfile.mcpo @@ -0,0 +1,45 @@ +FROM python:3.13-alpine3.22 AS builder + +RUN pip install --no-cache-dir --upgrade pip \ + && pip install --no-cache-dir uv + +WORKDIR /app + +RUN --mount=type=cache,target=/root/.cache/uv \ + --mount=type=bind,source=uv.lock,target=uv.lock \ + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ + uv sync --locked --no-install-project --no-dev \ + && uv pip install mcpo + +COPY . /app + +RUN --mount=type=cache,target=/root/.cache/uv \ + uv sync --locked --no-dev \ + && uv pip install mcpo + + +FROM python:3.13-alpine3.22 +ENV PYTHONUNBUFFERED=1 + +RUN apk add --no-cache ca-certificates \ + && addgroup -g 1000 appuser \ + && adduser -D -u 1000 -G appuser appuser + +COPY --from=builder --chown=appuser:appuser /app /app + +WORKDIR /app + +USER appuser + +ENV PATH="/app/.venv/bin:$PATH" + +HEALTHCHECK \ + --interval=30s \ + --timeout=10s \ + --start-period=30s \ + --retries=3 \ + CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:8000/openapi.json || exit 1 + +EXPOSE 8000 + +CMD ["sh", "-c", "if [ -n \"$MCPO_API_KEY\" ]; then exec mcpo --api-key \"$MCPO_API_KEY\" -- python -u server.py; else exec mcpo -- python -u server.py; fi"] \ No newline at end of file diff --git a/README.md b/README.md index fd263b9..e52be36 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,30 @@ This is a simple read-only [Model Context Protocol](https://modelcontextprotocol > Show me all configuration changes to the core router in the last month ``` +## Docker Usage + +This repository provides two Dockerfiles: + +- `Dockerfile`: Standard image for running the NetBox MCP server. Use this for most deployments, for example in Claude Desktop or VS Code. +- `Dockerfile.mcpo`: Image with [mcpo](https://github.com/open-webui/mcpo) included, mainly for integration with OpenWebUI or other MCP Orchestrators that require the OpenAPI-compatible HTTP MCP server. + +To build and run the standard image: + +```sh +docker build -t netbox-mcp-server:main . +docker run --rm -e NETBOX_URL=... -e NETBOX_TOKEN=... -p 8000:8000 netbox-mcp-server:main +``` + +To build and run the mcpo-enabled image (for OpenWebUI): + +```sh +docker build -f Dockerfile.mcpo -t netbox-mcp-server:mcpo . +docker run --rm -e NETBOX_URL=... -e NETBOX_TOKEN=... -e MCPO_API_KEY=... -p 8000:8000 netbox-mcp-server:mcpo +``` + +> Use the `MCPO_API_KEY` environment variable if want to set a API KEY. + + ## Development Contributions are welcome! Please open an issue or submit a PR.