docker-run.sh•6.21 kB
#!/bin/bash
# CodeCompass MCP Docker Run Script
# Based on patterns from OpenRouter MCP repository
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
CONTAINER_NAME="codecompass-mcp"
IMAGE_NAME="codecompass-mcp:latest"
DETACH=true
REMOVE_EXISTING=false
INTERACTIVE=false
PORT_MAPPING=""
ENV_FILE=""
MOUNT_VOLUMES=""
NETWORK="bridge"
# Function to display usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -n, --name NAME Container name (default: codecompass-mcp)"
echo " -i, --image IMAGE Image to run (default: codecompass-mcp:latest)"
echo " -p, --port PORT Port mapping (e.g., 8080:8080)"
echo " -e, --env KEY=VALUE Environment variable"
echo " --env-file FILE Environment file"
echo " -v, --volume SRC:DEST Volume mapping"
echo " --network NETWORK Network to use (default: bridge)"
echo " --interactive Run in interactive mode"
echo " --foreground Run in foreground (not detached)"
echo " --remove-existing Remove existing container if it exists"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Run with defaults"
echo " $0 --interactive # Run interactively"
echo " $0 --foreground --remove-existing # Run in foreground, remove existing"
echo " $0 -e GITHUB_TOKEN=token -e OPENROUTER_API_KEY=key"
echo " $0 --env-file .env # Use environment file"
echo " $0 -v /host/data:/app/data # Mount volume"
}
# Parse command line arguments
ENV_VARS=""
while [[ $# -gt 0 ]]; do
case $1 in
-n|--name)
CONTAINER_NAME="$2"
shift 2
;;
-i|--image)
IMAGE_NAME="$2"
shift 2
;;
-p|--port)
PORT_MAPPING="$PORT_MAPPING -p $2"
shift 2
;;
-e|--env)
ENV_VARS="$ENV_VARS -e $2"
shift 2
;;
--env-file)
ENV_FILE="$2"
shift 2
;;
-v|--volume)
MOUNT_VOLUMES="$MOUNT_VOLUMES -v $2"
shift 2
;;
--network)
NETWORK="$2"
shift 2
;;
--interactive)
INTERACTIVE=true
DETACH=false
shift
;;
--foreground)
DETACH=false
shift
;;
--remove-existing)
REMOVE_EXISTING=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
echo "Unknown option: $1"
show_usage
exit 1
;;
esac
done
echo -e "${BLUE}CodeCompass MCP Docker Run${NC}"
echo -e "${BLUE}==========================${NC}"
echo "Container: $CONTAINER_NAME"
echo "Image: $IMAGE_NAME"
echo "Network: $NETWORK"
echo "Detached: $DETACH"
echo "Interactive: $INTERACTIVE"
echo ""
# Check if image exists
if ! docker images --format "table {{.Repository}}:{{.Tag}}" | grep -q "$IMAGE_NAME"; then
echo -e "${RED}Error: Image '$IMAGE_NAME' not found${NC}"
echo "Build the image first with: ./scripts/docker-build.sh"
exit 1
fi
# Remove existing container if requested
if [ "$REMOVE_EXISTING" = true ]; then
if docker ps -a --format "table {{.Names}}" | grep -q "^$CONTAINER_NAME$"; then
echo -e "${YELLOW}Removing existing container...${NC}"
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
fi
fi
# Check if container already exists
if docker ps -a --format "table {{.Names}}" | grep -q "^$CONTAINER_NAME$"; then
echo -e "${YELLOW}Container '$CONTAINER_NAME' already exists${NC}"
# Check if it's running
if docker ps --format "table {{.Names}}" | grep -q "^$CONTAINER_NAME$"; then
echo -e "${GREEN}Container is already running${NC}"
echo "Use 'docker logs $CONTAINER_NAME' to see logs"
echo "Use 'docker exec -it $CONTAINER_NAME /bin/bash' to connect"
exit 0
else
echo -e "${YELLOW}Starting existing container...${NC}"
docker start "$CONTAINER_NAME"
echo -e "${GREEN}✓ Container started${NC}"
exit 0
fi
fi
# Build docker run command
DOCKER_CMD="docker run"
if [ "$DETACH" = true ]; then
DOCKER_CMD="$DOCKER_CMD -d"
fi
if [ "$INTERACTIVE" = true ]; then
DOCKER_CMD="$DOCKER_CMD -it"
fi
DOCKER_CMD="$DOCKER_CMD --name $CONTAINER_NAME"
DOCKER_CMD="$DOCKER_CMD --network $NETWORK"
if [ -n "$PORT_MAPPING" ]; then
DOCKER_CMD="$DOCKER_CMD $PORT_MAPPING"
fi
if [ -n "$ENV_VARS" ]; then
DOCKER_CMD="$DOCKER_CMD $ENV_VARS"
fi
if [ -n "$ENV_FILE" ]; then
if [ -f "$ENV_FILE" ]; then
DOCKER_CMD="$DOCKER_CMD --env-file $ENV_FILE"
else
echo -e "${RED}Error: Environment file '$ENV_FILE' not found${NC}"
exit 1
fi
fi
if [ -n "$MOUNT_VOLUMES" ]; then
DOCKER_CMD="$DOCKER_CMD $MOUNT_VOLUMES"
fi
DOCKER_CMD="$DOCKER_CMD $IMAGE_NAME"
if [ "$INTERACTIVE" = true ]; then
DOCKER_CMD="$DOCKER_CMD /bin/bash"
fi
echo -e "${YELLOW}Running container...${NC}"
echo "Command: $DOCKER_CMD"
echo ""
# Execute the command
eval $DOCKER_CMD
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Container started successfully${NC}"
if [ "$DETACH" = true ]; then
echo ""
echo "Container '$CONTAINER_NAME' is running in the background"
echo ""
echo "Useful commands:"
echo " docker logs $CONTAINER_NAME # View logs"
echo " docker logs -f $CONTAINER_NAME # Follow logs"
echo " docker exec -it $CONTAINER_NAME /bin/bash # Connect to container"
echo " docker stop $CONTAINER_NAME # Stop container"
echo " docker rm $CONTAINER_NAME # Remove container"
echo ""
echo "Health check:"
echo " docker exec $CONTAINER_NAME node -e \"console.log('Server is running')\""
fi
else
echo -e "${RED}✗ Failed to start container${NC}"
exit 1
fi