FastMCP 案例九(Docker部署)
1、目录结构

2、在src同级目录创建一个Dockerfile,内容如下:
# 使用官方 Python 3.11 基础镜像 FROM python:3.11-slim # 设置工作目录 WORKDIR /app # 安装依赖(fastmcp 会自动装好 uvicorn、starlette 等) RUN pip install --no-cache-dir fastmcp # 把代码拷进去 COPY src/ /app # 默认用 SSE 方式启动 CMD ["python", "./mcp/server-sse.py"]
3、编译容器
docker build -t mcp-server-sse .
[+] Building 0.2s (9/9) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 349B 0.0s => [internal] load metadata for docker.io/library/python:3.11-slim 0.1s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/4] FROM docker.io/library/python:3.11-slim@sha256:0ce77749ac83174a31d5e107ce0cfa6b28a2fd6b0615e029d9d84b39c48976ee 0.0s => [internal] load build context 0.0s => => transferring context: 422B 0.0s => CACHED [2/4] WORKDIR /app 0.0s => CACHED [3/4] RUN pip install --no-cache-dir fastmcp 0.0s => CACHED [4/4] COPY src/ /app 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:c04b072ba8d205e2092f92c02c1ea50917579dfed8818e70d4bfab6b710dc7d2 0.0s => => naming to docker.io/library/mcp-server-sse
4、运行容器
docker run -p 3002:3002 mcp-server-sse
(mcp) root:~/PrivateProject/fastmcp$ docker run -p 3002:3002 mcp-server-sse /usr/local/lib/python3.11/site-packages/fastmcp/server/server.py:213: DeprecationWarning: Providing `port` when creating a server is deprecated. Provide it when calling `run` or as a global setting instead. self._handle_deprecated_settings( ╭─ FastMCP 2.0 ──────────────────────────────────────────────────────────────╮ │ │ │ _ __ ___ ______ __ __ _____________ ____ ____ │ │ _ __ ___ / ____/___ ______/ /_/ |/ / ____/ __ \ |___ \ / __ \ │ │ _ __ ___ / /_ / __ `/ ___/ __/ /|_/ / / / /_/ / ___/ / / / / / │ │ _ __ ___ / __/ / /_/ (__ ) /_/ / / / /___/ ____/ / __/_/ /_/ / │ │ _ __ ___ /_/ \__,_/____/\__/_/ /_/\____/_/ /_____(_)____/ │ │ │ │ │ │ │ │ 🖥️ Server name: Weather Service │ │ 📦 Transport: SSE │ │ 🔗 Server URL: http://127.0.0.1:3002/sse/ │ │ │ │ 📚 Docs: https://gofastmcp.com │ │ 🚀 Deploy: https://fastmcp.cloud │ │ │ │ 🏎️ FastMCP version: 2.10.6 │ │ 🤝 MCP version: 1.12.3 │ │ │ ╰────────────────────────────────────────────────────────────────────────────╯ [08/01/25 04:06:05] INFO Starting MCP server 'Weather server.py:1448 Service' with transport 'sse' on http://127.0.0.1:3002/sse/ INFO: Started server process [1] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:3002 (Press CTRL+C to quit) ^CINFO: Shutting down ERROR: Cancel 0 running task(s), timeout graceful shutdown exceeded INFO: Waiting for application shutdown. INFO: Application shutdown complete. INFO: Finished server process [1]
5、执行测试
curl http://localhost:3002/sse/
本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/19016661

浙公网安备 33010602011771号