apple / apple/container

MCP Server Zombie Process Accumulation When Switching from Docker

Open
#403 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
49.9k
Forks
1.8k
Avg merge
1d 20h
Merged PRs (30d)
22

Description

# MCP Server Zombie Process Accumulation When Switching from Docker

## Environment
- **OS**: macOS Tahoe 26.0
- **Apple Container Version**: Latest (installed via package)
- **Hardware**: Apple Silicon Mac
- **Use Case**: Model Context Protocol (MCP) servers with Claude Desktop

## Problem Description

When switching from Docker to Apple Container for running MCP (Model Context Protocol) servers, zombie processes accumulate and are not properly cleaned up when sessions end. This leads to system resource exhaustion and MCP server failures.

## Steps to Reproduce

1. Configure Claude Desktop to use containerized MCP servers (originally with Docker)
2. Switch container runtime from `docker` to `container` command
3. Use MCP servers through multiple Claude Desktop sessions
4. Observe process accumulation with: `container list --all`

## Expected Behavior

- Containers should be automatically removed when MCP sessions end (due to `--rm` flag)
- System should maintain clean process state between sessions
- No zombie processes should accumulate

## Actual Behavior

- Containers accumulate without cleanup (observed 170+ containers)
- Many containers show `stopped` state but remain in system
- New MCP server connections begin failing
- JSON parsing errors occur due to communication issues

## Configuration Example

```json
{
"mcpServers": {
"postgres-server": {
"command": "container",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://connection-string"
]
}
}
}
```

## Current Workaround

Manual cleanup resolves the issue:
```bash
container rm $(container list --all --format json | jq -r '.[].configuration.id') --force
```

## Root Cause Analysis

The issue appears to be:
1. MCP servers require `stdin` communication (`-i` flag)
2. When Claude Desktop sessions terminate, container cleanup isn't triggered properly
3. Containers transition to `stopped` state but aren't removed despite `--rm` flag
4. Accumulation eventually causes resource exhaustion and new connection failures

## Additional Context

- Same configuration works properly with Docker (automatic cleanup)
- Issue is specific to Apple Container runtime
- MCP servers are short-lived processes that should clean up automatically
- Problem manifests after switching container runtimes (Docker → Apple Container)

## Suggested Solutions

1. Improve container lifecycle management for stdin-attached processes
2. Ensure `--rm` flag works correctly for MCP-style short-lived containers
3. Add automatic cleanup mechanisms for orphaned containers
4. Provide better debugging tools for container lifecycle issues

## Impact

This affects:
- Claude Desktop users using containerized MCP servers
- Any application using short-lived containers with stdin communication
- System stability due to resource accumulation

## System Information

- Container system shows no obvious errors in logs
- Containers can be manually cleaned up successfully
- Issue is reproducible across multiple sessions

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.