MCP Server in Docker: Run stdio Safely or Package Streamable HTTP | MCP Hunter

MCP Server in Docker: Run stdio Safely or Package Streamable HTTP

How to run a third-party MCP server inside Docker with stdio (-i --rm) and how to package your own Streamable HTTP MCP server in a minimal Dockerfile, plus testing with curl.

MCP Hunter team 4 min read

Developers use Docker with MCP servers for two opposite jobs: isolating a third-party stdio server so it cannot read your whole home directory, or packaging your own Streamable HTTP server so it deploys cleanly to Fly.io, Railway, Render or a VPS. The two patterns use different flags and different failure modes.

TL;DR:

  • Running a third-party stdio server: always pass docker run -i --rm (interactive stdin, no -t pseudo-TTY) and mount only the specific directory the server needs, read-only where possible.
  • Never pass -t to a stdio MCP container: a TTY adds carriage returns and control characters that corrupt JSON-RPC framing over stdout.
  • Packaging your own HTTP server: bind to 0.0.0.0 (not 127.0.0.1), expose one port, and test initialize and tools/list against the running container before you deploy.
  • Want pre-packaged images: Docker maintains the Docker MCP Catalog and Toolkit for pulling containerized servers [1].

Pattern 1: Run a stdio MCP server in a container

A local npx or uvx MCP server runs with your shell's full filesystem and environment access. Wrapping it in docker run narrows what it can see to the volumes and environment variables you explicitly pass.

In Claude Desktop, Cursor or VS Code, configure docker as the command and pass -i and --rm (never -t):

{
  "mcpServers": {
    "filesystem": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "--mount",
        "type=bind,src=/path/to/project,dst=/workspace,readonly",
        "mcp/filesystem",
        "/workspace"
      ]
    }
  }
}

Four flags that matter for every stdio container:

Flag What it does What breaks without it
-i (--interactive) Keeps stdin open for JSON-RPC requests Client hangs or closes immediately on initialize
No -t (--tty) Keeps stdout as a clean byte stream ANSI escapes and CRLF break JSON parsing
--rm Removes the container when the client disconnects Stopped containers pile up on every IDE restart
--network none (optional) Blocks outbound network calls for purely local tools A local file tool can still phone home

If the server needs an API key, pass -e API_KEY without a value and set API_KEY in the client's env block, or pass --env-file so secrets do not sit in your shell history. For the full pre-install checklist, see how to vet an MCP server.

Pattern 2: Package your own Streamable HTTP MCP server

If you are building a remote MCP server in Node.js or Python, a container lets you run the same image locally and on any container host. Two things trip up first-time MCP Dockerfiles:

  1. Bind to 0.0.0.0, not localhost. A server listening on 127.0.0.1:3000 inside a container cannot be reached through -p 3000:3000 from the host or a reverse proxy.
  2. Send logs to stderr, not stdout if the same binary ever runs in stdio mode, and keep HTTP health checks pointed at a real endpoint.

A minimal Node.js example for a Streamable HTTP server listening on port 3000:

FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]

Build and run it locally:

docker build -t my-mcp-server .
docker run --rm -p 3000:3000 my-mcp-server

Verify the container before deploying

Once the container is running on port 3000, run the initialize and tools/list requests from test an MCP server with curl against http://127.0.0.1:3000/mcp. Confirm three things:

  • initialize returns HTTP 200 and a protocolVersion (such as 2025-11-25).
  • tools/list returns your tools with descriptions and valid inputSchema objects.
  • If your server uses Mcp-Session-Id, restarting the container does not leave clients stuck with an unhandled session error.

When you push the image to a public host (see how to host an MCP server for Fly.io, Railway and Render pricing), paste the public https:// URL into our MCP connection test to confirm it answers from outside your network.

Browse developer MCP servers

Looking for existing developer tools you can run in Docker or connect to over HTTP? See the developer tools MCP servers category, ranked by community votes with dated records of what each HTTP endpoint returned when we connected. If you packaged your own server, submit it to an open launch week on MCP Hunter.

Sources

  1. Docker MCP Catalog and Toolkit, Docker Docs, read 2 December 2026.