Pull alpine image in a retry in Docker tests (#88654)

Closes #88651. When using an `alpine` image to perform container
fiddling, first explicitly pull the image and do so in a loop, in an
attempt to make things more robust.
This commit is contained in:
Rory Hunter 2022-07-25 09:41:17 +01:00 committed by GitHub
parent bbc10e976d
commit 2a656836d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -347,6 +347,9 @@ public class Docker {
* @param containerPath The path to mount localPath inside the container.
*/
private static void executePrivilegeEscalatedShellCmd(String shellCmd, Path localPath, Path containerPath) {
final String image = "alpine:3.13";
ensureImageIsPulled(image);
final List<String> args = new ArrayList<>();
args.add("docker run");
@ -358,7 +361,7 @@ public class Docker {
args.add("--volume \"" + localPath.getParent() + ":" + containerPath.getParent() + "\"");
// Use a lightweight musl libc based small image
args.add("alpine:3.13");
args.add(image);
// And run inline commands via the POSIX shell
args.add("/bin/sh -c \"" + shellCmd + "\"");
@ -368,6 +371,33 @@ public class Docker {
sh.run(command);
}
private static void ensureImageIsPulled(String image) {
// Don't pull if the image already exists. This does also mean that we never refresh it, but that
// isn't an issue in CI.
if (sh.runIgnoreExitCode("docker image inspect -f '{{ .Id }}' " + image).isSuccess()) {
return;
}
Shell.Result result = null;
int i = 0;
while (true) {
result = sh.runIgnoreExitCode("docker pull " + image);
if (result.isSuccess()) {
return;
}
if (++i == 3) {
throw new RuntimeException("Failed to pull Docker image [" + image + "]: " + result);
}
try {
Thread.sleep(10_000L);
} catch (InterruptedException e) {
// ignore
}
}
}
/**
* Create a directory with specified uid/gid using Docker backed privilege escalation.
* @param localPath The path to the directory to create.