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.

feat(docker): one-click apply for image updates

Juan Denis2b471a39a262
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py
index 17a36666..c5889616 100644
--- a/backend/app/api/apps.py
+++ b/backend/app/api/apps.py
@@ -13,6 +13,7 @@ from app.services.git_service import GitService
from app.services.repository_manifest_service import RepositoryManifestService
from app.services.source_connection_service import SourceConnectionService
from app.services.remote_docker_service import RemoteDockerService
+from app.services.image_update_service import ImageUpdateService
from app.services.log_service import LogService
from app.services.process_service import ProcessService
from app.services.upload_service import (
@@ -1163,6 +1164,53 @@ def start_app(app_id):
}), 200
+@apps_bp.route('/<int:app_id>/image-update/apply', methods=['POST'])
+@jwt_required()
+def apply_image_update(app_id):
+ """Pull the newest image for a compose-managed app and recreate it."""
+ 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 not _can_edit_app(user, app):
+ return jsonify({'error': 'Access denied'}), 403
+
+ # Auto-apply is only safe for compose-managed apps; recreating a standalone
+ # container would need its full run spec, which we don't store.
+ if app.app_type != 'docker' or not app.root_path or not app.compose_file:
+ return jsonify({'error': 'Automatic update is only supported for docker-compose apps. '
+ 'Pull and recreate this container manually.'}), 400
+
+ # Pull the newest images for the project, then recreate changed containers.
+ if app.server_id:
+ pull = RemoteDockerService.compose_pull(app.server_id, _compose_target(app), user_id=current_user_id)
+ if not pull.get('success') or _agent_result_failed(pull):
+ return jsonify({'error': _agent_result_error(pull, 'Failed to pull image')}), 400
+ up = RemoteDockerService.compose_up(app.server_id, _compose_target(app), detach=True, user_id=current_user_id)
+ else:
+ pull = DockerService.compose_pull(app.root_path, compose_file=_local_compose_file(app))
+ if not pull.get('success'):
+ return jsonify({'error': pull.get('error', 'Failed to pull image')}), 400
+ up = DockerService.compose_up(app.root_path, detach=True, compose_file=_local_compose_file(app))
+
+ if not up.get('success') or _agent_result_failed(up):
+ return jsonify({'error': _agent_result_error(up, 'Failed to recreate containers')}), 400
+
+ app.status = 'running'
+ app.last_deployed_at = datetime.utcnow()
+ db.session.commit()
+
+ # Refresh the update badge now that we're on the new image.
+ ImageUpdateService.check_application(app_id)
+
+ return jsonify({
+ 'message': 'Image updated and containers recreated',
+ 'app': app.to_dict(),
+ }), 200
+
+
@apps_bp.route('/<int:app_id>/stop', methods=['POST'])
@jwt_required()
def stop_app(app_id):
diff --git a/backend/tests/test_image_update.py b/backend/tests/test_image_update.py
index 97fd2075..907667b3 100644
--- a/backend/tests/test_image_update.py
+++ b/backend/tests/test_image_update.py
@@ -3,6 +3,7 @@ import uuid
from app import db
from app.models import Application
+from app.services.docker_service import DockerService
from app.services.image_update_service import ImageUpdateService
@@ -76,3 +77,50 @@ class TestImageUpdateApi:
def test_check_requires_auth(self, client, app):
assert client.post('/api/v1/image-updates/applications/1/check').status_code == 401
+
+
+class TestImageUpdateApply:
+ def _compose_app(self):
+ from app.models import User
+ admin = User.query.filter_by(username='testadmin').first()
+ row = Application(name='web', app_type='docker', source='manual',
+ root_path='/tmp/web', compose_file='docker-compose.yml',
+ docker_image='nginx:latest', user_id=admin.id)
+ db.session.add(row)
+ db.session.commit()
+ return row
+
+ def test_apply_pulls_and_recreates_compose_app(self, client, auth_headers, app, monkeypatch):
+ row = self._compose_app()
+ calls = []
+ monkeypatch.setattr(DockerService, 'compose_pull',
+ lambda *a, **k: calls.append('pull') or {'success': True})
+ monkeypatch.setattr(DockerService, 'compose_up',
+ lambda *a, **k: calls.append('up') or {'success': True})
+ monkeypatch.setattr(ImageUpdateService, '_local_digest', lambda ref: 'sha256:x')
+ monkeypatch.setattr(ImageUpdateService, '_registry_digest', lambda ref: 'sha256:x')
+
+ resp = client.post(f'/api/v1/apps/{row.id}/image-update/apply', headers=auth_headers)
+ assert resp.status_code == 200
+ assert resp.get_json()['app']['status'] == 'running'
+ assert calls == ['pull', 'up'] # pulled before recreating
+
+ def test_apply_propagates_pull_failure(self, client, auth_headers, app, monkeypatch):
+ row = self._compose_app()
+ monkeypatch.setattr(DockerService, 'compose_pull',
+ lambda *a, **k: {'success': False, 'error': 'pull boom'})
+ resp = client.post(f'/api/v1/apps/{row.id}/image-update/apply', headers=auth_headers)
+ assert resp.status_code == 400
+ assert 'pull boom' in resp.get_json()['error']
+
+ def test_apply_guards_non_compose_app(self, client, auth_headers, app):
+ from app.models import User
+ admin = User.query.filter_by(username='testadmin').first()
+ row = Application(name='static', app_type='static', source='manual', user_id=admin.id)
+ db.session.add(row)
+ db.session.commit()
+ resp = client.post(f'/api/v1/apps/{row.id}/image-update/apply', headers=auth_headers)
+ assert resp.status_code == 400
+
+ def test_apply_requires_auth(self, client, app):
+ assert client.post('/api/v1/apps/1/image-update/apply').status_code == 401