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(templates): only bind-mount absolute file paths; relative files are build-context only

Juan Denis465a4ee9a17f
diff --git a/backend/app/services/template_service.py b/backend/app/services/template_service.py
index cd89bf25..6942f32d 100644
--- a/backend/app/services/template_service.py
+++ b/backend/app/services/template_service.py
@@ -1093,11 +1093,17 @@ class TemplateService:
'content': content,
'mode': int(file_def.get('mode', 0o644)),
})
- bind_mounts.append({
- 'local': f'./{filename}',
- 'container': container_path,
- 'container_dir': os.path.dirname(container_path),
- })
+ # Only absolute container paths are bind-mounted into the
+ # container (e.g. /app/config.yaml). A relative path like
+ # 'Dockerfile' is a build-context file: it is written into the
+ # app directory only — mounting it would produce an invalid
+ # './Dockerfile:Dockerfile' volume spec and compose up fails.
+ if container_path.startswith('/'):
+ bind_mounts.append({
+ 'local': f'./{filename}',
+ 'container': container_path,
+ 'container_dir': os.path.dirname(container_path),
+ })
if bind_mounts:
cls._apply_bind_mounts_to_compose(compose, bind_mounts)
diff --git a/backend/tests/test_template_files_render.py b/backend/tests/test_template_files_render.py
new file mode 100644
index 00000000..64e7d411
--- /dev/null
+++ b/backend/tests/test_template_files_render.py
@@ -0,0 +1,46 @@
+"""Template ``files:`` rendering: absolute container paths are bind-mounted,
+relative paths (build-context files like ``Dockerfile``) are only written
+into the app directory.
+
+Regression test: a relative ``path: Dockerfile`` used to produce the invalid
+volume spec ``./Dockerfile:Dockerfile`` and every compose-based install of
+such a template failed at ``docker compose up`` with "invalid mount config
+for type bind: mount path must be absolute".
+"""
+import yaml
+
+from app.services.template_service import TemplateService as TS
+
+
+def _render(files):
+ template = {
+ 'compose': {
+ 'services': {'app': {'image': 'example:latest', 'volumes': ['data:/data']}},
+ 'volumes': {'data': None},
+ },
+ 'files': files,
+ }
+ return TS._render_compose_and_files(template, {}, '/tmp/app-x')
+
+
+def test_absolute_file_path_is_bind_mounted():
+ result = _render([{'path': '/app/config.yaml', 'content': 'a: b'}])
+
+ assert result['success']
+ compose = yaml.safe_load(result['compose_content'])
+ volumes = compose['services']['app']['volumes']
+ assert './config.yaml:/app/config.yaml' in volumes
+ assert result['files'][0]['path'].endswith('config.yaml')
+
+
+def test_relative_file_path_is_written_but_not_mounted():
+ result = _render([{'path': 'Dockerfile', 'content': 'FROM scratch'}])
+
+ assert result['success']
+ # Still written into the app directory (build context).
+ assert result['files'][0]['path'].endswith('Dockerfile')
+ # ...but NOT bind-mounted into the container.
+ compose = yaml.safe_load(result['compose_content'])
+ volumes = compose['services']['app']['volumes']
+ assert volumes == ['data:/data']
+ assert not any('Dockerfile' in str(v) for v in volumes)