Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Add Docker database and app management endpoints

Juan Denisbf237c698d7c
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py
index 25c5fffa..a24b0467 100644
--- a/backend/app/api/apps.py
+++ b/backend/app/api/apps.py
@@ -2,6 +2,8 @@ from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from app import db
from app.models import Application, User
+from app.services.docker_service import DockerService
+from app.services.log_service import LogService
apps_bp = Blueprint('apps', __name__)
@@ -148,7 +150,12 @@ def start_app(app_id):
if user.role != 'admin' and app.user_id != current_user_id:
return jsonify({'error': 'Access denied'}), 403
- # TODO: Actually start the application service
+ # Handle Docker apps
+ if app.app_type == 'docker' and app.root_path:
+ result = DockerService.compose_up(app.root_path, detach=True)
+ if not result.get('success'):
+ return jsonify({'error': result.get('error', 'Failed to start containers')}), 400
+
app.status = 'running'
db.session.commit()
@@ -171,7 +178,12 @@ def stop_app(app_id):
if user.role != 'admin' and app.user_id != current_user_id:
return jsonify({'error': 'Access denied'}), 403
- # TODO: Actually stop the application service
+ # Handle Docker apps
+ if app.app_type == 'docker' and app.root_path:
+ result = DockerService.compose_down(app.root_path)
+ if not result.get('success'):
+ return jsonify({'error': result.get('error', 'Failed to stop containers')}), 400
+
app.status = 'stopped'
db.session.commit()
@@ -194,7 +206,12 @@ def restart_app(app_id):
if user.role != 'admin' and app.user_id != current_user_id:
return jsonify({'error': 'Access denied'}), 403
- # TODO: Actually restart the application service
+ # Handle Docker apps
+ if app.app_type == 'docker' and app.root_path:
+ result = DockerService.compose_restart(app.root_path)
+ if not result.get('success'):
+ return jsonify({'error': result.get('error', 'Failed to restart containers')}), 400
+
app.status = 'running'
db.session.commit()
@@ -202,3 +219,81 @@ def restart_app(app_id):
'message': 'Application restarted',
'app': app.to_dict()
}), 200
+
+
+@apps_bp.route('/<int:app_id>/logs', methods=['GET'])
+@jwt_required()
+def get_app_logs(app_id):
+ """Get logs for a specific application."""
+ current_user_id = get_jwt_identity()
+ user = User.query.get(current_user_id)
+ app = Application.query.get(app_id)
+
+ if not app:
+ return jsonify({'error': 'Application not found'}), 404
+
+ if user.role != 'admin' and app.user_id != current_user_id:
+ return jsonify({'error': 'Access denied'}), 403
+
+ lines = request.args.get('lines', 100, type=int)
+ log_type = request.args.get('type', 'all')
+
+ # For Docker apps, get docker compose logs
+ if app.app_type == 'docker' and app.root_path:
+ result = LogService.get_docker_app_logs(app.name, app.root_path, lines)
+ return jsonify(result), 200 if result.get('success') else 400
+
+ # For other apps, get nginx logs
+ result = LogService.get_app_logs(app.name, log_type, lines)
+ return jsonify(result), 200 if result.get('success') else 400
+
+
+@apps_bp.route('/<int:app_id>/status', methods=['GET'])
+@jwt_required()
+def get_app_status(app_id):
+ """Get real-time status for a Docker application."""
+ current_user_id = get_jwt_identity()
+ user = User.query.get(current_user_id)
+ app = Application.query.get(app_id)
+
+ if not app:
+ return jsonify({'error': 'Application not found'}), 404
+
+ if user.role != 'admin' and app.user_id != current_user_id:
+ return jsonify({'error': 'Access denied'}), 403
+
+ if app.app_type == 'docker' and app.root_path:
+ # Get container status from Docker
+ containers = DockerService.compose_ps(app.root_path)
+
+ # Determine overall status
+ running_count = sum(1 for c in containers if c.get('status', '').startswith('Up'))
+ total_count = len(containers)
+
+ if total_count == 0:
+ actual_status = 'stopped'
+ elif running_count == total_count:
+ actual_status = 'running'
+ elif running_count > 0:
+ actual_status = 'partial'
+ else:
+ actual_status = 'stopped'
+
+ # Update DB if status changed
+ if app.status != actual_status and actual_status in ['running', 'stopped']:
+ app.status = actual_status
+ db.session.commit()
+
+ return jsonify({
+ 'status': actual_status,
+ 'containers': containers,
+ 'running': running_count,
+ 'total': total_count
+ }), 200
+
+ return jsonify({
+ 'status': app.status,
+ 'containers': [],
+ 'running': 1 if app.status == 'running' else 0,
+ 'total': 1
+ }), 200
diff --git a/backend/app/api/databases.py b/backend/app/api/databases.py
index 35e2c71e..82c11f91 100644
--- a/backend/app/api/databases.py
+++ b/backend/app/api/databases.py
@@ -1,6 +1,6 @@
from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
-from app.models import User
+from app.models import User, Application
from app.services.database_service import DatabaseService
databases_bp = Blueprint('databases', __name__)
@@ -606,6 +606,112 @@ def get_sqlite_tables():
return jsonify({'tables': tables}), 200
+# ==================== DOCKER CONTAINER DATABASES ====================
+
+@databases_bp.route('/docker', methods=['GET'])
+@jwt_required()
+def list_docker_databases():
+ """List all databases running in Docker containers.
+
+ This includes MySQL/MariaDB containers from template-deployed apps.
+ """
+ containers = DatabaseService.list_docker_mysql_containers()
+ return jsonify({'containers': containers}), 200
+
+
+@databases_bp.route('/docker/app/<int:app_id>', methods=['GET'])
+@jwt_required()
+def get_app_databases(app_id):
+ """Get database info for a Docker application.
+
+ This reads the docker-compose.yml and .env files to find database
+ containers and their credentials.
+ """
+ current_user_id = get_jwt_identity()
+ user = User.query.get(current_user_id)
+ app = Application.query.get(app_id)
+
+ if not app:
+ return jsonify({'error': 'Application not found'}), 404
+
+ if user.role != 'admin' and app.user_id != current_user_id:
+ return jsonify({'error': 'Access denied'}), 403
+
+ if app.app_type != 'docker' or not app.root_path:
+ return jsonify({'error': 'Application is not a Docker app'}), 400
+
+ db_info = DatabaseService.get_app_database_info(app.name, app.root_path)
+
+ if not db_info:
+ return jsonify({'databases': [], 'message': 'No database containers found'}), 200
+
+ return jsonify({'databases': db_info}), 200
+
+
+@databases_bp.route('/docker/<container>/databases', methods=['GET'])
+@jwt_required()
+def list_docker_container_databases(container):
+ """List databases in a Docker MySQL container."""
+ user = request.args.get('user', 'root')
+ password = request.headers.get('X-DB-Password')
+
+ databases = DatabaseService.docker_mysql_list_databases(container, user, password)
+ return jsonify({'databases': databases}), 200
+
+
+@databases_bp.route('/docker/<container>/<database>/tables', methods=['GET'])
+@jwt_required()
+def get_docker_database_tables(container, database):
+ """Get tables in a Docker MySQL database."""
+ user = request.args.get('user', 'root')
+ password = request.headers.get('X-DB-Password')
+
+ tables = DatabaseService.docker_mysql_get_tables(container, database, user, password)
+ return jsonify({'tables': tables}), 200
+
+
+@databases_bp.route('/docker/<container>/<database>/query', methods=['POST'])
+@jwt_required()
+def execute_docker_query(container, database):
+ """Execute a query on a Docker MySQL database.
+
+ Request body:
+ query: SQL query to execute
+ readonly: If true, only allow SELECT/SHOW/DESCRIBE/EXPLAIN (default: true)
+
+ Security: Readonly mode is enforced by default. Admin role required to disable.
+ """
+ data = request.get_json()
+
+ if not data or 'query' not in data:
+ return jsonify({'error': 'query is required'}), 400
+
+ query = data['query']
+ readonly = data.get('readonly', True)
+ user = data.get('user', 'root')
+ password = request.headers.get('X-DB-Password') or data.get('password')
+
+ # Only admins can disable readonly mode
+ if not readonly:
+ current_user_id = get_jwt_identity()
+ user_obj = User.query.get(current_user_id)
+ if not user_obj or user_obj.role != 'admin':
+ return jsonify({'error': 'Admin access required to execute write queries'}), 403
+
+ result = DatabaseService.docker_mysql_execute_query(
+ container_name=container,
+ database=database,
+ query=query,
+ user=user,
+ password=password,
+ readonly=readonly,
+ timeout=30,
+ max_rows=1000
+ )
+
+ return jsonify(result), 200 if result['success'] else 400
+
+
# ==================== UTILITY ====================
@databases_bp.route('/generate-password', methods=['GET'])
diff --git a/backend/app/api/templates.py b/backend/app/api/templates.py
index 140ced77..59b5e186 100644
--- a/backend/app/api/templates.py
+++ b/backend/app/api/templates.py
@@ -67,6 +67,9 @@ def get_template(template_id):
template = result['template']
# Add processed variable info - handle both list and dict formats
+ # Auto-generated types that users don't need to fill in
+ auto_generated_types = ['port', 'password', 'random', 'uuid']
+
variables = []
raw_vars = template.get('variables', [])
@@ -74,25 +77,31 @@ def get_template(template_id):
# New list format: [{name: 'PORT', type: 'port', ...}, ...]
for var in raw_vars:
if isinstance(var, dict):
+ var_type = var.get('type', 'string')
variables.append({
'name': var.get('name', ''),
'description': var.get('description', ''),
- 'type': var.get('type', 'string'),
+ 'type': var_type,
'default': var.get('default', ''),
'required': var.get('required', False),
- 'options': var.get('options', None)
+ 'options': var.get('options', None),
+ 'auto_generated': var_type in auto_generated_types,
+ 'hidden': var_type == 'port' # Ports are hidden from users
})
elif isinstance(raw_vars, dict):
# Old dict format: {PORT: {type: 'port', ...}, ...}
for var_name, var_config in raw_vars.items():
if isinstance(var_config, dict):
+ var_type = var_config.get('type', 'string')
variables.append({
'name': var_name,
'description': var_config.get('description', ''),
- 'type': var_config.get('type', 'string'),
+ 'type': var_type,
'default': var_config.get('default', ''),
'required': var_config.get('required', False),
- 'options': var_config.get('options', None)
+ 'options': var_config.get('options', None),
+ 'auto_generated': var_type in auto_generated_types,
+ 'hidden': var_type == 'port'
})
return jsonify({
diff --git a/backend/app/services/database_service.py b/backend/app/services/database_service.py
index adcc8d69..009f8597 100644
--- a/backend/app/services/database_service.py
+++ b/backend/app/services/database_service.py
@@ -2,6 +2,7 @@ import subprocess
import os
import secrets
import string
+import json
from datetime import datetime
@@ -992,3 +993,335 @@ class DatabaseService:
"""Generate a secure random password."""
alphabet = string.ascii_letters + string.digits + '!@#$%^&*'
return ''.join(secrets.choice(alphabet) for _ in range(length))
+
+ # ==================== DOCKER CONTAINER DATABASES ====================
+
+ @staticmethod
+ def list_docker_mysql_containers():
+ """Find all Docker containers running MySQL/MariaDB."""
+ try:
+ # Get all running containers
+ result = subprocess.run(
+ ['docker', 'ps', '--format', '{{json .}}'],
+ capture_output=True, text=True
+ )
+ if result.returncode != 0:
+ return []
+
+ containers = []
+ mysql_images = ['mysql', 'mariadb', 'percona']
+
+ for line in result.stdout.strip().split('\n'):
+ if not line:
+ continue
+ container = json.loads(line)
+ image = container.get('Image', '').lower()
+
+ # Check if it's a MySQL-like container
+ if any(img in image for img in mysql_images):
+ containers.append({
+ 'id': container.get('ID'),
+ 'name': container.get('Names'),
+ 'image': container.get('Image'),
+ 'status': container.get('Status'),
+ 'ports': container.get('Ports'),
+ 'type': 'mysql'
+ })
+
+ return containers
+ except Exception:
+ return []
+
+ @staticmethod
+ def docker_mysql_execute(container_name, query, database=None, user='root', password=None):
+ """Execute a MySQL query inside a Docker container."""
+ try:
+ cmd = ['docker', 'exec', container_name, 'mysql', '-u', user]
+
+ if password:
+ cmd.append(f'-p{password}')
+
+ if database:
+ cmd.extend(['-D', database])
+
+ cmd.extend(['-e', query])
+
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
+ return {
+ 'success': result.returncode == 0,
+ 'output': result.stdout,
+ 'error': result.stderr if result.returncode != 0 else None
+ }
+ except subprocess.TimeoutExpired:
+ return {'success': False, 'error': 'Query timed out'}
+ except Exception as e:
+ return {'success': False, 'error': str(e)}
+
+ @staticmethod
+ def docker_mysql_list_databases(container_name, user='root', password=None):
+ """List databases in a Docker MySQL container."""
+ result = DatabaseService.docker_mysql_execute(
+ container_name,
+ "SHOW DATABASES;",
+ user=user,
+ password=password
+ )
+ if not result['success']:
+ return []
+
+ databases = []
+ system_dbs = ['information_schema', 'mysql', 'performance_schema', 'sys']
+ for line in result['output'].strip().split('\n')[1:]:
+ db_name = line.strip()
+ if db_name and db_name not in system_dbs:
+ databases.append({
+ 'name': db_name,
+ 'type': 'docker_mysql',
+ 'container': container_name
+ })
+ return databases
+
+ @staticmethod
+ def docker_mysql_get_tables(container_name, database, user='root', password=None):
+ """Get tables in a Docker MySQL database."""
+ result = DatabaseService.docker_mysql_execute(
+ container_name,
+ "SHOW TABLES;",
+ database=database,
+ user=user,
+ password=password
+ )
+ if not result['success']:
+ return []
+
+ tables = []
+ for line in result['output'].strip().split('\n')[1:]:
+ table_name = line.strip()
+ if table_name:
+ # Get row count
+ count_result = DatabaseService.docker_mysql_execute(
+ container_name,
+ f"SELECT COUNT(*) FROM `{table_name}`;",
+ database=database,
+ user=user,
+ password=password
+ )
+ rows = 0
+ if count_result['success']:
+ try:
+ rows = int(count_result['output'].strip().split('\n')[1])
+ except (IndexError, ValueError):
+ pass
+
+ tables.append({
+ 'name': table_name,
+ 'rows': rows
+ })
+ return tables
+
+ @staticmethod
+ def docker_mysql_execute_query(container_name, database, query, user='root', password=None,
+ readonly=True, timeout=30, max_rows=1000):
+ """Execute a query against a Docker MySQL container and return structured results."""
+ import time
+
+ # Security check for readonly mode
+ if readonly and not DatabaseService._is_readonly_query(query):
+ return {
+ 'success': False,
+ 'error': 'Only SELECT, SHOW, DESCRIBE, and EXPLAIN queries are allowed in readonly mode'
+ }
+
+ try:
+ start_time = time.time()
+
+ cmd = ['docker', 'exec', container_name, 'mysql', '-u', user]
+ if password:
+ cmd.append(f'-p{password}')
+ cmd.extend([
+ '-D', database,
+ '-e', query,
+ '--batch'
+ ])
+
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
+ execution_time = time.time() - start_time
+
+ if result.returncode != 0:
+ return {
+ 'success': False,
+ 'error': result.stderr.strip() if result.stderr else 'Query execution failed'
+ }
+
+ # Parse the output
+ lines = result.stdout.strip().split('\n') if result.stdout.strip() else []
+
+ if not lines:
+ return {
+ 'success': True,
+ 'columns': [],
+ 'rows': [],
+ 'row_count': 0,
+ 'execution_time': execution_time,
+ 'truncated': False
+ }
+
+ # First line is headers
+ columns = lines[0].split('\t') if lines else []
+ data_lines = lines[1:] if len(lines) > 1 else []
+
+ # Parse rows
+ rows = []
+ for i, line in enumerate(data_lines):
+ if i >= max_rows:
+ break
+ values = line.split('\t')
+ values = [None if v == 'NULL' else v for v in values]
+ rows.append(values)
+
+ return {
+ 'success': True,
+ 'columns': columns,
+ 'rows': rows,
+ 'row_count': len(rows),
+ 'total_rows': len(data_lines),
+ 'execution_time': round(execution_time, 4),
+ 'truncated': len(data_lines) > max_rows
+ }
+
+ except subprocess.TimeoutExpired:
+ return {
+ 'success': False,
+ 'error': f'Query timed out after {timeout} seconds'
+ }
+ except Exception as e:
+ return {'success': False, 'error': str(e)}
+
+ @staticmethod
+ def get_app_database_info(app_name, app_path):
+ """Get database info for a Docker app by reading its compose file."""
+ compose_path = os.path.join(app_path, 'docker-compose.yml') if app_path else None
+ env_path = os.path.join(app_path, '.env') if app_path else None
+
+ if not compose_path or not os.path.exists(compose_path):
+ return None
+
+ try:
+ import yaml
+
+ with open(compose_path, 'r') as f:
+ compose = yaml.safe_load(f)
+
+ # Read .env file for passwords
+ env_vars = {}
+ if env_path and os.path.exists(env_path):
+ with open(env_path, 'r') as f:
+ for line in f:
+ if '=' in line and not line.strip().startswith('#'):
+ key, value = line.strip().split('=', 1)
+ env_vars[key] = value
+
+ db_info = []
+ services = compose.get('services', {})
+
+ for service_name, service_config in services.items():
+ image = service_config.get('image', '').lower()
+
+ # Check for MySQL containers
+ if any(db in image for db in ['mysql', 'mariadb', 'percona']):
+ container_name = service_config.get('container_name', f'{app_name}_{service_name}')
+ environment = service_config.get('environment', [])
+
+ # Extract credentials from environment
+ root_password = None
+ db_user = None
+ db_password = None
+ db_name = None
+
+ if isinstance(environment, list):
+ for env in environment:
+ if isinstance(env, str) and '=' in env:
+ key, value = env.split('=', 1)
+ elif isinstance(env, str):
+ key = env
+ value = env_vars.get(env.replace('${', '').replace('}', ''), '')
+ else:
+ continue
+
+ # Resolve ${VAR} syntax
+ if value.startswith('${') and value.endswith('}'):
+ var_name = value[2:-1]
+ value = env_vars.get(var_name, value)
+
+ if 'ROOT_PASSWORD' in key:
+ root_password = value
+ elif 'MYSQL_PASSWORD' in key and 'ROOT' not in key:
+ db_password = value
+ elif 'MYSQL_USER' in key:
+ db_user = value
+ elif 'MYSQL_DATABASE' in key:
+ db_name = value
+ elif isinstance(environment, dict):
+ for key, value in environment.items():
+ if isinstance(value, str) and value.startswith('${') and value.endswith('}'):
+ var_name = value[2:-1]
+ value = env_vars.get(var_name, value)
+
+ if 'ROOT_PASSWORD' in key:
+ root_password = value
+ elif 'MYSQL_PASSWORD' in key and 'ROOT' not in key:
+ db_password = value
+ elif 'MYSQL_USER' in key:
+ db_user = value
+ elif 'MYSQL_DATABASE' in key:
+ db_name = value
+
+ db_info.append({
+ 'type': 'mysql',
+ 'container': container_name,
+ 'service': service_name,
+ 'image': image,
+ 'database': db_name,
+ 'user': db_user or 'root',
+ 'password': db_password or root_password,
+ 'root_password': root_password
+ })
+
+ # Check for PostgreSQL containers
+ elif 'postgres' in image:
+ container_name = service_config.get('container_name', f'{app_name}_{service_name}')
+ environment = service_config.get('environment', [])
+
+ pg_user = 'postgres'
+ pg_password = None
+ pg_db = None
+
+ if isinstance(environment, list):
+ for env in environment:
+ if isinstance(env, str) and '=' in env:
+ key, value = env.split('=', 1)
+ if value.startswith('${') and value.endswith('}'):
+ var_name = value[2:-1]
+ value = env_vars.get(var_name, value)
+
+ if 'POSTGRES_PASSWORD' in key:
+ pg_password = value
+ elif 'POSTGRES_USER' in key:
+ pg_user = value
+ elif 'POSTGRES_DB' in key:
+ pg_db = value
+
+ db_info.append({
+ 'type': 'postgresql',
+ 'container': container_name,
+ 'service': service_name,
+ 'image': image,
+ 'database': pg_db,
+ 'user': pg_user,
+ 'password': pg_password
+ })
+
+ return db_info if db_info else None
+
+ except Exception as e:
+ return None