Skip to content

Commit

Permalink
Make findDockerContainer() more robust when containers vanish concurr…
Browse files Browse the repository at this point in the history
…ently
  • Loading branch information
bwaldvogel committed Oct 11, 2024
1 parent af02f42 commit b9f8b73
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/de/cronn/postgres/snapshot/util/PostgresUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;

final class PostgresUtils {
Expand Down Expand Up @@ -120,15 +121,20 @@ static ContainerAndNetwork findDockerContainer(String host) {

return client.listContainersCmd().exec().stream()
.map(container -> {
InspectContainerResponse inspectContainerResponse = client.inspectContainerCmd(container.getId()).exec();
for (ContainerNetwork network : inspectContainerResponse.getNetworkSettings().getNetworks().values()) {
if (network.getAliases() != null && network.getAliases().contains(host)) {
log.debug("Found Docker container {} with network {}",
String.join(", ", container.getNames()), network.getNetworkID());
return new ContainerAndNetwork(inspectContainerResponse, network);
try {
InspectContainerResponse inspectContainerResponse = client.inspectContainerCmd(container.getId()).exec();
for (ContainerNetwork network : inspectContainerResponse.getNetworkSettings().getNetworks().values()) {
if (network.getAliases() != null && network.getAliases().contains(host)) {
log.debug("Found Docker container {} with network {}",
String.join(", ", container.getNames()), network.getNetworkID());
return new ContainerAndNetwork(inspectContainerResponse, network);
}
}
return null;
} catch (NotFoundException e) {
log.debug("Docker container '{}' seems to be gone", container.getId(), e);
return null;
}
return null;
})
.filter(Objects::nonNull)
.findFirst()
Expand Down

0 comments on commit b9f8b73

Please sign in to comment.