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 support for Windows containers (WCOW) #1898

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static Map<String, String> markerLabels() {

private String activeApiVersion;

private boolean runningWindowsContainers;

@Getter(lazy = true)
private final boolean fileMountingSupported = checkMountableFile();

Expand Down Expand Up @@ -212,6 +214,8 @@ public void close() {
Version version = client.versionCmd().exec();
log.debug("Docker version: {}", version.getRawValues());
activeApiVersion = version.getApiVersion();
String osType = dockerInfo.getOsType();
runningWindowsContainers = StringUtils.isNotBlank(osType) && osType.equals("windows");

String serverInfo =
"Connected to docker: \n" +
Expand All @@ -223,6 +227,7 @@ public void close() {
"\n" +
" Operating System: " +
dockerInfo.getOperatingSystem() +
(runningWindowsContainers ? " (WCOW)" : "") +
"\n" +
" Total Memory: " +
dockerInfo.getMemTotal() /
Expand Down Expand Up @@ -383,6 +388,14 @@ public String getActiveApiVersion() {
return activeApiVersion;
}

/**
* @return whether the daemon is running Windows containers
*/
public boolean isRunningWindowsContainers() {
client();
return runningWindowsContainers;
}

/**
* @return the docker execution driver of the daemon that we have connected to
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public boolean allowUserOverrides() {
}

/**
/* @return the path under which the Docker unix socket is reachable relative to the Docker daemon
*/
* @return the path under which the Docker unix socket is reachable relative to the Docker daemon
*/
public String getRemoteDockerUnixSocketPath() {
return null;
}
Expand Down Expand Up @@ -301,7 +301,7 @@ private static boolean tryOutStrategy(List<String> configurationFailures, Docker
String osType = strategy.getInfo().getOsType();
if (StringUtils.isBlank(osType)) {
log.warn("Could not determine Docker OS type");
} else if (!osType.equals("linux")) {
} else if (!osType.equals("linux") && !osType.equals("windows")) {
log.warn("{} is currently not supported", osType);
throw new InvalidConfigurationException(osType + " containers are currently not supported");
}
Expand Down
16 changes: 11 additions & 5 deletions core/src/main/java/org/testcontainers/utility/RyukContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ class RyukContainer extends GenericContainer<RyukContainer> {

RyukContainer() {
super("testcontainers/ryuk:0.9.0");
DockerClientFactory clientFactory = DockerClientFactory.instance();
withExposedPorts(8080);
withCreateContainerCmdModifier(cmd -> {
cmd.withName("testcontainers-ryuk-" + DockerClientFactory.SESSION_ID);
cmd.withHostConfig(
cmd
.getHostConfig()
.withAutoRemove(true)
.withPrivileged(TestcontainersConfiguration.getInstance().isRyukPrivileged())
.withPrivileged(
TestcontainersConfiguration.getInstance().isRyukPrivileged() &&
!clientFactory.isRunningWindowsContainers()
)
.withBinds(
new Bind(
DockerClientFactory.instance().getRemoteDockerUnixSocketPath(),
new Volume("/var/run/docker.sock")
)
clientFactory.isRunningWindowsContainers()
? new Bind("//./pipe/docker_engine", new Volume("//./pipe/docker_engine"))
: new Bind(
clientFactory.getRemoteDockerUnixSocketPath(),
new Volume("/var/run/docker.sock")
)
)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RyukResourceReaper extends ResourceReaper {
.withConstantThroughput()
.build();

private final AtomicBoolean started = new AtomicBoolean(false);
private static final AtomicBoolean started = new AtomicBoolean(false);

private final RyukContainer ryukContainer = new RyukContainer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Test;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;

Expand Down Expand Up @@ -103,10 +104,31 @@ public static void main(String[] args) {
LABELS_MARKER + new ObjectMapper().writeValueAsString(ResourceReaper.instance().getLabels())
);

GenericContainer<?> container = new GenericContainer<>("testcontainers/helloworld:1.1.0")
GenericContainer<?> container = new GenericContainer<>(
"testcontainers/helloworld:sha-141af7909907e04b124e691d3cd6fc7c32da2207"
)
.withNetwork(org.testcontainers.containers.Network.newNetwork())
.withExposedPorts(8080);

DockerClientFactory clientFactory = DockerClientFactory.instance();
if (clientFactory.isRunningWindowsContainers()) {
// Override the default WaitStrategy (HostPortWaitStrategy),
// which doesn't work on Windows, yet.
// See the InternalCommandPortListeningCheck.
container.setWaitStrategy(
new AbstractWaitStrategy() {
@Override
protected void waitUntilReady() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
);
}

container.start();
}
}
Expand Down