podup 1.7.1

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
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Validation of a fully parsed and merged compose file.
//!
//! Two entry points live here. [`validate_config`] backs the `config` subcommand,
//! applying the same cross-reference and well-formedness checks
//! `docker compose config` performs and that the mutating commands would
//! otherwise only surface later (at `resolve_order` time, when Podman rejects a
//! bad port, or when an undeclared volume/network reaches the runtime). Running
//! them up front means `config` reports the divergence at exit non-zero instead
//! of echoing the file verbatim.
//!
//! [`validate`] is the semantic consistency pass run automatically after parsing
//! and merging: it rejects files that deserialize cleanly but are semantically
//! contradictory (e.g. a service that declares both `network_mode` and
//! `networks`, or an `external: true` network that also sets creation-time
//! attributes). docker-compose errors on these at config time; podup used to
//! accept them and then silently pick one interpretation, with the live engine
//! and the Quadlet exporter diverging on which one. Failing fast here keeps
//! `config`, `up`, and `generate` in agreement.

use crate::compose::order::resolve_order;
use crate::compose::types::{ComposeFile, PortMapping, VolumeMount, VolumeType};
use crate::error::{ComposeError, Result};
use crate::ports::parse_ports;

/// Validate a parsed compose file the way `docker compose config` does.
///
/// Checks, in order: at least one service is defined; every service declares an
/// `image:` or `build:`; service names use the compose charset; published/target
/// ports are in range; every referenced named volume and network is declared at
/// the top level; and the `depends_on` graph is acyclic with no dangling
/// required dependency. Returns the first violation found.
pub fn validate_config(file: &ComposeFile) -> Result<()> {
	// An empty file, a missing `services:` key, or `services: {}` is not a valid
	// project — `docker compose config` errors with "no service selected".
	if file.services.is_empty() {
		return Err(ComposeError::Unsupported(
			"no services defined in compose file".to_string(),
		));
	}

	for (name, svc) in &file.services {
		validate_service_name(name)?;
		if svc.image.is_none() && svc.build.is_none() {
			return Err(ComposeError::NoImageOrBuild(name.clone()));
		}
		validate_ports(name, &svc.ports)?;
		validate_network_refs(name, file, svc)?;
		validate_volume_refs(name, file, svc)?;
	}

	// Reject `depends_on` cycles and dangling required dependencies, matching the
	// mutating commands (which run `resolve_order` before they start anything).
	resolve_order(file)?;
	Ok(())
}

/// Reject a service name that is empty or uses characters outside the compose
/// charset (`[a-zA-Z0-9._-]`). Spaces and punctuation like `!` are rejected.
fn validate_service_name(name: &str) -> Result<()> {
	let ok = !name.is_empty()
		&& name
			.chars()
			.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'));
	if ok {
		Ok(())
	} else {
		Err(ComposeError::Unsupported(format!(
			"service name {name:?} is invalid: use only ASCII letters, digits, '.', '_', '-'"
		)))
	}
}

/// Range-check every port a service publishes. `parse_ports` rejects values that
/// do not fit a `u16` (e.g. `99999`); on top of that a container or host port of
/// `0` is rejected here, since a valid published/target port is `1`–`65535`.
fn validate_ports(service: &str, ports: &[PortMapping]) -> Result<()> {
	for parsed in parse_ports(ports)? {
		if parsed.container_port == 0 || parsed.host_port == Some(0) {
			return Err(ComposeError::InvalidPort(format!(
				"service '{service}' has a port of 0; ports must be in 1-65535"
			)));
		}
	}
	Ok(())
}

/// Every network a service joins must be declared in the top-level `networks:`
/// map (the implicit `default` network is synthesized before this runs, so a
/// bare service still validates). `network_mode:` services declare no networks.
fn validate_network_refs(
	service: &str,
	file: &ComposeFile,
	svc: &crate::compose::types::Service,
) -> Result<()> {
	for net in svc.networks.names() {
		if !file.networks.contains_key(&net) {
			return Err(ComposeError::Unsupported(format!(
				"service '{service}' refers to undefined network '{net}'; declare it under the \
				 top-level 'networks:' key"
			)));
		}
	}
	Ok(())
}

/// Every *named* volume a service mounts must be declared in the top-level
/// `volumes:` map. Bind mounts (host paths) and anonymous volumes carry no
/// top-level declaration and are skipped.
fn validate_volume_refs(
	service: &str,
	file: &ComposeFile,
	svc: &crate::compose::types::Service,
) -> Result<()> {
	for mount in &svc.volumes {
		let named = match mount {
			VolumeMount::Short(s) => short_named_volume(s),
			VolumeMount::Long {
				volume_type: VolumeType::Volume,
				source: Some(src),
				..
			} => Some(src.as_str()),
			VolumeMount::Long { .. } => None,
		};
		if let Some(name) = named {
			if !file.volumes.contains_key(name) {
				return Err(ComposeError::Unsupported(format!(
					"service '{service}' refers to undefined volume '{name}'; declare it under the \
					 top-level 'volumes:' key"
				)));
			}
		}
	}
	Ok(())
}

/// Extract the named-volume reference from a short-form `source:target[:opts]`
/// mount, or `None` when it is a host-path bind or an anonymous volume.
///
/// Mirrors the engine's own classification: a source starting with `/`, `.` or
/// `~`, or a Windows drive prefix (`C:`), is a bind, not a named volume; a
/// single token with no target is an anonymous volume.
fn short_named_volume(spec: &str) -> Option<&str> {
	let (src, _rest) = spec.split_once(':')?;
	if src.is_empty()
		|| src.starts_with('/')
		|| src.starts_with('.')
		|| src.starts_with('~')
		|| is_windows_drive(src)
	{
		return None;
	}
	Some(src)
}

/// Whether `src` is exactly a Windows drive letter (e.g. `C`), meaning the colon
/// after it is part of a host path rather than the `source:target` separator.
fn is_windows_drive(src: &str) -> bool {
	let b = src.as_bytes();
	b.len() == 1 && b[0].is_ascii_alphabetic()
}

/// Validate the semantic consistency of a resolved compose file. Returns the
/// first error found, matching docker-compose's fail-at-config-time behaviour.
///
/// Run only on the interpolated file: with `--no-interpolate` the values may
/// still contain literal `${VAR}` placeholders, which cannot be meaningfully
/// range- or reference-checked.
pub(super) fn validate(file: &ComposeFile) -> Result<()> {
	validate_services(file)?;
	validate_networks(file)?;
	Ok(())
}

/// Per-service checks: the `network_mode`/`networks` mutual exclusion, every
/// network reference resolving to a declared (or external) network, and that the
/// `ports:` entries parse and are in range.
fn validate_services(file: &ComposeFile) -> Result<()> {
	for (name, service) in &file.services {
		let attached = service.networks.names();
		if service.network_mode.is_some() {
			// docker-compose: "network_mode" and "networks" cannot be combined.
			// The live engine silently honours network_mode and drops the declared
			// networks; Quadlet emits both, producing a contradictory unit.
			if !attached.is_empty() {
				return Err(ComposeError::Unsupported(format!(
					"service '{name}' sets both 'network_mode' and 'networks', which are \
					 mutually exclusive; keep one"
				)));
			}
		} else {
			// Every referenced network must be declared at the top level (or be the
			// synthesized `default`). An undefined reference is a config error in
			// docker-compose; podup otherwise prefixes it on the engine path while
			// the Quadlet exporter emits the raw name, a cross-project attach risk.
			for net in &attached {
				// `default` is the implicit project network docker-compose always
				// provides, so an explicit reference to it is valid even without a
				// top-level entry; everything else must be declared.
				if net != "default" && !file.networks.contains_key(net) {
					return Err(ComposeError::Unsupported(format!(
						"service '{name}' refers to undefined network '{net}'; declare it \
						 under the top-level 'networks:' or mark it external"
					)));
				}
			}
		}

		// Surface a malformed/out-of-range port at config time rather than letting
		// it slip through to a podman create error at run time.
		parse_ports(&service.ports)?;
	}
	Ok(())
}

/// Top-level network checks: an `external: true` network must not also carry
/// creation-time attributes (driver, IPAM, internal, …), which podman cannot
/// apply to a pre-existing network and docker-compose rejects.
fn validate_networks(file: &ComposeFile) -> Result<()> {
	for (name, cfg) in &file.networks {
		let Some(cfg) = cfg else { continue };
		if cfg.external != Some(true) {
			continue;
		}
		let mut conflicts = Vec::new();
		if cfg.driver.is_some() {
			conflicts.push("driver");
		}
		if !cfg.driver_opts.is_empty() {
			conflicts.push("driver_opts");
		}
		if cfg.internal.is_some() {
			conflicts.push("internal");
		}
		if cfg.attachable.is_some() {
			conflicts.push("attachable");
		}
		if cfg.enable_ipv6.is_some() {
			conflicts.push("enable_ipv6");
		}
		if cfg.enable_ipv4.is_some() {
			conflicts.push("enable_ipv4");
		}
		if cfg.ipam.is_some() {
			conflicts.push("ipam");
		}
		if !conflicts.is_empty() {
			return Err(ComposeError::Unsupported(format!(
				"network '{name}' is external but also sets {}; an external network is \
				 used as-is and these attributes cannot be applied to it",
				conflicts.join(", ")
			)));
		}
	}
	Ok(())
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{parse_str, parse_str_raw};

	fn file(yaml: &str) -> ComposeFile {
		parse_str_raw(yaml).unwrap()
	}

	fn validate_str(yaml: &str) -> crate::error::Result<()> {
		let mut file: ComposeFile = parse_str(yaml).unwrap();
		// Mirror the CLI: synthesize the implicit default network before validating
		// so a bare service is not flagged as referencing an undefined network.
		crate::compose::normalize_default_network(&mut file);
		super::validate(&file)
	}

	// validate_config (the `config` subcommand)

	#[test]
	fn empty_services_is_rejected() {
		let err = validate_config(&file("services: {}\n")).unwrap_err();
		assert!(format!("{err}").contains("no services"));
		// A file with no `services:` key at all is equally rejected.
		assert!(validate_config(&ComposeFile::default()).is_err());
	}

	#[test]
	fn missing_image_and_build_is_rejected() {
		let err = validate_config(&file("services:\n  web:\n    ports: ['80:80']\n")).unwrap_err();
		assert!(matches!(err, ComposeError::NoImageOrBuild(_)));
	}

	#[test]
	fn valid_minimal_file_passes() {
		validate_config(&file("services:\n  web:\n    image: nginx\n")).unwrap();
	}

	#[test]
	fn out_of_range_port_is_rejected() {
		let err = validate_config(&file(
			"services:\n  web:\n    image: nginx\n    ports: ['99999:80']\n",
		))
		.unwrap_err();
		assert!(matches!(err, ComposeError::InvalidPort(_)));
	}

	#[test]
	fn zero_port_is_rejected() {
		let err = validate_config(&file(
			"services:\n  web:\n    image: nginx\n    ports: ['0:80']\n",
		))
		.unwrap_err();
		assert!(matches!(err, ComposeError::InvalidPort(_)));
	}

	#[test]
	fn undefined_named_volume_is_rejected() {
		let err = validate_config(&file(
			"services:\n  web:\n    image: nginx\n    volumes: ['data:/x']\n",
		))
		.unwrap_err();
		assert!(format!("{err}").contains("undefined volume 'data'"));
	}

	#[test]
	fn declared_named_volume_passes() {
		validate_config(&file(
			"services:\n  web:\n    image: nginx\n    volumes: ['data:/x']\nvolumes:\n  data:\n",
		))
		.unwrap();
	}

	#[test]
	fn bind_and_anonymous_volumes_are_not_flagged() {
		// Host-path binds and anonymous volumes carry no top-level declaration.
		validate_config(&file(
			"services:\n  web:\n    image: nginx\n    volumes:\n      - ./host:/x\n      - /abs:/y\n      - /data\n",
		))
		.unwrap();
	}

	#[test]
	fn undefined_network_is_rejected() {
		let err = validate_config(&file(
			"services:\n  web:\n    image: nginx\n    networks: [backend]\n",
		))
		.unwrap_err();
		assert!(format!("{err}").contains("undefined network 'backend'"));
	}

	#[test]
	fn declared_network_passes() {
		validate_config(&file(
			"services:\n  web:\n    image: nginx\n    networks: [backend]\nnetworks:\n  backend:\n",
		))
		.unwrap();
	}

	#[test]
	fn invalid_service_name_is_rejected() {
		let err =
			validate_config(&file("services:\n  'bad name':\n    image: nginx\n")).unwrap_err();
		assert!(format!("{err}").contains("service name"));
	}

	#[test]
	fn dependency_cycle_is_rejected() {
		let err = validate_config(&file(
			"services:\n  a:\n    image: x\n    depends_on: [b]\n  b:\n    image: y\n    depends_on: [a]\n",
		))
		.unwrap_err();
		assert!(matches!(err, ComposeError::CircularDependency(_)));
	}

	#[test]
	fn dangling_required_dependency_is_rejected() {
		let err = validate_config(&file(
			"services:\n  web:\n    image: nginx\n    depends_on: [ghost]\n",
		))
		.unwrap_err();
		assert!(matches!(err, ComposeError::ServiceNotFound(_)));
	}

	// validate (the post-parse semantic pass)

	#[test]
	fn network_mode_with_networks_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    network_mode: host\n    networks: [front]\nnetworks:\n  front:\n";
		let err = validate_str(yaml).unwrap_err();
		assert!(err.to_string().contains("mutually exclusive"), "got: {err}");
	}

	#[test]
	fn network_mode_alone_is_accepted() {
		let yaml = "services:\n  web:\n    image: x\n    network_mode: host\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn undefined_network_reference_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [missing]\n";
		let err = validate_str(yaml).unwrap_err();
		assert!(
			err.to_string().contains("undefined network 'missing'"),
			"got: {err}"
		);
	}

	#[test]
	fn declared_network_reference_is_accepted() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [front]\nnetworks:\n  front:\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn bare_service_default_network_is_accepted() {
		// No networks declared at all: the synthesized `default` must satisfy the
		// reference check, not trip it.
		let yaml = "services:\n  web:\n    image: x\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn explicit_default_network_reference_is_accepted() {
		// `default` is the implicit project network; referencing it without a
		// top-level entry must not be flagged as undefined.
		let yaml = "services:\n  web:\n    image: x\n    networks: [default]\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn external_network_with_internal_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [ext]\nnetworks:\n  ext:\n    external: true\n    internal: true\n";
		let err = validate_str(yaml).unwrap_err();
		let msg = err.to_string();
		assert!(msg.contains("external"), "got: {msg}");
		assert!(msg.contains("internal"), "got: {msg}");
	}

	#[test]
	fn external_network_with_ipam_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [ext]\nnetworks:\n  ext:\n    external: true\n    ipam:\n      config:\n        - subnet: 10.0.0.0/24\n";
		let err = validate_str(yaml).unwrap_err();
		assert!(err.to_string().contains("ipam"), "got: {err}");
	}

	#[test]
	fn plain_external_network_is_accepted() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [ext]\nnetworks:\n  ext:\n    external: true\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn external_network_with_name_only_is_accepted() {
		let yaml = "services:\n  web:\n    image: x\n    networks: [ext]\nnetworks:\n  ext:\n    external: true\n    name: shared_net\n";
		assert!(validate_str(yaml).is_ok());
	}

	#[test]
	fn out_of_range_short_port_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    ports: ['99999:80']\n";
		assert!(validate_str(yaml).is_err());
	}

	#[test]
	fn invalid_port_protocol_is_rejected() {
		let yaml = "services:\n  web:\n    image: x\n    ports: ['80/banana']\n";
		assert!(validate_str(yaml).is_err());
	}
}