Commit 52810e10 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add "publish" keyword to Container.__init__

This is, depending on the (non-falsey) value type, equivalent to
passing the "--publish-all" or "--publish=" arguments to the `docker
create` command.
parent 0b9aa779
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -196,6 +196,7 @@ class Container(Item):
		network: Network|None = None,
		entrypoint: HostMount|Argument|None = None,
		privileged: bool = False,
		publish: bool|list[int] = False,
	):
		if isinstance(entrypoint, tuple):
			volumes = [*volumes, entrypoint]
@@ -207,6 +208,7 @@ class Container(Item):
		self.env = env
		self.entrypoint = entrypoint
		self.privileged = privileged
		self.publish = publish
		self.networks = dict[Network, Tuple[str, ...]]()
		self.cid: str|None = None

@@ -260,8 +262,7 @@ class Container(Item):
			return self.cid

		opts: MutableArguments = [
			b"--network=none",
			*(f"--env={name}={val}" for name, val in self.env.items()),
			f"--env={name}={val}" for name, val in self.env.items()
		]

		for vol in self.volumes:
@@ -283,10 +284,18 @@ class Container(Item):
		if self.privileged:
			opts.append(b"--privileged")

		if isinstance(self.publish, list):
			opts.extend(f"--publish={p}" for p in self.publish)
		elif self.publish:
			opts.append(b"--publish-all")
		else:
			opts.append(b"--network=none")

		self.cid = docker_output(b"container", b"create", *opts, self.image.iid, *self.cmd)
		assert self.cid

		# Disconnect the "none" network specified as the starting network
		if not self.publish:
			docker_quiet(b"network", b"disconnect", b"none", self.cid)

		return self.cid