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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
feat(services): first-class managed volumes for apps
Juan Deniscbbd410bc8c4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 050ad6ee..ce3ffd38 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,12 @@ awaiting a stable release:### Added+- **Per-app managed volumes** — first-class, tracked persistent storage for a+ service. Attach a named Docker volume at a chosen container path under+ Settings → Storage; it survives redeploys and is visible with live+ present/size state, instead of a fragile relative bind mount+ (`./mysql-data:/var/lib/mysql`). Detaching keeps the data by default; wiping is+ blocked while the app runs. API under `/api/v1/apps/<id>/volumes`.- **Private container registries** — store credentials once under Settings →Connections (GHCR, Docker Hub, GitLab, ECR, or any generic registry) andServerKit runs `docker login` before pulling a private image, then logs out.diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex 9634ee9b..0eb6d946 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -122,6 +122,10 @@ def create_app(config_name=None):app.register_blueprint(apps_bp, url_prefix='/api/v1/services', name='services')app.register_blueprint(domains_bp, url_prefix='/api/v1/domains')app.register_blueprint(private_urls_bp, url_prefix='/api/v1/apps')+ # Per-app managed volumes — same dual mount as apps (/apps + /services alias).+ from app.api.app_volumes import app_volumes_bp+ app.register_blueprint(app_volumes_bp, url_prefix='/api/v1/apps')+ app.register_blueprint(app_volumes_bp, url_prefix='/api/v1/services', name='app_volumes_services')# Register blueprints - Systemfrom app.api.system import system_bpdiff --git a/backend/app/api/app_volumes.py b/backend/app/api/app_volumes.pynew file mode 100644index 00000000..3ee7d2cd--- /dev/null+++ b/backend/app/api/app_volumes.py@@ -0,0 +1,94 @@+"""Per-app managed volume API — attach/list/detach first-class persistent+volumes for an application. Mounted under ``/api/v1/apps`` alongside the main+apps blueprint (routes are ``/<app_id>/volumes*``).+"""+from flask import Blueprint, request, jsonify+from flask_jwt_extended import jwt_required, get_jwt_identity++from app.models import Application, User+from app.services.resource_grant_service import ResourceGrantService+from app.services.volume_service import VolumeService, VolumeError++app_volumes_bp = Blueprint('app_volumes', __name__)+++def _load_app_for(app_id, *, write):+ """Resolve (user, app) with the right ACL, or an (error_response, status)."""+ user = User.query.get(get_jwt_identity())+ app = Application.query.get(app_id)+ if not app:+ return None, None, (jsonify({'error': 'Application not found'}), 404)+ allowed = (ResourceGrantService.can_edit_app(user, app) if write+ else ResourceGrantService.can_access_app(user, app))+ if not allowed:+ return None, None, (jsonify({'error': 'Access denied'}), 403)+ return user, app, None+++@app_volumes_bp.route('/<int:app_id>/volumes', methods=['GET'])+@jwt_required()+def list_volumes(app_id):+ _user, app, err = _load_app_for(app_id, write=False)+ if err:+ return err+ volumes = [v.to_dict(live=live) for v, live in VolumeService.list_for_app(app)]+ return jsonify({'volumes': volumes})+++@app_volumes_bp.route('/<int:app_id>/volumes', methods=['POST'])+@jwt_required()+def attach_volume(app_id):+ _user, app, err = _load_app_for(app_id, write=True)+ if err:+ return err+ data = request.get_json() or {}+ try:+ volume = VolumeService.create(+ app,+ name=data.get('name'),+ mount_path=data.get('mount_path'),+ driver=data.get('driver') or 'local',+ read_only=bool(data.get('read_only')),+ )+ except VolumeError as e:+ return jsonify({'error': str(e)}), 400+ live = None+ return jsonify({'volume': volume.to_dict(live=live)}), 201+++@app_volumes_bp.route('/<int:app_id>/volumes/<int:volume_id>', methods=['DELETE'])+@jwt_required()+def detach_volume(app_id, volume_id):+ _user, app, err = _load_app_for(app_id, write=True)+ if err:+ return err+ volume = VolumeService.get(volume_id)+ if not volume or volume.application_id != app.id:+ return jsonify({'error': 'Volume not found'}), 404+ wipe = str(request.args.get('wipe', '')).lower() in ('1', 'true', 'yes')+ try:+ VolumeService.delete(volume, wipe=wipe)+ except VolumeError as e:+ return jsonify({'error': str(e)}), 400+ return jsonify({'success': True, 'wiped': wipe})+++@app_volumes_bp.route('/<int:app_id>/volumes/convert', methods=['POST'])+@jwt_required()+def convert_bind_mount(app_id):+ """Create a managed volume and copy an existing host directory's contents+ into it. (Deviates from the plan's ``/<vid>/convert`` — a convert creates a+ new volume, so there is no prior vid.)"""+ _user, app, err = _load_app_for(app_id, write=True)+ if err:+ return err+ data = request.get_json() or {}+ host_path = data.get('host_path')+ mount_path = data.get('mount_path')+ if not host_path or not mount_path:+ return jsonify({'error': 'host_path and mount_path are required'}), 400+ try:+ volume = VolumeService.convert_bind_mount(app, host_path, mount_path, name=data.get('name'))+ except VolumeError as e:+ return jsonify({'error': str(e)}), 400+ return jsonify({'volume': volume.to_dict()}), 201diff --git a/backend/app/models/__init__.py b/backend/app/models/__init__.pyindex 145dcb43..160637cd 100644--- a/backend/app/models/__init__.py+++ b/backend/app/models/__init__.py@@ -30,6 +30,7 @@ from app.models.oauth_identity import OAuthIdentityfrom app.models.source_connection import SourceConnectionfrom app.models.registrar_connection import RegistrarConnectionfrom app.models.container_registry import ContainerRegistry+from app.models.app_volume import AppVolumefrom app.models.api_key import ApiKeyfrom app.models.api_usage import ApiUsageLog, ApiUsageSummaryfrom app.models.event_subscription import EventSubscription, EventDelivery@@ -73,7 +74,7 @@ __all__ = ['WordPressCustomPlugin', 'WordPressSitePlugin','EnvironmentActivity', 'PromotionJob', 'SanitizationProfile','EmailDomain', 'EmailAccount', 'EmailAlias', 'EmailForwardingRule', 'DNSProviderConfig', 'EmailRelayConfig',- 'OAuthIdentity', 'SourceConnection', 'RegistrarConnection', 'ContainerRegistry', 'ApiKey', 'ApiUsageLog', 'ApiUsageSummary',+ 'OAuthIdentity', 'SourceConnection', 'RegistrarConnection', 'ContainerRegistry', 'AppVolume', 'ApiKey', 'ApiUsageLog', 'ApiUsageSummary','EventSubscription', 'EventDelivery', 'Invitation','ServerAlertThreshold', 'MetricAlert','AgentPlugin', 'AgentPluginInstall',diff --git a/backend/app/models/app_volume.py b/backend/app/models/app_volume.pynew file mode 100644index 00000000..a6f83980--- /dev/null+++ b/backend/app/models/app_volume.py@@ -0,0 +1,76 @@+from datetime import datetime++from app import db+++class AppVolume(db.Model):+ """A first-class, tracked persistent volume attached to an application.++ Replaces ad-hoc **relative bind mounts** (``./mysql-data:/var/lib/mysql``)+ with a **named Docker volume** that survives redeploys, is visible in the UI,+ and is backup-addressable. v1 is the ``local`` driver only; ``driver`` is+ modeled so networked drivers (NFS/CSI/cloud block) can grow later.++ The real Docker volume is namespaced ``serverkit-app-{app_id}-{slug}`` so it+ is unambiguously ServerKit-owned and never collides with a host admin's+ volumes.+ """++ __tablename__ = 'app_volumes'++ id = db.Column(db.Integer, primary_key=True)+ application_id = db.Column(+ db.Integer, db.ForeignKey('applications.id', ondelete='CASCADE'),+ nullable=False, index=True,+ )+ name = db.Column(db.String(120), nullable=False) # operator label, e.g. "uploads"+ docker_volume_name = db.Column(db.String(200), nullable=False, unique=True)+ mount_path = db.Column(db.String(500), nullable=False) # absolute path inside the container+ driver = db.Column(db.String(40), nullable=False, default='local')+ read_only = db.Column(db.Boolean, nullable=False, default=False)+ size_bytes = db.Column(db.BigInteger, nullable=True) # denormalized last-measured size+ created_at = db.Column(db.DateTime, default=datetime.utcnow)+ updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)++ # Deleting the app removes its volume rows (ORM cascade); the underlying+ # Docker volumes are intentionally left on disk so an app delete never nukes+ # data silently — prune them explicitly with a wipe.+ application = db.relationship(+ 'Application',+ backref=db.backref('volumes', cascade='all, delete-orphan'),+ )++ __table_args__ = (+ db.UniqueConstraint('application_id', 'mount_path', name='uq_app_volume_mount'),+ )++ def mount_spec(self):+ """The ``docker run -v`` / compose short-syntax spec for this volume,+ e.g. ``serverkit-app-5-uploads:/var/www/html/wp-content/uploads:ro``."""+ spec = f'{self.docker_volume_name}:{self.mount_path}'+ if self.read_only:+ spec += ':ro'+ return spec++ def to_dict(self, live=None):+ data = {+ 'id': self.id,+ 'application_id': self.application_id,+ 'name': self.name,+ 'docker_volume_name': self.docker_volume_name,+ 'mount_path': self.mount_path,+ 'driver': self.driver,+ 'read_only': self.read_only,+ 'size_bytes': self.size_bytes,+ 'created_at': self.created_at.isoformat() if self.created_at else None,+ 'updated_at': self.updated_at.isoformat() if self.updated_at else None,+ }+ if live is not None:+ data['present'] = live.get('present')+ data['mountpoint'] = live.get('mountpoint')+ if live.get('size_bytes') is not None:+ data['size_bytes'] = live.get('size_bytes')+ return data++ def __repr__(self):+ return f'<AppVolume {self.docker_volume_name} -> {self.mount_path}>'diff --git a/backend/app/services/deployment_service.py b/backend/app/services/deployment_service.pyindex 8034ef9e..e9ef78ea 100644--- a/backend/app/services/deployment_service.py+++ b/backend/app/services/deployment_service.py@@ -279,6 +279,11 @@ class DeploymentService:if not pull.get('success'):return {'success': False, 'error': pull.get('error', 'Registry pull failed')}+ # Attach any managed volumes so app data persists across redeploys+ # (each returns a `name:/mount[:ro]` spec for `docker run -v`).+ from app.services.volume_service import VolumeService+ volumes = VolumeService.run_args(app)+# Run new containerif log_callback:log_callback(f"Starting container {container_name}...")@@ -287,6 +292,7 @@ class DeploymentService:image=image_tag,name=container_name,ports=ports if ports else None,+ volumes=volumes if volumes else None,env=env if env else None,restart_policy='unless-stopped',detach=Truediff --git a/backend/app/services/docker_service.py b/backend/app/services/docker_service.pyindex 443075d5..91b4d890 100644--- a/backend/app/services/docker_service.py+++ b/backend/app/services/docker_service.py@@ -811,6 +811,39 @@ class DockerService:except Exception as e:return {'success': False, 'error': str(e)}+ @staticmethod+ def inspect_volume(name):+ """Live state of a named volume: {'present', 'mountpoint', 'driver'}."""+ try:+ result = subprocess.run(+ ['docker', 'volume', 'inspect', name],+ capture_output=True, text=True+ )+ if result.returncode != 0:+ return {'present': False, 'mountpoint': None, 'driver': None}+ data = json.loads(result.stdout or '[]')+ info = data[0] if data else {}+ return {'present': True, 'mountpoint': info.get('Mountpoint'),+ 'driver': info.get('Driver')}+ except Exception:+ return {'present': False, 'mountpoint': None, 'driver': None}++ @staticmethod+ def containers_using_volume(name, running_only=False):+ """Names of containers referencing a volume. ``running_only`` limits it to+ currently-running containers (the guard for a safe wipe)."""+ try:+ cmd = ['docker', 'ps']+ if not running_only:+ cmd.append('-a')+ cmd += ['--filter', f'volume={name}', '--format', '{{.Names}}']+ result = subprocess.run(cmd, capture_output=True, text=True)+ if result.returncode != 0:+ return []+ return [n for n in result.stdout.strip().split('\n') if n]+ except Exception:+ return []+# ==================== DOCKER COMPOSE ====================@classmethod@@ -1145,8 +1178,15 @@ class DockerService:return []@staticmethod- def create_docker_app(app_path, app_name, image, ports=None, volumes=None, env=None):- """Create a Docker-based application with docker-compose."""+ def create_docker_app(app_path, app_name, image, ports=None, volumes=None, env=None,+ named_volumes=None):+ """Create a Docker-based application with docker-compose.++ ``named_volumes`` is a list of managed Docker volume names to declare as+ top-level (``external: false``) volumes — Compose requires the top-level+ declaration for any named volume referenced by a service. Callers pass the+ matching ``name:/mount`` specs in ``volumes`` (see AppVolume.mount_spec).+ """try:os.makedirs(app_path, exist_ok=True)@@ -1171,6 +1211,11 @@ class DockerService:if env:compose['services'][app_name]['environment'] = env+ if named_volumes:+ # Top-level named volumes so a redeploy reuses the same volume+ # instead of a fresh anonymous one.+ compose['volumes'] = {name: {} for name in named_volumes}+compose_path = os.path.join(app_path, 'docker-compose.yml')with open(compose_path, 'w') as f:yaml.dump(compose, f, default_flow_style=False)diff --git a/backend/app/services/volume_service.py b/backend/app/services/volume_service.pynew file mode 100644index 00000000..436cfab4--- /dev/null+++ b/backend/app/services/volume_service.py@@ -0,0 +1,206 @@+"""Per-app managed volumes — first-class, tracked persistent storage.++Wraps the raw ``docker volume`` plumbing in ``DockerService`` and links each+volume to an ``Application`` so it survives redeploys, is visible in the UI, and+is backup-addressable. The real Docker volume is namespaced+``serverkit-app-{app_id}-{slug}``.++Two mount surfaces are supported:+ - single-container deploys — :meth:`run_args` returns the ``-v`` specs the+ ``docker run`` (``DockerService.run_container``) appends;+ - compose stacks — :meth:`compose_fragment` returns the service-level specs ++ the top-level ``volumes:`` declaration a generated stack needs.++Safety: :meth:`delete` only removes the underlying Docker volume when+``wipe=True`` **and** nothing is running off it, so a detach never nukes data by+accident.+"""+import logging+import os+import re+import subprocess++from app import db+from app.models.app_volume import AppVolume+from app.services.docker_service import DockerService++logger = logging.getLogger(__name__)+++class VolumeError(Exception):+ """Raised for invalid volume operations (bad mount path, duplicate, unsafe wipe)."""+++class VolumeService:+ """CRUD + docker orchestration for per-app managed volumes."""++ # A throwaway image for size measurement / data copies. Small + ubiquitous.+ HELPER_IMAGE = 'alpine'++ # ── helpers ──+ @staticmethod+ def _slug(name):+ slug = re.sub(r'[^a-z0-9]+', '-', (name or '').strip().lower()).strip('-')+ return slug or 'data'++ @classmethod+ def _unique_docker_name(cls, app_id, name):+ """``serverkit-app-{id}-{slug}``, suffixed if that collides with an+ existing managed volume name (two labels can slugify the same)."""+ base = f'serverkit-app-{app_id}-{cls._slug(name)}'+ candidate = base+ n = 2+ while AppVolume.query.filter_by(docker_volume_name=candidate).first() is not None:+ candidate = f'{base}-{n}'+ n += 1+ return candidate++ @staticmethod+ def _normalize_mount_path(mount_path):+ mount_path = (mount_path or '').strip()+ if not mount_path.startswith('/'):+ raise VolumeError('mount_path must be an absolute container path (e.g. /var/www/html/uploads)')+ # Normalize trailing slash (but keep root '/')+ return mount_path.rstrip('/') or '/'++ # ── CRUD ──+ @classmethod+ def get(cls, volume_id):+ if not volume_id:+ return None+ return AppVolume.query.get(volume_id)++ @classmethod+ def list_for_app(cls, app, with_live=True):+ """DB rows for an app, each optionally joined with live Docker state+ (present / mountpoint)."""+ rows = (AppVolume.query+ .filter_by(application_id=app.id)+ .order_by(AppVolume.created_at.asc())+ .all())+ if not with_live:+ return [(v, None) for v in rows]+ return [(v, DockerService.inspect_volume(v.docker_volume_name)) for v in rows]++ @classmethod+ def create(cls, app, name, mount_path, driver='local', read_only=False):+ """Create the namespaced Docker volume and persist the tracking row.+ Refuses a duplicate mount path for the same app."""+ name = (name or '').strip()+ if not name:+ raise VolumeError('name is required')+ mount_path = cls._normalize_mount_path(mount_path)++ existing = AppVolume.query.filter_by(application_id=app.id, mount_path=mount_path).first()+ if existing is not None:+ raise VolumeError(f'A volume is already mounted at {mount_path}')++ docker_volume_name = cls._unique_docker_name(app.id, name)+ result = DockerService.create_volume(docker_volume_name, driver=driver)+ if not result.get('success'):+ raise VolumeError(result.get('error') or 'Failed to create Docker volume')++ volume = AppVolume(+ application_id=app.id,+ name=name,+ docker_volume_name=docker_volume_name,+ mount_path=mount_path,+ driver=driver or 'local',+ read_only=bool(read_only),+ )+ db.session.add(volume)+ db.session.commit()+ return volume++ @classmethod+ def delete(cls, volume, wipe=False):+ """Detach a volume. Removes only the tracking row by default; with+ ``wipe=True`` also ``docker volume rm`` — but never while a container is+ running off it (loud refusal), so a detach can't destroy live data."""+ if wipe:+ app = volume.application+ if app is not None and app.status == 'running':+ raise VolumeError('Stop the app before wiping a volume')+ running = DockerService.containers_using_volume(volume.docker_volume_name, running_only=True)+ if running:+ raise VolumeError(f'Volume is in use by a running container ({", ".join(running)}); stop it first')+ rm = DockerService.remove_volume(volume.docker_volume_name)+ if not rm.get('success'):+ raise VolumeError(rm.get('error') or 'Failed to remove Docker volume')++ db.session.delete(volume)+ db.session.commit()++ @classmethod+ def measure(cls, volume):+ """Best-effort ``du -sb`` of the volume via a throwaway container. Linux+ only; returns the size in bytes (and caches it) or None."""+ if os.name != 'posix':+ return None+ try:+ result = subprocess.run(+ ['docker', 'run', '--rm', '-v', f'{volume.docker_volume_name}:/data:ro',+ cls.HELPER_IMAGE, 'du', '-sb', '/data'],+ capture_output=True, text=True, timeout=120,+ )+ if result.returncode == 0 and result.stdout.strip():+ size = int(result.stdout.split()[0])+ volume.size_bytes = size+ db.session.commit()+ return size+ except Exception as e: # pragma: no cover - best-effort/defensive+ logger.debug('Volume measure failed for %s: %s', volume.docker_volume_name, e)+ return None++ @classmethod+ def convert_bind_mount(cls, app, host_path, mount_path, name=None):+ """Guided migration: create a managed volume and copy an existing host+ directory's contents into it (so a fragile relative bind mount becomes a+ tracked named volume). Returns the new AppVolume. The stack still has to+ be pointed at the volume — the caller/UI handles that explicitly rather+ than rewriting a live compose file silently."""+ mount_path = cls._normalize_mount_path(mount_path)+ volume = cls.create(app, name or os.path.basename(mount_path.rstrip('/')) or 'data', mount_path)++ host_path = os.path.abspath(host_path or '')+ if host_path and os.path.isdir(host_path) and os.name == 'posix':+ try:+ subprocess.run(+ ['docker', 'run', '--rm',+ '-v', f'{host_path}:/src:ro',+ '-v', f'{volume.docker_volume_name}:/dst',+ cls.HELPER_IMAGE, 'sh', '-c', 'cp -a /src/. /dst/'],+ capture_output=True, text=True, timeout=600,+ )+ except Exception as e: # pragma: no cover - best-effort/defensive+ logger.warning('Bind-mount data copy failed for %s: %s', volume.docker_volume_name, e)+ return volume++ # ── mount wiring ──+ @classmethod+ def run_args(cls, app):+ """``-v`` specs for a single-container ``docker run`` (list of strings)."""+ return [v.mount_spec() for v in AppVolume.query.filter_by(application_id=app.id).all()]++ @classmethod+ def compose_fragment(cls, app):+ """Compose pieces for an app's managed volumes:+ ``{'service': [<name:/mount[:ro]>, …], 'top_level': {<name>: {}, …}}``.+ A generated stack references ``service`` from the service and declares+ ``top_level`` at the compose root."""+ rows = AppVolume.query.filter_by(application_id=app.id).all()+ return {+ 'service': [v.mount_spec() for v in rows],+ 'top_level': {v.docker_volume_name: {} for v in rows},+ }++ @classmethod+ def host_paths_for_app(cls, app):+ """Live host mountpoints of an app's managed volumes, for backup+ addressability. Skips volumes not currently present on the host."""+ paths = []+ for volume in AppVolume.query.filter_by(application_id=app.id).all():+ live = DockerService.inspect_volume(volume.docker_volume_name)+ if live.get('present') and live.get('mountpoint'):+ paths.append(live['mountpoint'])+ return pathsdiff --git a/backend/migrations/versions/043_app_volumes.py b/backend/migrations/versions/043_app_volumes.pynew file mode 100644index 00000000..db6f6307--- /dev/null+++ b/backend/migrations/versions/043_app_volumes.py@@ -0,0 +1,54 @@+"""Per-app managed volumes.++Adds:+- app_volumes (first-class tracked persistent volumes attached to an application,+ replacing fragile relative bind mounts)++Idempotent: MigrationService runs _fix_missing_columns() + db.create_all() on boot+before Alembic, so guard on the live schema.++Revision ID: 043_app_volumes+Revises: 042_container_registries+Create Date: 2026-07-01+"""+from alembic import op+import sqlalchemy as sa++revision = '043_app_volumes'+down_revision = '042_container_registries'+branch_labels = None+depends_on = None+++def upgrade():+ conn = op.get_bind()+ inspector = sa.inspect(conn)+ existing = set(inspector.get_table_names())++ if 'app_volumes' not in existing:+ op.create_table(+ 'app_volumes',+ sa.Column('id', sa.Integer, nullable=False),+ sa.Column('application_id', sa.Integer, nullable=False),+ sa.Column('name', sa.String(120), nullable=False),+ sa.Column('docker_volume_name', sa.String(200), nullable=False),+ sa.Column('mount_path', sa.String(500), nullable=False),+ sa.Column('driver', sa.String(40), nullable=False, server_default='local'),+ sa.Column('read_only', sa.Boolean, nullable=False, server_default=sa.false()),+ sa.Column('size_bytes', sa.BigInteger, nullable=True),+ sa.Column('created_at', sa.DateTime, nullable=True),+ sa.Column('updated_at', sa.DateTime, nullable=True),+ sa.PrimaryKeyConstraint('id'),+ sa.ForeignKeyConstraint(['application_id'], ['applications.id'], ondelete='CASCADE'),+ sa.UniqueConstraint('docker_volume_name', name='uq_app_volume_docker_name'),+ sa.UniqueConstraint('application_id', 'mount_path', name='uq_app_volume_mount'),+ )+ op.create_index('ix_app_volumes_application_id', 'app_volumes', ['application_id'])+++def downgrade():+ conn = op.get_bind()+ inspector = sa.inspect(conn)+ existing = set(inspector.get_table_names())+ if 'app_volumes' in existing:+ op.drop_table('app_volumes')diff --git a/backend/tests/test_app_volumes.py b/backend/tests/test_app_volumes.pynew file mode 100644index 00000000..0a67c378--- /dev/null+++ b/backend/tests/test_app_volumes.py@@ -0,0 +1,247 @@+"""Per-app managed volumes — model, service, deploy wiring, and API.++Covers the proving points from docs/plans/APP_MANAGED_VOLUMES.md:+- create persists a row AND a namespaced Docker volume+- a duplicate mount path is refused+- delete(wipe=False) never calls `docker volume rm`+- delete(wipe=True) refuses while a container runs off the volume+- the single-container deploy passes `-v <docker_volume>:<mount>` to docker run+- a generated compose stack emits a top-level `volumes:` block (named volume,+ not a relative bind mount)+"""+import subprocess++import pytest+import yaml++from app import db+from app.models import Application, AppVolume, User+from app.services.docker_service import DockerService+from app.services.volume_service import VolumeService, VolumeError+++class _FakeProc:+ def __init__(self, returncode=0, stdout='', stderr=''):+ self.returncode = returncode+ self.stdout = stdout+ self.stderr = stderr+++def owner(app):+ user = User(email='[email protected]', username='volowner',+ password_hash='x', role=User.ROLE_ADMIN, is_active=True)+ db.session.add(user)+ db.session.commit()+ return user+++def docker_app(app, owner):+ application = Application(name='vol-app', app_type='docker', status='stopped',+ root_path='/tmp/vol-app', user_id=owner.id)+ db.session.add(application)+ db.session.commit()+ return application+++def fake_docker(monkeypatch):+ """Stub the docker volume plumbing; record created/removed volume names."""+ created, removed = [], []+ monkeypatch.setattr(DockerService, 'create_volume',+ staticmethod(lambda name, driver='local': created.append(name) or {'success': True, 'volume_name': name}))+ monkeypatch.setattr(DockerService, 'remove_volume',+ staticmethod(lambda name, force=False: removed.append(name) or {'success': True}))+ monkeypatch.setattr(DockerService, 'inspect_volume',+ staticmethod(lambda name: {'present': True, 'mountpoint': f'/var/lib/docker/volumes/{name}/_data', 'driver': 'local'}))+ monkeypatch.setattr(DockerService, 'containers_using_volume',+ staticmethod(lambda name, running_only=False: []))+ return {'created': created, 'removed': removed}+++# ── create / list ────────────────────────────────────────────────────────────++def test_create_persists_row_and_namespaced_docker_volume(docker_app, fake_docker):+ volume = VolumeService.create(docker_app, name='uploads', mount_path='/var/www/html/wp-content/uploads')++ assert volume.id is not None+ assert volume.docker_volume_name == f'serverkit-app-{docker_app.id}-uploads'+ assert fake_docker['created'] == [volume.docker_volume_name]+ assert AppVolume.query.filter_by(application_id=docker_app.id).count() == 1+++def test_duplicate_mount_path_is_refused(docker_app, fake_docker):+ VolumeService.create(docker_app, name='data', mount_path='/data')+ with pytest.raises(VolumeError):+ VolumeService.create(docker_app, name='data2', mount_path='/data')+++def test_mount_path_must_be_absolute(docker_app, fake_docker):+ with pytest.raises(VolumeError):+ VolumeService.create(docker_app, name='rel', mount_path='relative/path')+++def test_slug_collision_gets_unique_docker_name(docker_app, fake_docker):+ v1 = VolumeService.create(docker_app, name='db data', mount_path='/a')+ v2 = VolumeService.create(docker_app, name='db-data', mount_path='/b')+ assert v1.docker_volume_name != v2.docker_volume_name # same slug, disambiguated+++def test_list_for_app_joins_live_state(docker_app, fake_docker):+ VolumeService.create(docker_app, name='uploads', mount_path='/uploads')+ rows = VolumeService.list_for_app(docker_app)+ assert len(rows) == 1+ v, live = rows[0]+ assert live['present'] is True+ assert live['mountpoint'].endswith('/_data')+++# ── delete / wipe safety ─────────────────────────────────────────────────────++def test_delete_without_wipe_never_removes_docker_volume(docker_app, fake_docker):+ volume = VolumeService.create(docker_app, name='keep', mount_path='/keep')+ VolumeService.delete(volume, wipe=False)+ assert fake_docker['removed'] == [] # docker volume rm NOT called+ assert AppVolume.query.filter_by(application_id=docker_app.id).count() == 0+++def test_wipe_removes_docker_volume_when_stopped(docker_app, fake_docker):+ volume = VolumeService.create(docker_app, name='gone', mount_path='/gone')+ dv = volume.docker_volume_name+ VolumeService.delete(volume, wipe=True)+ assert fake_docker['removed'] == [dv]+++def test_wipe_refused_while_container_running(docker_app, fake_docker, monkeypatch):+ volume = VolumeService.create(docker_app, name='busy', mount_path='/busy')+ monkeypatch.setattr(DockerService, 'containers_using_volume',+ staticmethod(lambda name, running_only=False: ['serverkit-app-x'] if running_only else []))+ with pytest.raises(VolumeError):+ VolumeService.delete(volume, wipe=True)+ assert fake_docker['removed'] == [] # nothing wiped+ assert AppVolume.query.filter_by(application_id=docker_app.id).count() == 1 # row kept+++def test_wipe_refused_while_app_running(docker_app, fake_docker):+ docker_app.status = 'running'+ db.session.commit()+ volume = VolumeService.create(docker_app, name='live', mount_path='/live')+ with pytest.raises(VolumeError):+ VolumeService.delete(volume, wipe=True)+ assert fake_docker['removed'] == []+++# ── mount wiring: single-container + compose ─────────────────────────────────++def test_run_args_returns_mount_specs(docker_app, fake_docker):+ VolumeService.create(docker_app, name='data', mount_path='/data')+ VolumeService.create(docker_app, name='ro', mount_path='/ro', read_only=True)+ specs = VolumeService.run_args(docker_app)+ assert f'serverkit-app-{docker_app.id}-data:/data' in specs+ assert f'serverkit-app-{docker_app.id}-ro:/ro:ro' in specs+++def test_deploy_docker_mounts_managed_volume(docker_app, fake_docker, monkeypatch):+ """The proving test: a single-container deploy passes -v for each volume."""+ from app.services.deployment_service import DeploymentService+ from app.services.env_service import EnvService+ from app.models.deployment import Deployment++ VolumeService.create(docker_app, name='uploads', mount_path='/uploads')+ docker_app.docker_image = 'nginx:latest'+ deployment = Deployment(app_id=docker_app.id, version=1, version_tag='v1',+ status='deploying', image_tag='nginx:latest',+ deployed_by=docker_app.user_id)+ db.session.add(deployment)+ db.session.commit()++ captured = {}++ def fake_run(cmd, *args, **kwargs):+ if cmd[:2] == ['docker', 'run']:+ captured['cmd'] = cmd+ return _FakeProc(returncode=0, stdout='cid123')++ monkeypatch.setattr(subprocess, 'run', fake_run)+ monkeypatch.setattr(DockerService, 'get_container', staticmethod(lambda *a, **k: None))+ monkeypatch.setattr(EnvService, 'get_effective_env', staticmethod(lambda *a, **k: {}))++ result = DeploymentService._deploy_docker(docker_app, deployment)+ assert result['success'] is True+ cmd = captured['cmd']+ assert '-v' in cmd+ assert f'serverkit-app-{docker_app.id}-uploads:/uploads' in cmd+++def test_compose_generation_emits_top_level_named_volume(tmp_path, docker_app, fake_docker):+ """A generated compose stack references a named volume + declares it at the+ top level — no relative bind mount for that path."""+ VolumeService.create(docker_app, name='db-data', mount_path='/var/lib/mysql')+ frag = VolumeService.compose_fragment(docker_app)+ dv = f'serverkit-app-{docker_app.id}-db-data'+ assert frag['service'] == [f'{dv}:/var/lib/mysql']+ assert dv in frag['top_level']++ app_path = str(tmp_path / 'stack')+ DockerService.create_docker_app(app_path, 'web', 'nginx:latest',+ volumes=frag['service'],+ named_volumes=list(frag['top_level'].keys()))+ with open(f'{app_path}/docker-compose.yml') as f:+ compose = yaml.safe_load(f)++ # Top-level volumes block declares the named volume ...+ assert dv in (compose.get('volumes') or {})+ # ... and the service references it, not a relative ./ bind mount.+ svc_vols = compose['services']['web']['volumes']+ assert f'{dv}:/var/lib/mysql' in svc_vols+ assert not any(str(v).startswith('./') for v in svc_vols)+++def test_convert_bind_mount_creates_managed_volume(docker_app, fake_docker, tmp_path, monkeypatch):+ """convert_bind_mount creates a tracked volume for an existing host dir. The+ data-copy container is best-effort; stub subprocess so the test is hermetic."""+ monkeypatch.setattr(subprocess, 'run',+ lambda *a, **k: _FakeProc(returncode=0))+ src = tmp_path / 'olddata'+ src.mkdir()++ volume = VolumeService.convert_bind_mount(docker_app, str(src), '/var/lib/mysql', name='db')+ assert volume.mount_path == '/var/lib/mysql'+ assert volume.docker_volume_name == f'serverkit-app-{docker_app.id}-db'+ assert AppVolume.query.filter_by(application_id=docker_app.id).count() == 1+++# ── API ──────────────────────────────────────────────────────────────────────++def test_api_attach_list_detach(client, auth_headers, app, fake_docker):+ # auth_headers creates its own admin; make an app owned generically+ owner = User.query.filter_by(username='testadmin').first()+ application = Application(name='api-vol', app_type='docker', status='stopped',+ root_path='/tmp/api-vol', user_id=owner.id)+ db.session.add(application)+ db.session.commit()++ # attach+ resp = client.post(f'/api/v1/apps/{application.id}/volumes', headers=auth_headers,+ json={'name': 'uploads', 'mount_path': '/uploads'})+ assert resp.status_code == 201, resp.get_json()+ vid = resp.get_json()['volume']['id']++ # list (with live state)+ resp = client.get(f'/api/v1/apps/{application.id}/volumes', headers=auth_headers)+ assert resp.status_code == 200+ vols = resp.get_json()['volumes']+ assert len(vols) == 1 and vols[0]['mount_path'] == '/uploads'+ assert vols[0]['present'] is True++ # duplicate mount path -> 400+ resp = client.post(f'/api/v1/apps/{application.id}/volumes', headers=auth_headers,+ json={'name': 'dup', 'mount_path': '/uploads'})+ assert resp.status_code == 400++ # detach (no wipe) -> row gone, docker volume untouched+ resp = client.delete(f'/api/v1/apps/{application.id}/volumes/{vid}', headers=auth_headers)+ assert resp.status_code == 200+ assert fake_docker['removed'] == []+ assert AppVolume.query.filter_by(application_id=application.id).count() == 0diff --git a/frontend/src/components/apps/VolumesPanel.jsx b/frontend/src/components/apps/VolumesPanel.jsxnew file mode 100644index 00000000..6932fc27--- /dev/null+++ b/frontend/src/components/apps/VolumesPanel.jsx@@ -0,0 +1,210 @@+import { useState, useEffect, useCallback } from 'react';+import { HardDrive, Plus, Trash2, AlertTriangle } from 'lucide-react';+import api from '../../services/api';+import { useToast } from '../../contexts/ToastContext';+import { Pill } from '../ds';+import { Button } from '@/components/ui/button';+import { Input } from '@/components/ui/input';+import { Label } from '@/components/ui/label';+import { Checkbox } from '@/components/ui/checkbox';++function formatBytes(bytes) {+ if (bytes == null) return '—';+ if (bytes === 0) return '0 B';+ const units = ['B', 'KB', 'MB', 'GB', 'TB'];+ const i = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024)));+ return `${(bytes / 1024 ** i).toFixed(i ? 1 : 0)} ${units[i]}`;+}++// A single volume row with an inline detach confirm (the "also delete data"+// checkbox is off by default and disabled while the app is running, so a detach+// can never nuke live data by accident).+function VolumeRow({ volume, appRunning, onDetach }) {+ const [confirming, setConfirming] = useState(false);+ const [wipe, setWipe] = useState(false);+ const [busy, setBusy] = useState(false);++ async function detach() {+ setBusy(true);+ try {+ await onDetach(volume, wipe);+ } finally {+ setBusy(false);+ setConfirming(false);+ setWipe(false);+ }+ }++ return (+ <div className="app-volumes__row">+ <div className="app-volumes__cell app-volumes__cell--name">+ <strong>{volume.name}</strong>+ <span className="app-volumes__mono">{volume.mount_path}</span>+ </div>+ <div className="app-volumes__cell">{formatBytes(volume.size_bytes)}</div>+ <div className="app-volumes__cell app-volumes__pills">+ {volume.read_only && <Pill kind="gray">read-only</Pill>}+ <Pill kind={volume.present === false ? 'red' : 'green'}>+ {volume.present === false ? 'missing' : 'present'}+ </Pill>+ </div>+ <div className="app-volumes__cell app-volumes__actions">+ {confirming ? (+ <div className="app-volumes__confirm">+ <label className="app-volumes__wipe">+ <Checkbox+ checked={wipe}+ onCheckedChange={(v) => setWipe(Boolean(v))}+ disabled={appRunning}+ />+ <span>Also delete data{appRunning ? ' (stop the app first)' : ' — cannot be undone'}</span>+ </label>+ <Button type="button" size="sm" variant="ghost" disabled={busy} onClick={() => setConfirming(false)}>Cancel</Button>+ <Button type="button" size="sm" variant={wipe ? 'destructive' : 'default'} disabled={busy} onClick={detach}>+ {busy ? 'Detaching…' : (wipe ? 'Detach + wipe' : 'Detach')}+ </Button>+ </div>+ ) : (+ <Button type="button" size="sm" variant="outline" onClick={() => setConfirming(true)}>+ <Trash2 size={15} /> Detach+ </Button>+ )}+ </div>+ </div>+ );+}++const VolumesPanel = ({ app, onChanged }) => {+ const toast = useToast();+ const [volumes, setVolumes] = useState([]);+ const [loading, setLoading] = useState(true);+ const [form, setForm] = useState({ name: '', mount_path: '', read_only: false });+ const [attaching, setAttaching] = useState(false);++ const appRunning = app.status === 'running';++ const load = useCallback(async () => {+ setLoading(true);+ try {+ const data = await api.getAppVolumes(app.id);+ setVolumes(data?.volumes || []);+ } catch (err) {+ toast.error(err.message || 'Failed to load volumes');+ } finally {+ setLoading(false);+ }+ }, [app.id, toast]);++ useEffect(() => { load(); }, [load]);++ async function attach(e) {+ e.preventDefault();+ setAttaching(true);+ try {+ await api.attachAppVolume(app.id, {+ name: form.name.trim(),+ mount_path: form.mount_path.trim(),+ read_only: form.read_only,+ });+ toast.success('Volume attached. It mounts on the next deploy.');+ setForm({ name: '', mount_path: '', read_only: false });+ await load();+ onChanged?.();+ } catch (err) {+ toast.error(err.message || 'Failed to attach volume');+ } finally {+ setAttaching(false);+ }+ }++ async function detach(volume, wipe) {+ try {+ await api.detachAppVolume(app.id, volume.id, { wipe });+ toast.success(wipe ? 'Volume detached and data wiped.' : 'Volume detached (data preserved).');+ await load();+ onChanged?.();+ } catch (err) {+ toast.error(err.message || 'Failed to detach volume');+ }+ }++ const canAttach = form.name.trim() && form.mount_path.trim().startsWith('/');++ return (+ <div className="app-volumes">+ <div className="app-panel">+ <div className="app-panel-header">+ <HardDrive />+ <span>Managed Volumes</span>+ </div>+ <div className="app-panel-body">+ <p className="app-panel-hint">+ First-class persistent storage that survives redeploys. Each volume is a+ named Docker volume mounted into the container at the path you choose —+ safer than a relative bind mount. Changes apply on the next deploy.+ </p>++ {loading ? (+ <p className="app-panel-hint">Loading volumes…</p>+ ) : volumes.length === 0 ? (+ <p className="app-volumes__empty">No managed volumes yet. Attach one below.</p>+ ) : (+ <div className="app-volumes__table">+ <div className="app-volumes__row app-volumes__row--head">+ <div className="app-volumes__cell">Name & mount path</div>+ <div className="app-volumes__cell">Size</div>+ <div className="app-volumes__cell">Status</div>+ <div className="app-volumes__cell" />+ </div>+ {volumes.map((v) => (+ <VolumeRow key={v.id} volume={v} appRunning={appRunning} onDetach={detach} />+ ))}+ </div>+ )}++ <form className="app-volumes__attach" onSubmit={attach}>+ <div className="app-volumes__attach-fields">+ <div className="container-ops__input">+ <Label htmlFor={`vol-name-${app.id}`}>Name</Label>+ <Input+ id={`vol-name-${app.id}`}+ value={form.name}+ onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))}+ placeholder="uploads"+ />+ </div>+ <div className="container-ops__input">+ <Label htmlFor={`vol-path-${app.id}`}>Container mount path</Label>+ <Input+ id={`vol-path-${app.id}`}+ value={form.mount_path}+ onChange={(e) => setForm((f) => ({ ...f, mount_path: e.target.value }))}+ placeholder="/var/www/html/wp-content/uploads"+ />+ </div>+ <label className="app-volumes__ro">+ <Checkbox+ checked={form.read_only}+ onCheckedChange={(v) => setForm((f) => ({ ...f, read_only: Boolean(v) }))}+ />+ <span>Read-only</span>+ </label>+ </div>+ <div className="app-detail-actions">+ <Button type="submit" size="sm" disabled={attaching || !canAttach}>+ <Plus size={15} /> {attaching ? 'Attaching…' : 'Attach volume'}+ </Button>+ </div>+ </form>++ <p className="app-volumes__note">+ <AlertTriangle size={14} /> Detaching keeps the data by default. Wiping is+ only allowed while the app is stopped.+ </p>+ </div>+ </div>+ </div>+ );+};++export default VolumesPanel;diff --git a/frontend/src/components/service-detail/SettingsTab.jsx b/frontend/src/components/service-detail/SettingsTab.jsxindex f56a9481..7555da13 100644--- a/frontend/src/components/service-detail/SettingsTab.jsx+++ b/frontend/src/components/service-detail/SettingsTab.jsx@@ -11,6 +11,7 @@ import {Boxes,Hammer,Archive,+ HardDrive,CircleCheck,CircleX,Sparkles,@@ -21,6 +22,7 @@ import { DangerZone } from '../DangerZone';import RepoConnectForm from '../git/RepoConnectForm';import ProtectionPanel from '../backups/ProtectionPanel';import ContainerOpsPanel from '../apps/ContainerOpsPanel';+import VolumesPanel from '../apps/VolumesPanel';import AppWafPanel from '../apps/AppWafPanel';import BuildTab from '../appdetail/BuildTab';import DeployTab from '../appdetail/DeployTab';@@ -66,7 +68,13 @@ function buildSettingsGroups(app) {label: 'Security',items: [{ id: 'waf', label: 'WAF', icon: ShieldCheck }],}] : []),- { label: 'Data', items: [{ id: 'backups', label: 'Backups', icon: Archive }] },+ {+ label: 'Data',+ items: [+ ...(isDocker ? [{ id: 'storage', label: 'Storage', icon: HardDrive }] : []),+ { id: 'backups', label: 'Backups', icon: Archive },+ ],+ },{ label: 'Advanced', items: [{ id: 'danger', label: 'Danger Zone', icon: AlertTriangle }] },];}@@ -288,6 +296,15 @@ const SettingsTab = ({ app, deployConfig, domains, primaryDomain, onUpdate }) =></div>)}+ {/* Storage (Docker only) — first-class managed volumes that+ survive redeploys, replacing fragile relative bind mounts. */}+ {section === 'storage' && (+ <div className="svc-settings__section">+ <h3 className="svc-settings__section-title">Storage</h3>+ <VolumesPanel app={app} onChanged={onUpdate} />+ </div>+ )}+{/* Git & Deploy — the shared RepoConnectForm (provider picker + URLfallback, connected summary with Disconnect) owns the repoconnection. Once a repo is linked, the embedded DeployTab addsdiff --git a/frontend/src/services/api/apps.js b/frontend/src/services/api/apps.jsindex 3518b282..31958c0b 100644--- a/frontend/src/services/api/apps.js+++ b/frontend/src/services/api/apps.js@@ -99,6 +99,25 @@ export async function deleteApp(id) {});}+// Per-app managed volumes — first-class persistent storage that survives+// redeploys. Listing joins live Docker state (present/mountpoint/size).+export async function getAppVolumes(id) {+ return this.request(`/apps/${id}/volumes`);+}++export async function attachAppVolume(id, data) {+ return this.request(`/apps/${id}/volumes`, { method: 'POST', body: data });+}++export async function detachAppVolume(id, volumeId, { wipe = false } = {}) {+ const q = wipe ? '?wipe=true' : '';+ return this.request(`/apps/${id}/volumes/${volumeId}${q}`, { method: 'DELETE' });+}++export async function convertAppBindMount(id, data) {+ return this.request(`/apps/${id}/volumes/convert`, { method: 'POST', body: data });+}+export async function startApp(id) {return this.request(`/apps/${id}/start`, { method: 'POST' });}diff --git a/frontend/src/styles/pages/_applications.scss b/frontend/src/styles/pages/_applications.scssindex 26dfaa7e..7f77be20 100644--- a/frontend/src/styles/pages/_applications.scss+++ b/frontend/src/styles/pages/_applications.scss@@ -1566,6 +1566,118 @@}}+// ============================================+// MANAGED VOLUMES (Storage panel)+// ============================================++.app-volumes {+ &__empty {+ font-size: 13px;+ color: var(--text-faint);+ padding: $space-3 0;+ }++ &__table {+ display: flex;+ flex-direction: column;+ margin-top: $space-3;+ }++ &__row {+ display: grid;+ grid-template-columns: minmax(0, 2fr) 90px 140px auto;+ align-items: center;+ gap: $spacing-md;+ padding: $space-3 0;+ border-top: 1px solid var(--border-soft);++ &--head {+ border-top: none;+ font-size: 12px;+ text-transform: uppercase;+ letter-spacing: 0.04em;+ color: var(--text-faint);+ }++ @media (max-width: $breakpoint-sm) {+ grid-template-columns: 1fr;+ gap: 4px;+ }+ }++ &__cell {+ min-width: 0;+ font-size: 13px;++ &--name {+ display: flex;+ flex-direction: column;+ gap: 2px;+ }+ }++ &__mono {+ font-family: $font-mono, monospace;+ font-size: 12px;+ color: var(--text-dim);+ word-break: break-all;+ }++ &__pills {+ display: flex;+ gap: 6px;+ flex-wrap: wrap;+ }++ &__actions {+ display: flex;+ justify-content: flex-end;+ }++ &__confirm {+ display: flex;+ align-items: center;+ gap: $spacing-sm;+ flex-wrap: wrap;+ justify-content: flex-end;+ }++ &__wipe,+ &__ro {+ display: flex;+ align-items: center;+ gap: 8px;+ font-size: 12.5px;+ color: var(--text-dim);+ }++ &__attach {+ margin-top: $spacing-md;+ padding-top: $spacing-md;+ border-top: 1px solid var(--border-soft);+ }++ &__attach-fields {+ display: grid;+ grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) auto;+ align-items: end;+ gap: $spacing-md;++ @media (max-width: $breakpoint-sm) {+ grid-template-columns: 1fr;+ }+ }++ &__note {+ display: flex;+ align-items: center;+ gap: 6px;+ margin-top: $spacing-md;+ font-size: 12px;+ color: var(--text-faint);+ }+}+// ============================================// WAF PANEL (ModSecurity policy / events)// ============================================