podup 1.7.0

Translate and run docker-compose files on rootless Podman
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Compose file parsing, `extends:` resolution, `include:` merging, and
//! topological service ordering.

pub mod types;

mod anchor;
mod diagnostics;
mod extends;
mod include;
mod merge;
mod order;
mod validate;

use std::path::{Path, PathBuf};

use crate::error::{ComposeError, Result};
use crate::substitute;
use types::{ComposeFile, ServiceNetworks};

pub use order::{resolve_levels, resolve_order};
pub use validate::validate_config;

/// Whether a compose-file path is the stdin sentinel `-` (`docker compose -f -`).
fn is_stdin(path: &Path) -> bool {
	path == Path::new("-")
}

/// Parse a compose file from disk, applying variable substitution and
/// resolving `extends:` / `include:` directives.
pub fn parse_file(path: &Path) -> Result<ComposeFile> {
	parse_file_with_env_files(path, &[])
}

/// Like [`parse_file`], additionally loading `env_files` (the global
/// `--env-file` flag) into the variable map used for interpolation. These take
/// effect for the top-level file and any included files; the process
/// environment and a project `.env` still take precedence.
pub fn parse_file_with_env_files(path: &Path, env_files: &[String]) -> Result<ComposeFile> {
	parse_file_with_env_files_interp(path, env_files, true)
}

/// Like [`parse_file_with_env_files`] but with explicit control over variable
/// interpolation. `interpolate = false` (the `config --no-interpolate` path)
/// leaves `${VAR}` placeholders literal while still resolving
/// `extends:`/`include:`/merge.
pub fn parse_file_with_env_files_interp(
	path: &Path,
	env_files: &[String],
	interpolate: bool,
) -> Result<ComposeFile> {
	// `-f -` reads the compose document from stdin (like `docker compose`); there
	// is no file to canonicalize, so relative paths and `.env` resolve against the
	// working directory.
	let (abs, dir) = if is_stdin(path) {
		let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
		(PathBuf::from("-"), cwd)
	} else {
		let abs = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
		let dir = abs.parent().unwrap_or(Path::new(".")).to_path_buf();
		(abs, dir)
	};
	let mut file = parse_file_inner_with_env(&abs, &dir, env_files, interpolate)?;

	let includes = std::mem::take(&mut file.include);
	for inc in includes {
		let (extra_env_files, project_dir_override) = match &inc {
			types::IncludeConfig::Long {
				env_file,
				project_directory,
				..
			} => (
				env_file.as_ref().map(|ef| ef.to_list()).unwrap_or_default(),
				project_directory.as_ref().map(|pd| dir.join(pd)),
			),
			_ => (vec![], None),
		};
		for rel in inc.paths() {
			let rel_path = std::path::Path::new(&rel);
			// The Compose Specification resolves `include` paths relative to the
			// including file and treats `../` as canonical (monorepos routinely use
			// `include: ../shared/compose.yaml`). An absolute path is used as given.
			// This matches docker-compose and the trusted-input policy already
			// applied to `extends.file` and `env_file` — the compose file is
			// trusted input, like a Makefile.
			let inc_path = if rel_path.is_absolute() {
				rel_path.to_path_buf()
			} else {
				dir.join(&rel)
			};
			let inc_dir = project_dir_override.clone().unwrap_or_else(|| {
				inc_path
					.parent()
					.map(|p| p.to_path_buf())
					.unwrap_or_else(|| dir.clone())
			});
			let mut combined_env_files = env_files.to_vec();
			combined_env_files.extend(extra_env_files.iter().cloned());
			let mut included =
				parse_file_inner_with_env(&inc_path, &inc_dir, &combined_env_files, interpolate)?;
			anchor::anchor_compose_file(&mut included, &inc_dir);
			include::merge_compose_file(&mut file, included);
		}
	}

	extends::resolve_all_extends(&mut file, &dir)?;
	Ok(file)
}

/// Collect parse-time diagnostics for an already-parsed compose file: warnings
/// about recognized-but-unsupported keys and fields that are accepted but carry
/// no effect on Podman. The CLI prints these automatically; library consumers
/// (e.g. panel-agent) can call this to surface the same warnings, since
/// [`parse_file`] does not emit them itself.
pub fn collect_diagnostics(file: &ComposeFile) -> Vec<String> {
	diagnostics::collect(file)
}

/// Parse and merge multiple compose files (the `-f`/`COMPOSE_FILE` list).
///
/// Files are merged left to right: a later file overrides an earlier one,
/// service by service (per-field, like `extends`), with top-level
/// volumes/networks/secrets/configs replaced on key conflicts. Relative paths
/// resolve against the first file's directory, matching the compose project
/// directory. `env_files` feed interpolation for every file.
pub fn parse_files_with_env_files(paths: &[PathBuf], env_files: &[String]) -> Result<ComposeFile> {
	parse_files_with_env_files_interp(paths, env_files, true)
}

/// Like [`parse_files_with_env_files`] but with explicit interpolation control.
/// `interpolate = false` backs `config --no-interpolate`: `${VAR}` placeholders
/// are left literal across all merged files.
pub fn parse_files_with_env_files_interp(
	paths: &[PathBuf],
	env_files: &[String],
	interpolate: bool,
) -> Result<ComposeFile> {
	let mut iter = paths.iter();
	let first = iter
		.next()
		.ok_or_else(|| ComposeError::FileNotFound("no compose file given".to_string()))?;
	let mut merged = parse_file_with_env_files_interp(first, env_files, interpolate)?;
	for path in iter {
		let other = parse_file_with_env_files_interp(path, env_files, interpolate)?;
		merge_override(&mut merged, other);
	}
	normalize_default_network(&mut merged);
	// Semantic validation runs only on the interpolated file: `--no-interpolate`
	// leaves literal `${VAR}` placeholders that cannot be reference- or
	// range-checked. This makes `config`, `up`, and `generate` reject the same
	// contradictory files docker-compose does, at config time.
	if interpolate {
		validate::validate(&merged)?;
	}
	for warning in diagnostics::collect(&merged) {
		tracing::warn!("{warning}");
	}
	Ok(merged)
}

/// Synthesize the implicit `default` network, matching docker-compose: any
/// service that declares neither `networks:` nor `network_mode` is attached to
/// a project `default` network. Without this, such services are created with no
/// network namespace at all — they get no IP and cannot resolve each other by
/// name, silently breaking the common no-`networks:`-block compose file.
///
/// The `default` network is created as `{project}_default` (see
/// `resolve_network_name`) unless the file already defines a top-level
/// `networks.default`, whose configuration is then respected. Idempotent.
pub(crate) fn normalize_default_network(file: &mut ComposeFile) {
	let needs_default = file
		.services
		.values()
		.any(|svc| svc.network_mode.is_none() && matches!(svc.networks, ServiceNetworks::Empty));
	if !needs_default {
		return;
	}
	file.networks.entry("default".to_string()).or_insert(None);
	for svc in file.services.values_mut() {
		if svc.network_mode.is_none() && matches!(svc.networks, ServiceNetworks::Empty) {
			svc.networks = ServiceNetworks::List(vec!["default".to_string()]);
		}
	}
}

/// Merge `other` into `target` with `other` winning (compose `-f` override
/// semantics): services are merged field-by-field, other top-level maps replace
/// on key conflict.
fn merge_override(target: &mut ComposeFile, other: ComposeFile) {
	for (name, svc) in other.services {
		if let Some(base) = target.services.get_mut(&name) {
			*base = extends::merge_service(std::mem::take(base), svc);
		} else {
			target.services.insert(name, svc);
		}
	}
	for (k, v) in other.volumes {
		target.volumes.insert(k, v);
	}
	for (k, v) in other.networks {
		target.networks.insert(k, v);
	}
	for (k, v) in other.secrets {
		target.secrets.insert(k, v);
	}
	for (k, v) in other.configs {
		target.configs.insert(k, v);
	}
	for (k, v) in other.models {
		target.models.insert(k, v);
	}
}

/// Parse a compose YAML string (no file I/O).
///
/// Variable substitution is applied using only the process environment.
/// `extends: { file: ... }` and `include:` directives are not resolved —
/// use [`parse_file`] for that.
pub fn parse_str(content: &str) -> Result<ComposeFile> {
	let vars = substitute::build_vars(Path::new("."));
	let mut file = merge::deserialize_with_merge_interp(content, Some(&vars))?;
	extends::resolve_extends_same_file(&mut file)?;
	Ok(file)
}

/// Parse raw (already-substituted) YAML into a `ComposeFile` without any
/// post-processing.
pub fn parse_str_raw(content: &str) -> Result<ComposeFile> {
	merge::deserialize_with_merge(content)
}

pub(crate) fn parse_file_inner(path: &Path, dir: &Path) -> Result<ComposeFile> {
	parse_file_inner_with_env(path, dir, &[], true)
}

pub(crate) fn parse_file_inner_with_env(
	path: &Path,
	dir: &Path,
	extra_env_files: &[String],
	interpolate: bool,
) -> Result<ComposeFile> {
	let content = if is_stdin(path) {
		crate::filesystem::read_stdin_to_string_capped().map_err(ComposeError::Io)?
	} else {
		crate::filesystem::read_to_string_capped(path).map_err(|e| {
			if e.kind() == std::io::ErrorKind::NotFound {
				ComposeError::FileNotFound(path.display().to_string())
			} else {
				ComposeError::Io(e)
			}
		})?
	};
	// `config --no-interpolate` leaves `${VAR}` placeholders literal; otherwise
	// interpolate against the env/.env/env-file variable map. Interpolation runs
	// on the parsed YAML scalars (see `deserialize_with_merge_interp`), not the
	// raw text, so resolved values cannot alter the document structure.
	if interpolate {
		let vars = if extra_env_files.is_empty() {
			substitute::build_vars(dir)
		} else {
			substitute::build_vars_with_env_files_strict(dir, extra_env_files)?
		};
		merge::deserialize_with_merge_interp(&content, Some(&vars))
	} else {
		merge::deserialize_with_merge(&content)
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	// parse_str_raw

	#[test]
	fn is_stdin_matches_only_the_dash_sentinel() {
		assert!(is_stdin(Path::new("-")));
		assert!(!is_stdin(Path::new("docker-compose.yml")));
		assert!(!is_stdin(Path::new("./-")));
		assert!(!is_stdin(Path::new("a-b")));
	}

	#[test]
	fn parse_str_raw_minimal_service() {
		let yaml = "services:\n  web:\n    image: nginx\n";
		let file = parse_str_raw(yaml).unwrap();
		assert!(file.services.contains_key("web"));
		assert_eq!(file.services["web"].image.as_deref(), Some("nginx"));
	}

	#[test]
	fn collect_diagnostics_surfaces_unknown_keys() {
		// The public helper lets library consumers see the same warnings the CLI
		// prints; parse_file itself stays quiet.
		let file =
			parse_str_raw("services:\n  web:\n    image: nginx\n    enviroment:\n      - A=1\n")
				.unwrap();
		let diags = collect_diagnostics(&file);
		assert!(
			diags.iter().any(|d| d.contains("enviroment")),
			"expected an unknown-key diagnostic, got {diags:?}"
		);
	}

	#[test]
	fn parse_str_raw_invalid_yaml_is_error() {
		assert!(parse_str_raw(": : :").is_err());
	}

	// unknown-key capture / warning

	#[test]
	fn unknown_service_key_is_captured_not_dropped() {
		// A typo'd key lands in `unknown` instead of vanishing silently.
		let yaml = "services:\n  web:\n    image: nginx\n    enviroment:\n      - A=1\n";
		let file = parse_str_raw(yaml).unwrap();
		assert!(file.services["web"].unknown.contains_key("enviroment"));
		assert!(file.services["web"].environment.is_empty());
	}

	#[test]
	fn known_service_keys_do_not_land_in_unknown() {
		let yaml = "services:\n  web:\n    image: nginx\n    environment:\n      - A=1\n";
		let file = parse_str_raw(yaml).unwrap();
		assert!(file.services["web"].unknown.is_empty());
	}

	// Multi-file `-f` override merge

	#[test]
	fn merge_override_adds_models_and_override_wins() {
		use crate::compose::types::ModelConfig;
		let model = |m: &str| ModelConfig {
			model: Some(m.to_string()),
			..Default::default()
		};
		let mut target = ComposeFile::default();
		target.models.insert("llm".to_string(), model("base/m"));
		let mut other = ComposeFile::default();
		other.models.insert("llm".to_string(), model("over/m"));
		other.models.insert("extra".to_string(), model("e/m"));
		merge_override(&mut target, other);
		// Override file wins on conflict; the override-only model is added.
		assert_eq!(target.models["llm"].model.as_deref(), Some("over/m"));
		assert_eq!(target.models["extra"].model.as_deref(), Some("e/m"));
	}

	#[test]
	fn merge_override_unions_top_level_resource_maps() {
		use crate::compose::types::{ConfigConfig, NetworkConfig, SecretConfig, VolumeConfig};
		let mut target = ComposeFile::default();
		target
			.volumes
			.insert("data".to_string(), Some(VolumeConfig::default()));
		target
			.networks
			.insert("net".to_string(), Some(NetworkConfig::default()));
		target.secrets.insert(
			"tok".to_string(),
			SecretConfig {
				file: Some("base.txt".to_string()),
				..Default::default()
			},
		);
		target
			.configs
			.insert("cfg".to_string(), ConfigConfig::default());

		let mut other = ComposeFile::default();
		// An override-only volume/network/config is added; an overlapping secret is
		// replaced by the override file's definition.
		other
			.volumes
			.insert("cache".to_string(), Some(VolumeConfig::default()));
		other
			.networks
			.insert("backend".to_string(), Some(NetworkConfig::default()));
		other.secrets.insert(
			"tok".to_string(),
			SecretConfig {
				file: Some("override.txt".to_string()),
				..Default::default()
			},
		);
		other
			.configs
			.insert("extra".to_string(), ConfigConfig::default());

		merge_override(&mut target, other);

		assert!(target.volumes.contains_key("data"));
		assert!(target.volumes.contains_key("cache"));
		assert!(target.networks.contains_key("net"));
		assert!(target.networks.contains_key("backend"));
		assert_eq!(
			target.secrets["tok"].file.as_deref(),
			Some("override.txt"),
			"the override file's secret definition must win"
		);
		assert!(target.configs.contains_key("cfg"));
		assert!(target.configs.contains_key("extra"));
	}

	// YAML merge keys (<<)

	#[test]
	fn yaml_merge_key_fills_missing_fields() {
		let yaml = "x-defaults: &defaults\n  image: nginx\n  restart: always\nservices:\n  web:\n    <<: *defaults\n    ports: ['80:80']\n";
		let file = parse_str_raw(yaml).unwrap();
		assert_eq!(file.services["web"].image.as_deref(), Some("nginx"));
	}

	// Default-network synthesis (#417)

	#[test]
	fn normalize_attaches_bare_service_to_default_network() {
		let mut file = parse_str("services:\n  web:\n    image: nginx\n").unwrap();
		normalize_default_network(&mut file);
		assert!(file.networks.contains_key("default"));
		assert_eq!(file.services["web"].networks.names(), vec!["default"]);
	}

	#[test]
	fn normalize_leaves_service_with_explicit_networks_untouched() {
		let mut file = parse_str(
			"services:\n  web:\n    image: nginx\n    networks: [front]\nnetworks:\n  front:\n",
		)
		.unwrap();
		normalize_default_network(&mut file);
		assert_eq!(file.services["web"].networks.names(), vec!["front"]);
		// No default network is synthesized when nothing needs it.
		assert!(!file.networks.contains_key("default"));
	}

	#[test]
	fn normalize_skips_service_with_network_mode() {
		let mut file =
			parse_str("services:\n  web:\n    image: nginx\n    network_mode: host\n").unwrap();
		normalize_default_network(&mut file);
		assert!(file.services["web"].networks.names().is_empty());
		assert!(!file.networks.contains_key("default"));
	}

	#[test]
	fn normalize_respects_explicit_default_network_config() {
		let mut file = parse_str(
			"services:\n  web:\n    image: nginx\nnetworks:\n  default:\n    driver: bridge\n",
		)
		.unwrap();
		normalize_default_network(&mut file);
		// The user-defined `default` config is kept, not overwritten with None.
		assert!(file.networks["default"].is_some());
		assert_eq!(file.services["web"].networks.names(), vec!["default"]);
	}
}