Files
bookmarksite/scripts/docker-setup.sh
2025-07-20 20:43:06 +02:00

249 lines
6.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Docker-based Bookmark Manager Setup Script for Linux
echo "🐳 Setting up Bookmark Manager with Docker Database..."
echo ""
# Function to check command exists
check_command() {
if ! command -v $1 &> /dev/null; then
echo "$1 is not installed or not in PATH"
return 1
else
echo "$1 found: $(which $1)"
return 0
fi
}
# Check prerequisites
echo "📋 Checking prerequisites..."
echo ""
NODE_OK=false
DOCKER_OK=false
if check_command "node"; then
NODE_VERSION=$(node --version)
echo " Version: $NODE_VERSION"
NODE_OK=true
else
echo " Install with: sudo apt install nodejs npm"
fi
if check_command "npm"; then
NPM_VERSION=$(npm --version)
echo " Version: $NPM_VERSION"
else
echo " npm should come with Node.js"
fi
if check_command "docker"; then
DOCKER_VERSION=$(docker --version)
echo " Version: $DOCKER_VERSION"
DOCKER_OK=true
else
echo " Install with: sudo apt install docker.io"
fi
if docker compose version >/dev/null 2>&1; then
COMPOSE_VERSION=$(docker compose version)
echo "✅ docker compose found"
echo " Version: $COMPOSE_VERSION"
else
echo "❌ docker compose not available"
echo " Make sure you have Docker with Compose plugin installed"
DOCKER_OK=false
fi
echo ""
if [ "$NODE_OK" = false ] || [ "$DOCKER_OK" = false ]; then
echo "❌ Prerequisites not met. Please install missing components:"
echo ""
if [ "$NODE_OK" = false ]; then
echo "Install Node.js:"
echo " sudo apt update"
echo " sudo apt install nodejs npm"
fi
if [ "$DOCKER_OK" = false ]; then
echo "Install Docker:"
echo " sudo apt update"
echo " sudo apt install docker.io docker-compose-plugin"
echo " sudo systemctl start docker"
echo " sudo usermod -aG docker \$USER"
echo " # Then logout and login again"
fi
exit 1
fi
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "❌ Docker is not running. Starting Docker..."
sudo systemctl start docker
sleep 2
if ! docker info >/dev/null 2>&1; then
echo "❌ Failed to start Docker. Please run:"
echo " sudo systemctl start docker"
echo " sudo usermod -aG docker \$USER"
echo " # Then logout and login again"
exit 1
fi
fi
echo "✅ Docker is running"
# Navigate to backend directory
if [ ! -d "backend" ]; then
echo "❌ Backend directory not found. Are you in the correct directory?"
exit 1
fi
cd backend
# Check if docker-compose.yml exists
if [ ! -f "docker-compose.yml" ]; then
echo "❌ docker-compose.yml not found in backend directory"
exit 1
fi
# Start PostgreSQL with Docker
echo "🐳 Starting PostgreSQL database with Docker..."
if docker compose up -d; then
echo "✅ Database container started"
# Wait for database to be ready
echo "⏳ Waiting for database to be ready..."
sleep 5
# Check if container is running
if docker compose ps | grep -q "Up"; then
echo "✅ Database is running"
else
echo "❌ Database container failed to start"
docker compose logs
exit 1
fi
else
echo "❌ Failed to start database container"
exit 1
fi
# Install Node.js dependencies
echo "📦 Installing Node.js dependencies..."
if npm install; then
echo "✅ Dependencies installed successfully"
else
echo "❌ Failed to install dependencies"
echo " Try: npm cache clean --force && npm install"
exit 1
fi
# Create environment file
if [ ! -f .env ]; then
echo "📝 Creating environment file..."
if cp .env.example .env; then
echo "✅ Environment file created"
# Update .env with Docker database settings
echo "🔧 Configuring environment for Docker database..."
# Create a properly configured .env file
cat > .env << 'EOF'
NODE_ENV=development
PORT=3001
# Docker PostgreSQL settings
DB_HOST=localhost
DB_PORT=5432
DB_NAME=bookmark_manager
DB_USER=postgres
DB_PASSWORD=password
DB_SSL=false
# JWT Configuration - CHANGE THIS IN PRODUCTION
JWT_SECRET=bookmark_manager_docker_jwt_secret_key_change_in_production_2024
JWT_EXPIRES_IN=24h
# Email Configuration (optional)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_SECURE=false
EMAIL_USER=
EMAIL_PASSWORD=
EMAIL_FROM=
# Application Configuration
ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
BASE_URL=http://localhost:3001
# Security Configuration
BCRYPT_SALT_ROUNDS=12
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
AUTH_RATE_LIMIT_MAX=5
EOF
echo "✅ Environment configured for Docker database"
else
echo "❌ Failed to create environment file"
exit 1
fi
else
echo "✅ Environment file already exists"
fi
# Create test database
echo "🗄️ Creating test database..."
if docker exec bookmark_postgres psql -U postgres -d bookmark_manager -c "CREATE DATABASE bookmark_manager_test;" 2>/dev/null; then
echo "✅ Test database created"
else
echo " Test database may already exist (this is OK)"
fi
# Test database connection
echo "🔍 Testing database connection..."
sleep 2
if npm run db:status >/dev/null 2>&1; then
echo "✅ Database connection successful"
else
echo "⚠️ Database connection test failed, but continuing..."
fi
# Initialize database tables
echo "🗄️ Initializing database tables..."
if npm run db:init; then
echo "✅ Database tables initialized successfully"
else
echo "❌ Database initialization failed"
echo ""
echo "Try running manually:"
echo " npm run db:diagnostics"
echo " npm run db:init"
exit 1
fi
echo ""
echo "🎉 Docker setup complete!"
echo ""
echo "Database is running in Docker container: bookmark_postgres"
echo ""
echo "To start the application:"
echo " cd backend"
echo " npm run dev"
echo ""
echo "Then open http://localhost:3001 in your browser"
echo ""
echo "The backend now serves the frontend files automatically!"
echo ""
echo "Useful Docker commands:"
echo " docker compose ps - Check container status"
echo " docker compose logs postgres - View database logs"
echo " docker compose down - Stop database"
echo " docker compose up -d - Start database"
echo ""
echo "Database commands:"
echo " npm run db:status - Check database connection"
echo " npm run db:init - Initialize/reset database"
echo " npm test - Run tests"
echo ""