mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): create a publishing container on its network off Docker Desktop (#7757)
* fix(docker): create a publishing container on its network off Docker Desktop A container that publishes a host port was created on the default bridge and connected to its configured network after it started, a workaround for Docker Desktop, so on any other daemon it stayed on both. Off Docker Desktop it is now created on its network and joins it alone; Docker Desktop, and a daemon that cannot be asked, keep the connect after start. * fix(docker): keep the daemon answer when it cannot be asked, so create and start agree A failed GET /info fell back to the Docker Desktop path without keeping that answer. If the daemon answered as Docker Engine by the time the container started, it was created on the default bridge and never connected to its network. The first answer, the fallback included, is now kept.
This commit is contained in:
1 parent
767f50e13f
commit
a3a3efec32
4 files changed
+133
-17
No files matched your search
+66
@@ -7,6 +7,7 @@ import com.github.dockerjava.api.command.CreateContainerCmd;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
import com.github.dockerjava.api.command.CreateNetworkCmd;
|
||||
import com.github.dockerjava.api.command.DisconnectFromNetworkCmd;
|
||||
import com.github.dockerjava.api.command.InfoCmd;
|
||||
import com.github.dockerjava.api.command.InspectContainerCmd;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.command.InspectNetworkCmd;
|
||||
@@ -18,6 +19,7 @@ import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import com.github.dockerjava.api.model.Network;
|
||||
import com.github.dockerjava.api.model.NetworkSettings;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
@@ -171,6 +173,62 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
verify(dockerClient, never()).removeContainerCmd(any(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartPutsAPublishingContainerOnItsNetworkAloneOnDockerEngine() {
|
||||
daemonRuns("Ubuntu 24.04.1 LTS");
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getNetworkSettings()).thenReturn(mock(NetworkSettings.class));
|
||||
|
||||
manager().createAndStart(spec("floci-net", Map.of(8080, 18080), List.of()));
|
||||
|
||||
HostConfig hostConfig = createdHostConfig();
|
||||
assertEquals("floci-net", hostConfig.getNetworkMode());
|
||||
assertTrue(hostConfig.getPortBindings().getBindings().containsKey(ExposedPort.tcp(8080)));
|
||||
verify(connectCmd, never()).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartConnectsAPublishingContainerAfterStartOnDockerDesktop() {
|
||||
daemonRuns("Docker Desktop");
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getNetworkSettings()).thenReturn(mock(NetworkSettings.class));
|
||||
|
||||
manager().createAndStart(spec("floci-net", Map.of(8080, 18080), List.of()));
|
||||
|
||||
assertNull(createdHostConfig().getNetworkMode());
|
||||
verify(connectCmd).withNetworkId("floci-net");
|
||||
verify(connectCmd).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartStillConnectsAPublishingContainerWhenTheDaemonAnswersOnlyAfterCreate() {
|
||||
InfoCmd infoCmd = mock(InfoCmd.class);
|
||||
Info info = mock(Info.class);
|
||||
lenient().when(dockerClient.infoCmd()).thenReturn(infoCmd);
|
||||
lenient().when(infoCmd.exec()).thenThrow(new RuntimeException("daemon busy")).thenReturn(info);
|
||||
lenient().when(info.getOperatingSystem()).thenReturn("Ubuntu 24.04.1 LTS");
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getNetworkSettings()).thenReturn(mock(NetworkSettings.class));
|
||||
|
||||
manager().createAndStart(spec("floci-net", Map.of(8080, 18080), List.of()));
|
||||
|
||||
assertNull(createdHostConfig().getNetworkMode());
|
||||
verify(connectCmd).withNetworkId("floci-net");
|
||||
verify(connectCmd).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReconnectsAPublishingContainerWithItsLinkLocalEndpointOnDockerEngine() {
|
||||
daemonRuns("Ubuntu 24.04.1 LTS");
|
||||
|
||||
manager().create(specWithLinkLocalIp(Map.of(8080, 18080)));
|
||||
|
||||
assertEquals("test-network", createdHostConfig().getNetworkMode());
|
||||
InOrder order = inOrder(disconnectCmd, connectCmd);
|
||||
order.verify(disconnectCmd).exec();
|
||||
order.verify(connectCmd).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createLeavesNetworkAloneWithoutLinkLocalIps() {
|
||||
manager().create(new ContainerSpec("busybox:stable"));
|
||||
@@ -416,6 +474,14 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
return inspect;
|
||||
}
|
||||
|
||||
private void daemonRuns(String operatingSystem) {
|
||||
InfoCmd infoCmd = mock(InfoCmd.class);
|
||||
Info info = mock(Info.class);
|
||||
when(dockerClient.infoCmd()).thenReturn(infoCmd);
|
||||
when(infoCmd.exec()).thenReturn(info);
|
||||
when(info.getOperatingSystem()).thenReturn(operatingSystem);
|
||||
}
|
||||
|
||||
private HostConfig createdHostConfig() {
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
|
||||
Reference in new issue
Block a user