fix(docker): fail a port-publishing container whose network does not exist (#5726)

* fix(docker): fail a port-publishing container whose network does not exist

A container that publishes ports is created on the default bridge and
joins its service network only after start, and startCreated logged a
failed join and carried on. When the network does not exist, typically
a mistyped FLOCI_SERVICES_DOCKER_NETWORK, the container kept running on
the bridge alone, out of reach of the network it was meant for, while
its resource reported itself ready. A container that does not publish
ports joins its network at creation and already fails there.

startCreated now throws when Docker reports the network as not found,
and createAndStart removes the container as for any failed start. Other
join failures are still only logged.

* fix(docker): keep Docker's reason when joining a network returns 404
This commit is contained in:
dkatavic authored and GitHub committed 2026-10-16 17:45:33 -05:00
1 parent 1d1479f9fb
commit 84e5bed623
2 files changed
+36

No files matched your search

@@ -393,6 +393,13 @@ public class ContainerLifecycleManager {
.withNetworkId(spec.networkMode())
.exec();
LOG.debugv("Connected container {0} to network {1}", containerId, spec.networkMode());
} catch (NotFoundException e) {
// Docker answers 404 when the network does not exist, which fails a container that
// joins at creation, or when the container is already gone. Either way it cannot
// reach its network, so fail the start instead of leaving it on the default bridge.
// Docker's message says which of the two was missing.
throw new IllegalStateException("Could not connect container " + containerId
+ " to Docker network " + spec.networkMode() + ": " + e.getMessage(), e);
} catch (Exception e) {
LOG.warnv("Could not connect container {0} to network {1}: {2}",
containerId, spec.networkMode(), e.getMessage());
@@ -10,6 +10,7 @@ import com.github.dockerjava.api.command.InspectContainerCmd;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.RemoveContainerCmd;
import com.github.dockerjava.api.command.StartContainerCmd;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.NetworkSettings;
@@ -130,6 +131,34 @@ class ContainerLifecycleManagerNetworkTest {
verify(dockerClient).removeContainerCmd("container-id");
}
@Test
void createAndStartFailsAndRemovesThePublishingContainerWhenItsNetworkDoesNotExist() {
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
RemoveContainerCmd removeCmd = mock(RemoveContainerCmd.class, RETURNS_SELF);
when(dockerClient.removeContainerCmd("container-id")).thenReturn(removeCmd);
doThrow(new NotFoundException("network floci-nte not found")).when(connectCmd).exec();
IllegalStateException e = assertThrows(IllegalStateException.class,
() -> manager().createAndStart(spec("floci-nte", Map.of(8080, 18080), List.of())));
assertTrue(e.getMessage().contains("Docker network floci-nte"), e.getMessage());
assertTrue(e.getMessage().contains("network floci-nte not found"), e.getMessage());
verify(removeCmd).exec();
}
@Test
void createAndStartKeepsThePublishingContainerWhenJoiningAnExistingNetworkFails() {
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
doThrow(new RuntimeException("endpoint already exists")).when(connectCmd).exec();
NetworkSettings networks = mock(NetworkSettings.class);
InspectContainerResponse inspect = inspectOf("container-id");
when(inspect.getNetworkSettings()).thenReturn(networks);
manager().createAndStart(spec("floci-net", Map.of(8080, 18080), List.of()));
verify(dockerClient, never()).removeContainerCmd(any(String.class));
}
@Test
void createLeavesNetworkAloneWithoutLinkLocalIps() {
manager().create(new ContainerSpec("busybox:stable"));