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.

fix(apps): keep compose the default; divert only build-pack apps

Juan Denisaa9a570f57e3
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py
index 89ddb13f..e2fe5b85 100644
--- a/backend/app/api/apps.py
+++ b/backend/app/api/apps.py
@@ -137,21 +137,27 @@ def _assert_managed_app_path(app_name):
raise ValueError('Invalid application path')
-def _has_local_compose_project(app):
- """True when a local app really has a compose project on disk.
-
- ``app_type == 'docker'`` is not the same as "compose-managed". A build-pack
- app is deployed by building an image and running a single container
- (``DeploymentService._deploy_docker``); nothing in that path ever writes a
- compose file, so ``compose_file`` stays NULL. Handing such a directory to
- ``docker compose`` fails with "no configuration file provided: not found",
- which is what start/stop/restart did for every build-pack app.
+def _is_single_container_app(app):
+ """True when this app's deploy produced one container, not a compose project.
+
+ ``app_type == 'docker'`` is not the same as "compose-managed". The build
+ pack deploys by building an image and running a single container
+ (``DeploymentService._deploy_docker``, named ``serverkit-app-<id>``) and
+ nothing in that path ever writes a compose file, so ``compose_file`` stays
+ NULL. Handing that directory to ``docker compose`` fails with "no
+ configuration file provided: not found" -- which is what start, stop and
+ restart did for every build-pack app.
+
+ Deliberately narrow, and phrased as a positive test rather than "has no
+ compose file": compose stays the default for everything, including an app
+ whose compose file has not been rendered yet. Only a local build-pack app
+ with no compose file recorded and none on disk takes the container path.
"""
- if app.server_id or not app.root_path:
+ if app.server_id or app.compose_file or not app.root_path:
return False
- if app.compose_file:
- return os.path.isfile(os.path.join(app.root_path, app.compose_file))
- return any(
+ if not app.buildpack_type:
+ return False
+ return not any(
os.path.isfile(os.path.join(app.root_path, name))
for name in ('docker-compose.yml', 'docker-compose.yaml',
'compose.yml', 'compose.yaml')
@@ -1555,7 +1561,15 @@ def start_app(app_id):
detach=True,
user_id=current_user_id
)
- elif _has_local_compose_project(app):
+ elif _is_single_container_app(app):
+ # Build-pack app: one container, created by the deploy.
+ container = _app_container_name(app)
+ if not DockerService.get_container(container):
+ return jsonify({'error': 'This application has not been deployed yet. '
+ 'Run a deploy to build its image and create '
+ 'the container.'}), 400
+ result = DockerService.start_container(container)
+ else:
# Authenticate a bound private registry before compose pulls the
# image; best-effort, always logs back out. No-op without registry_id.
_registry = ContainerRegistryService.login_for_app(app)
@@ -1567,14 +1581,6 @@ def start_app(app_id):
)
finally:
ContainerRegistryService.logout_for_app(_registry)
- else:
- # Build-pack app: one container, created by the deploy.
- container = _app_container_name(app)
- if not DockerService.get_container(container):
- return jsonify({'error': 'This application has not been deployed yet. '
- 'Run a deploy to build its image and create '
- 'the container.'}), 400
- result = DockerService.start_container(container)
if not result.get('success') or _agent_result_failed(result):
return jsonify({'error': _agent_result_error(result, 'Failed to start containers')}), 400
@@ -1999,17 +2005,17 @@ def stop_app(app_id):
_compose_target(app),
user_id=current_user_id
)
- elif _has_local_compose_project(app):
+ elif _is_single_container_app(app):
+ # A container that no longer exists is already stopped -- reporting
+ # that as a failure would strand the app in `running` forever.
+ container = _app_container_name(app)
+ result = ({'success': True} if not DockerService.get_container(container)
+ else DockerService.stop_container(container))
+ else:
result = DockerService.compose_down(
app.root_path,
compose_file=_local_compose_file(app)
)
- else:
- # Build-pack app. A container that no longer exists is already
- # stopped -- reporting that as a failure would strand the app.
- container = _app_container_name(app)
- result = ({'success': True} if not DockerService.get_container(container)
- else DockerService.stop_container(container))
if not result.get('success') or _agent_result_failed(result):
return jsonify({'error': _agent_result_error(result, 'Failed to stop containers')}), 400
@@ -2044,18 +2050,18 @@ def restart_app(app_id):
_compose_target(app),
user_id=current_user_id
)
- elif _has_local_compose_project(app):
- result = DockerService.compose_restart(
- app.root_path,
- compose_file=_local_compose_file(app)
- )
- else:
+ elif _is_single_container_app(app):
container = _app_container_name(app)
if not DockerService.get_container(container):
return jsonify({'error': 'This application has not been deployed yet. '
'Run a deploy to build its image and create '
'the container.'}), 400
result = DockerService.restart_container(container)
+ else:
+ result = DockerService.compose_restart(
+ app.root_path,
+ compose_file=_local_compose_file(app)
+ )
if not result.get('success') or _agent_result_failed(result):
return jsonify({'error': _agent_result_error(result, 'Failed to restart containers')}), 400
diff --git a/backend/tests/test_app_lifecycle_noncompose.py b/backend/tests/test_app_lifecycle_noncompose.py
index 1a8eb653..b2235b2d 100644
--- a/backend/tests/test_app_lifecycle_noncompose.py
+++ b/backend/tests/test_app_lifecycle_noncompose.py
@@ -20,10 +20,9 @@ from unittest.mock import patch
import pytest
-from app import db
-from app.api.apps import _has_local_compose_project
-from app.models import Application, User
-from factories import headers_for, make_user
+from app.api.apps import _is_single_container_app
+from app.models import Application
+from factories import headers_for, make_application, make_user
@pytest.fixture
@@ -32,26 +31,32 @@ def owner(app, db_session):
@pytest.fixture
-def buildpack_app(app, owner, tmp_path):
+def buildpack_app(app, db_session, owner, tmp_path):
"""A build-pack app: source on disk, no compose file anywhere."""
- application = Application(name='buildpack-app', app_type='docker',
- status='stopped', root_path=str(tmp_path),
- buildpack_type='nixpacks', port=4173,
- user_id=owner.id)
- db.session.add(application)
- db.session.commit()
- return application
+ return make_application(
+ db_session, name='buildpack-app', app_type='docker', source='github',
+ status='stopped', root_path=str(tmp_path), compose_file=None,
+ docker_image=None, buildpack_type='nixpacks', port=4173,
+ user_id=owner.id,
+ )
# ── the probe ────────────────────────────────────────────────────────────────
-def test_docker_app_type_alone_is_not_a_compose_project(buildpack_app):
- assert _has_local_compose_project(buildpack_app) is False
+def test_a_buildpack_app_with_no_compose_file_is_single_container(buildpack_app):
+ assert _is_single_container_app(buildpack_app) is True
-def test_a_compose_file_on_disk_is_a_compose_project(buildpack_app, tmp_path):
+def test_a_compose_file_on_disk_keeps_the_compose_path(buildpack_app, tmp_path):
(tmp_path / 'docker-compose.yml').write_text('services: {}\n', encoding='utf-8')
- assert _has_local_compose_project(buildpack_app) is True
+ assert _is_single_container_app(buildpack_app) is False
+
+
+def test_a_non_buildpack_app_keeps_the_compose_path(buildpack_app):
+ """Compose stays the default. An app whose compose file simply has not
+ been rendered yet must not be diverted onto the container path."""
+ buildpack_app.buildpack_type = None
+ assert _is_single_container_app(buildpack_app) is False
# ── start ────────────────────────────────────────────────────────────────────