fix: Docker container cleanup on session end (Issue #66)
- Added proper SIGTERM/SIGINT signal handlers to stdio-wrapper.ts - Removed problematic trap commands from docker-entrypoint.sh - Added STOPSIGNAL directive to Dockerfile for explicit signal handling - Implemented graceful shutdown in MCP server with database cleanup - Added stdin close detection for proper cleanup when Claude Desktop closes the pipe - Containers now properly exit with the --rm flag, preventing accumulation - Added --init flag to all Docker configuration examples - Updated documentation with container lifecycle management best practices - Bumped version to 2.7.20 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -70,22 +70,12 @@ if [ "$(id -u)" = "0" ]; then
|
||||
if [ -d "/app/data" ]; then
|
||||
chown -R nodejs:nodejs /app/data
|
||||
fi
|
||||
# Switch to nodejs user (using Alpine's native su)
|
||||
exec su nodejs -c "$*"
|
||||
# Switch to nodejs user with proper exec chain for signal propagation
|
||||
exec su -s /bin/sh nodejs -c "exec $*"
|
||||
fi
|
||||
|
||||
# Trap signals for graceful shutdown
|
||||
# In stdio mode, don't output anything to stdout as it breaks JSON-RPC
|
||||
if [ "$MCP_MODE" = "stdio" ]; then
|
||||
# Silent trap - no output at all
|
||||
trap 'kill -TERM $PID 2>/dev/null || true' TERM INT EXIT
|
||||
else
|
||||
# In HTTP mode, output to stderr
|
||||
trap 'echo "Shutting down..." >&2; kill -TERM $PID 2>/dev/null' TERM INT EXIT
|
||||
fi
|
||||
|
||||
# Execute the main command in background
|
||||
# In stdio mode, use the wrapper for clean output
|
||||
# Execute the main command directly with exec
|
||||
# This ensures our Node.js process becomes PID 1 and receives signals directly
|
||||
if [ "$MCP_MODE" = "stdio" ]; then
|
||||
# Debug: Log to stderr to check if wrapper exists
|
||||
if [ "$DEBUG_DOCKER" = "true" ]; then
|
||||
@@ -95,6 +85,7 @@ if [ "$MCP_MODE" = "stdio" ]; then
|
||||
|
||||
if [ -f "/app/dist/mcp/stdio-wrapper.js" ]; then
|
||||
# Use the stdio wrapper for clean JSON-RPC output
|
||||
# exec replaces the shell with node process as PID 1
|
||||
exec node /app/dist/mcp/stdio-wrapper.js
|
||||
else
|
||||
# Fallback: run with explicit environment
|
||||
|
||||
Reference in New Issue
Block a user