Skip to content

Commit

Permalink
Use --interactive option when using docker inside Windows' WSL
Browse files Browse the repository at this point in the history
There seems to be an issue with docker printing output when running in
Windows' WSL without `--interactive`.

Closes quarkusio#37272
  • Loading branch information
zakkak committed Nov 29, 2023
1 parent 7cc02ec commit 3a6c35b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public NativeImageBuildLocalContainerRunner(NativeConfig nativeConfig) {
super(nativeConfig);
if (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) {
final ArrayList<String> containerRuntimeArgs = new ArrayList<>(Arrays.asList(baseContainerRuntimeArgs));
if (containerRuntime.isWSL()) {
containerRuntimeArgs.add("--interactive");
}
if (containerRuntime.isDocker() && containerRuntime.isRootless()) {
Collections.addAll(containerRuntimeArgs, "--user", String.valueOf(0));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang3.SystemUtils;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

Expand Down Expand Up @@ -112,14 +113,67 @@ private static ContainerRuntime getContainerRuntimeEnvironment() {
}

private static ContainerRuntime fullyResolveContainerRuntime(ContainerRuntime containerRuntimeEnvironment) {
boolean rootless = getRootlessStateFor(containerRuntimeEnvironment);
boolean rootless = false;
boolean needsInteractive = false;
Process rootlessProcess = null;
ProcessBuilder pb = null;
try {
pb = new ProcessBuilder(containerRuntimeEnvironment.getExecutableName(), "info").redirectErrorStream(true);
rootlessProcess = pb.start();
int exitCode = rootlessProcess.waitFor();
if (exitCode != 0) {
log.warnf("Command \"%s\" exited with error code %d. " +
"Rootless container runtime detection might not be reliable or the container service is not running at all.",
String.join(" ", pb.command()), exitCode);
}
try (InputStream inputStream = rootlessProcess.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
if (exitCode != 0) {
log.debugf("Command \"%s\" output: %s", String.join(" ", pb.command()),
bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())));
} else {
Predicate<String> stringPredicate;
// Docker includes just "rootless" under SecurityOptions, while podman includes "rootless: <boolean>"
if (containerRuntimeEnvironment.isDocker()) {
// We also treat Docker Desktop as "rootless" since the way it binds mounts does not
// transparently map the host user ID and GID
// see https://docs.docker.com/desktop/faqs/linuxfaqs/#how-do-i-enable-file-sharing
stringPredicate = line -> line.trim().equals("rootless") || line.contains("Docker Desktop")
|| line.contains("desktop-linux");
} else {
stringPredicate = line -> line.trim().equals("rootless: true");
}
rootless = bufferedReader.lines().anyMatch(stringPredicate);

if (SystemUtils.IS_OS_LINUX && containerRuntimeEnvironment.isDocker()) {
stringPredicate = line -> line.trim().contains("WSL");
needsInteractive = bufferedReader.lines().anyMatch(stringPredicate);
}
}
}
} catch (IOException | InterruptedException e) {
// If an exception is thrown in the process, assume we are not running rootless (default docker installation)
log.debugf(e, "Failure to read info output from %s", String.join(" ", pb.command()));
} finally {
if (rootlessProcess != null) {
rootlessProcess.destroy();
}
}

if (rootless) {
if (needsInteractive) {
return ContainerRuntime.WSL_ROOTLESS;
}
return containerRuntimeEnvironment == ContainerRuntime.DOCKER ? ContainerRuntime.DOCKER_ROOTLESS
: ContainerRuntime.PODMAN_ROOTLESS;
}

if (!rootless) {
return containerRuntimeEnvironment;
if (needsInteractive) {
return ContainerRuntime.WSL;
}

return containerRuntimeEnvironment == ContainerRuntime.DOCKER ? ContainerRuntime.DOCKER_ROOTLESS
: ContainerRuntime.PODMAN_ROOTLESS;
return containerRuntimeEnvironment;
}

private static ContainerRuntime loadContainerRuntimeFromSystemProperty() {
Expand Down Expand Up @@ -168,57 +222,14 @@ private static String getVersionOutputFor(ContainerRuntime containerRuntime) {
}
}

private static boolean getRootlessStateFor(ContainerRuntime containerRuntime) {
Process rootlessProcess = null;
ProcessBuilder pb = null;
try {
pb = new ProcessBuilder(containerRuntime.getExecutableName(), "info").redirectErrorStream(true);
rootlessProcess = pb.start();
int exitCode = rootlessProcess.waitFor();
if (exitCode != 0) {
log.warnf("Command \"%s\" exited with error code %d. " +
"Rootless container runtime detection might not be reliable or the container service is not running at all.",
String.join(" ", pb.command()), exitCode);
}
try (InputStream inputStream = rootlessProcess.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
if (exitCode != 0) {
log.debugf("Command \"%s\" output: %s", String.join(" ", pb.command()),
bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())));
return false;
} else {
final Predicate<String> stringPredicate;
// Docker includes just "rootless" under SecurityOptions, while podman includes "rootless: <boolean>"
if (containerRuntime == ContainerRuntime.DOCKER) {
// We also treat Docker Desktop as "rootless" since the way it binds mounts does not
// transparently map the host user ID and GID
// see https://docs.docker.com/desktop/faqs/linuxfaqs/#how-do-i-enable-file-sharing
stringPredicate = line -> line.trim().equals("rootless") || line.contains("Docker Desktop")
|| line.contains("desktop-linux");
} else {
stringPredicate = line -> line.trim().equals("rootless: true");
}
return bufferedReader.lines().anyMatch(stringPredicate);
}
}
} catch (IOException | InterruptedException e) {
// If an exception is thrown in the process, assume we are not running rootless (default docker installation)
log.debugf(e, "Failure to read info output from %s", String.join(" ", pb.command()));
return false;
} finally {
if (rootlessProcess != null) {
rootlessProcess.destroy();
}
}
}

/**
* Supported Container runtimes
*/
public enum ContainerRuntime {
DOCKER("docker", false),
DOCKER_ROOTLESS("docker", true),
WSL("docker", false),
WSL_ROOTLESS("docker", false),
PODMAN("podman", false),
PODMAN_ROOTLESS("podman", true),
UNAVAILABLE(null, false);
Expand All @@ -245,7 +256,11 @@ public boolean isDocker() {
}

public boolean isPodman() {
return this == PODMAN || this == PODMAN_ROOTLESS;
return this.executableName.equals("docker");
}

public boolean isWSL() {
return this == WSL || this == WSL_ROOTLESS;
}

public boolean isRootless() {
Expand Down

0 comments on commit 3a6c35b

Please sign in to comment.