Skip to content

Commit b9f8b73

Browse files
committed
Make findDockerContainer() more robust when containers vanish concurrently
1 parent af02f42 commit b9f8b73

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main/java/de/cronn/postgres/snapshot/util/PostgresUtils.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.github.dockerjava.api.DockerClient;
2525
import com.github.dockerjava.api.command.InspectContainerResponse;
26+
import com.github.dockerjava.api.exception.NotFoundException;
2627
import com.github.dockerjava.api.model.ContainerNetwork;
2728

2829
final class PostgresUtils {
@@ -120,15 +121,20 @@ static ContainerAndNetwork findDockerContainer(String host) {
120121

121122
return client.listContainersCmd().exec().stream()
122123
.map(container -> {
123-
InspectContainerResponse inspectContainerResponse = client.inspectContainerCmd(container.getId()).exec();
124-
for (ContainerNetwork network : inspectContainerResponse.getNetworkSettings().getNetworks().values()) {
125-
if (network.getAliases() != null && network.getAliases().contains(host)) {
126-
log.debug("Found Docker container {} with network {}",
127-
String.join(", ", container.getNames()), network.getNetworkID());
128-
return new ContainerAndNetwork(inspectContainerResponse, network);
124+
try {
125+
InspectContainerResponse inspectContainerResponse = client.inspectContainerCmd(container.getId()).exec();
126+
for (ContainerNetwork network : inspectContainerResponse.getNetworkSettings().getNetworks().values()) {
127+
if (network.getAliases() != null && network.getAliases().contains(host)) {
128+
log.debug("Found Docker container {} with network {}",
129+
String.join(", ", container.getNames()), network.getNetworkID());
130+
return new ContainerAndNetwork(inspectContainerResponse, network);
131+
}
129132
}
133+
return null;
134+
} catch (NotFoundException e) {
135+
log.debug("Docker container '{}' seems to be gone", container.getId(), e);
136+
return null;
130137
}
131-
return null;
132138
})
133139
.filter(Objects::nonNull)
134140
.findFirst()

0 commit comments

Comments
 (0)