Commit 231bae0e authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Fix Container-Network connecting in docker.py

parent 22c446ec
Loading
Loading
Loading
Loading
+13 −16
Original line number Diff line number Diff line
@@ -184,7 +184,7 @@ class Container(Item):
		self.cid: str|None = None

		if network:
			self.networks[network] = Container.DEFAULT_ALIASES
			self.connect(network, *self.DEFAULT_ALIASES)

	def __enter__(self) -> Container:
		return self
@@ -225,8 +225,8 @@ class Container(Item):
		if self.cid is not None:
			return self.cid

		networks = set[Network]()
		opts: MutableArguments = [
			"--network=none",
			*(
				(f"--volume={vol[0]}:{vol[1]}" if isinstance(vol, tuple) else f"--volume={vol}")
				for vol in self.volumes
@@ -236,14 +236,13 @@ class Container(Item):

		if self.entrypoint:
			opts.append(f"--entrypoint={self.entrypoint}")
		if self.networks:
			networks.update(self.networks)
			net = networks.pop()
			opts.append(f"--network={net}")
			opts.extend(f"--network-alias={alias}" for alias in self.networks[net])

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

		# Disconnect the "none" network specified as the starting network
		docker_quiet("network", "disconnect", "none", self.cid)

		return self.cid

	def start(self) -> None:
@@ -270,17 +269,15 @@ class Container(Item):
		Any aliases supplied will be resolvable to the container by other containers on the
		network.
		"""
		is_running = self.is_running()
		cid = self.get_id()
		if network in self.networks:
			if self.networks[network] == aliases:
				return
			if is_running:
				docker('network', 'disconnect', str(network), self.get_id())
		if is_running:
			docker('network', 'disconnect', str(network), cid)
		docker(
			'network', 'connect',
			*(f'--alias={a}' for a in aliases),
				str(network), self.get_id(),
			str(network), cid,
		)
		self.networks[network] = aliases