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 authored and holly-cummins committed Feb 8, 2024
1 parent 52fd161 commit 7037a57
Show file tree
Hide file tree
Showing 2 changed files with 75 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.isInWindowsWSL()) {
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 All @@ -27,6 +28,10 @@ public final class ContainerRuntimeUtil {
* runtime and the container runtime would be detected again and again unnecessarily.
*/
private static final String CONTAINER_RUNTIME_SYS_PROP = "quarkus-local-container-runtime";
/**
* Defines the maximum number of characters to read from the output of the `docker info` command.
*/
private static final int MAX_ANTICIPATED_CHARACTERS_IN_DOCKER_INFO = 3000;

private ContainerRuntimeUtil() {
}
Expand Down Expand Up @@ -112,14 +117,69 @@ private static ContainerRuntime getContainerRuntimeEnvironment() {
}

private static ContainerRuntime fullyResolveContainerRuntime(ContainerRuntime containerRuntimeEnvironment) {
boolean rootless = getRootlessStateFor(containerRuntimeEnvironment);
boolean rootless = false;
boolean isInWindowsWSL = 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)) {
bufferedReader.mark(MAX_ANTICIPATED_CHARACTERS_IN_DOCKER_INFO);
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 (!rootless) {
return containerRuntimeEnvironment;
if (SystemUtils.IS_OS_LINUX && containerRuntimeEnvironment.isDocker()) {
stringPredicate = line -> line.trim().contains("WSL");
bufferedReader.reset();
isInWindowsWSL = 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();
}
}

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

if (isInWindowsWSL) {
return ContainerRuntime.WSL;
}

return containerRuntimeEnvironment;
}

private static ContainerRuntime loadContainerRuntimeFromSystemProperty() {
Expand Down Expand Up @@ -168,57 +228,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 +262,11 @@ public boolean isDocker() {
}

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

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

public boolean isRootless() {
Expand Down

0 comments on commit 7037a57

Please sign in to comment.