fix: preserve empty strings and remove empty sections in docker-compose

- Preserve empty string environment variables instead of converting to null
  Empty strings and null have different semantics in Docker Compose:
  * Empty string (VAR: ""): Variable is set to "" in container (e.g., HTTP_PROXY="" means "no proxy")
  * Null (VAR: null): Variable is unset/removed from container environment

- Remove empty top-level sections (volumes, configs, secrets) from generated compose files
  These sections now only appear when they contain actual content, following Docker Compose best practices

- Add safety check for missing volumes in validateComposeFile to prevent iteration errors

- Add comprehensive unit tests for both fixes

Fixes #7126
This commit is contained in:
Andras Bacsai
2025-11-06 12:30:03 +01:00
parent 395d225f90
commit 1ab5dbca20
4 changed files with 427 additions and 10 deletions

View File

@@ -1073,6 +1073,9 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable
}
$yaml_compose = Yaml::parse($compose);
foreach ($yaml_compose['services'] as $service_name => $service) {
if (! isset($service['volumes'])) {
continue;
}
foreach ($service['volumes'] as $volume_name => $volume) {
if (data_get($volume, 'type') === 'bind' && data_get($volume, 'content')) {
unset($yaml_compose['services'][$service_name]['volumes'][$volume_name]['content']);