Skip to content

Commit

Permalink
Detect container runtime in when using Jib
Browse files Browse the repository at this point in the history
The detection uses the same method Quarkus already
used when building the native binary using a container build

Fixes: quarkusio#24231
(cherry picked from commit a6745e0)
  • Loading branch information
geoand authored and gsmet committed Mar 14, 2022
1 parent 17095d2 commit 35d11ed
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Optional;
import java.util.OptionalInt;

import io.quarkus.deployment.util.ContainerRuntimeUtil;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
Expand Down Expand Up @@ -69,7 +70,7 @@ public class NativeConfig {
* It also serves as the default Locale language for the native executable application runtime.
* e.g. en or cs as defined by IETF BCP 47 language tags.
* <p>
*
*
* @deprecated Use the global quarkus.default-locale.
*/
@ConfigItem
Expand All @@ -82,7 +83,7 @@ public class NativeConfig {
* It also serves as the default Locale country for the native executable application runtime.
* e.g. US or FR as defined by ISO 3166-1 alpha-2 codes.
* <p>
*
*
* @deprecated Use the global quarkus.default-locale.
*/
@ConfigItem
Expand Down Expand Up @@ -429,7 +430,10 @@ public static class Compression {

/**
* Supported Container runtimes
*
* @deprecated Use {@link ContainerRuntimeUtil.ContainerRuntime} instead.
*/
@Deprecated(since = "2.7", forRemoval = true)
public static enum ContainerRuntime {
DOCKER,
PODMAN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jboss.logging.Logger;

import io.quarkus.deployment.pkg.NativeConfig;
import io.quarkus.deployment.util.ContainerRuntimeUtil;
import io.quarkus.deployment.util.FileUtil;
import io.quarkus.deployment.util.ProcessUtil;

Expand Down Expand Up @@ -130,7 +131,9 @@ protected String[] buildCommand(String dockerCmd, List<String> containerRuntimeA
* if the podman
* executable exists in the environment or if the docker executable is an alias to podman
* @throws IllegalStateException if no container runtime was found to build the image
* @deprecated Use {@link ContainerRuntimeUtil#detectContainerRuntime()} instead
*/
@Deprecated(since = "2.7", forRemoval = true)
public static NativeConfig.ContainerRuntime detectContainerRuntime() {
// Docker version 19.03.14, build 5eb3275d40
String dockerVersionOutput = getVersionOutputFor(NativeConfig.ContainerRuntime.DOCKER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.quarkus.deployment.util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.jboss.logging.Logger;

public final class ContainerRuntimeUtil {

private static final Logger log = Logger.getLogger(ContainerRuntimeUtil.class);

private ContainerRuntimeUtil() {
}

/**
* @return {@link ContainerRuntime#DOCKER} if it's available, or {@link ContainerRuntime#PODMAN}
* if the podman
* executable exists in the environment or if the docker executable is an alias to podman
* @throws IllegalStateException if no container runtime was found to build the image
*/
public static ContainerRuntime detectContainerRuntime() {
// Docker version 19.03.14, build 5eb3275d40
String dockerVersionOutput = getVersionOutputFor(ContainerRuntime.DOCKER);
boolean dockerAvailable = dockerVersionOutput.contains("Docker version");
// Check if Podman is installed
// podman version 2.1.1
String podmanVersionOutput = getVersionOutputFor(ContainerRuntime.PODMAN);
boolean podmanAvailable = podmanVersionOutput.startsWith("podman version");
if (dockerAvailable) {
// Check if "docker" is an alias to "podman"
if (dockerVersionOutput.equals(podmanVersionOutput)) {
return ContainerRuntime.PODMAN;
}
return ContainerRuntime.DOCKER;
} else if (podmanAvailable) {
return ContainerRuntime.PODMAN;
} else {
throw new IllegalStateException("No container runtime was found to. "
+ "Make sure you have Docker or Podman installed in your environment.");
}
}

private static String getVersionOutputFor(ContainerRuntime containerRuntime) {
Process versionProcess = null;
try {
ProcessBuilder pb = new ProcessBuilder(containerRuntime.getExecutableName(), "--version")
.redirectErrorStream(true);
versionProcess = pb.start();
versionProcess.waitFor();
return new String(FileUtil.readFileContents(versionProcess.getInputStream()), StandardCharsets.UTF_8);
} catch (IOException | InterruptedException e) {
// If an exception is thrown in the process, just return an empty String
log.debugf(e, "Failure to read version output from %s", containerRuntime.getExecutableName());
return "";
} finally {
if (versionProcess != null) {
versionProcess.destroy();
}
}
}

/**
* Supported Container runtimes
*/
public enum ContainerRuntime {
DOCKER,
PODMAN;

public String getExecutableName() {
return this.name().toLowerCase();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import io.quarkus.deployment.pkg.builditem.UpxCompressedBuildItem;
import io.quarkus.deployment.pkg.steps.JarResultBuildStep;
import io.quarkus.deployment.pkg.steps.NativeBuild;
import io.quarkus.deployment.util.ContainerRuntimeUtil;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.dependency.ResolvedDependency;

Expand Down Expand Up @@ -266,6 +267,11 @@ private Containerizer createContainerizer(ContainerImageConfig containerImageCon
dockerDaemonImage.setDockerExecutable(Paths.get(jibConfigExecutableName.get()));
} else if (dockerConfigExecutableName.isPresent()) {
dockerDaemonImage.setDockerExecutable(Paths.get(dockerConfigExecutableName.get()));
} else {
// detect the container runtime instead of falling back to 'docker' as the default
ContainerRuntimeUtil.ContainerRuntime detectedContainerRuntime = ContainerRuntimeUtil.detectContainerRuntime();
log.infof("Using %s to run the native image builder", detectedContainerRuntime.getExecutableName());
dockerDaemonImage.setDockerExecutable(Paths.get(detectedContainerRuntime.getExecutableName()));
}
containerizer = Containerizer.to(dockerDaemonImage);
}
Expand Down

0 comments on commit 35d11ed

Please sign in to comment.