Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic rootless podman support #5822

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.testcontainers.dockerclient;

import com.sun.jna.Library;
import com.sun.jna.Native;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

/**
*
* @deprecated this class is used by the SPI and should not be used directly
*/
@Deprecated
@Slf4j
public final class RootlessPodmanClientProviderStrategy extends DockerClientProviderStrategy {

public static final int PRIORITY = UnixSocketClientProviderStrategy.PRIORITY + 1;

@Getter(lazy = true)
@Nullable
private final Path socketPath = resolveSocketPath();

private Path resolveSocketPath() {
return tryEnv()
.orElseGet(() -> {
Path implicitPath = Paths.get("/run/user/" + LibC.INSTANCE.getuid());
return tryFolder(implicitPath).orElse(null);
});
}

private Optional<Path> tryEnv() {
String xdgRuntimeDir = System.getenv("XDG_RUNTIME_DIR");
if (StringUtils.isBlank(xdgRuntimeDir)) {
log.debug("$XDG_RUNTIME_DIR is not set.");
return Optional.empty();
}
Path path = Paths.get(xdgRuntimeDir);
if (!Files.exists(path)) {
log.debug("$XDG_RUNTIME_DIR is set to '{}' but the folder does not exist.", path);
return Optional.empty();
}
Path podmanSocketPath = path.resolve("podman/podman.sock");
if (!Files.exists(podmanSocketPath)) {
log.debug("$XDG_RUNTIME_DIR is set but '{}' does not exist.", podmanSocketPath);
return Optional.empty();
}
return Optional.of(podmanSocketPath);
}

private Optional<Path> tryFolder(Path path) {
if (!Files.exists(path)) {
log.debug("'{}' does not exist.", path);
return Optional.empty();
}
Path podmanSocketPath = path.resolve("podman/podman.sock");
if (!Files.exists(podmanSocketPath)) {
log.debug("'{}' does not exist.", podmanSocketPath);
return Optional.empty();
}
return Optional.of(podmanSocketPath);
}

@Override
public TransportConfig getTransportConfig() throws InvalidConfigurationException {
return TransportConfig.builder().dockerHost(URI.create("unix://" + getSocketPath().toString())).build();
}

@Override
protected boolean isApplicable() {
return SystemUtils.IS_OS_LINUX && getSocketPath() != null && Files.exists(getSocketPath());
}

@Override
public String getDescription() {
return "Rootless Podman accessed via Unix socket (" + getSocketPath() + ")";
}

@Override
protected int getPriority() {
return PRIORITY;
}

private interface LibC extends Library {
LibC INSTANCE = Native.loadLibrary("c", LibC.class);
int getuid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
org.testcontainers.dockerclient.DockerMachineClientProviderStrategy
org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy
org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy
org.testcontainers.dockerclient.RootlessPodmanClientProviderStrategy
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public DockerImageName createImage(String originalImage, String tag) {
.withTag(tag);

// push the image to the registry
client.tagImageCmd(dummyImageId, imageName.asCanonicalNameString(), tag).exec();
client.tagImageCmd(dummyImageId, imageName.getUnversionedPart(), tag).exec();

client
.pushImageCmd(imageName.asCanonicalNameString())
Expand Down