Skip to content

Fix host directory mounts on Windows #240

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -12,6 +12,9 @@
import javax.annotation.Nonnull;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand Down Expand Up @@ -39,8 +42,38 @@ public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNu
if (workdir != null) {
argb.add("-w", workdir);
}
Set<String> drives = new HashSet<>();
for (Map.Entry<String, String> volume : volumes.entrySet()) {
argb.add("-v", volume.getKey() + ":" + volume.getValue());
String driveName = null;

try {
Path hostPath = Paths.get(volume.getKey());
Path rootPath = hostPath.getRoot();
// If we have a valid root we can check if we need to do our special root handling
if (rootPath != null) {
driveName = rootPath.toString();
if (driveName.endsWith("\\")) {
driveName = driveName.substring(0, driveName.length() - 1);
}
}
} catch (InvalidPathException e) {
// We got a value that is not a valid path. Keeping driveName at null allows us to gracefully handle this
// and skip the special drive path handling for those cases
}
if (driveName == null || driveName.equals("C:")) {
// C: path can be mapped directly
argb.add("-v", volume.getKey() + ":" + volume.getValue());
}
else
{
// Non C: drive paths in the container can not be mapped due to Windows limitations. It is only possible
// to map an entire drive so we collect the used drives and map the entire drive
drives.add(driveName);
}
}
for (String drive : drives) {
// Windows requires that the host part is a directory but the container path must be an entire drive
argb.add("-v", String.format("%s\\:%s", drive, drive));
}
for (String containerId : volumesFromContainers) {
argb.add("--volumes-from", containerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class WindowsDockerClientTest {

Expand All @@ -35,7 +37,7 @@ public void test_run() throws IOException, InterruptedException {
"learn/tutorial",
null,
null,
Collections.emptyMap(),
Collections.singletonMap("D:\\Jenkins\\workspace", "D:\\Jenkins\\workspace"),
Collections.emptyList(),
new EnvVars(),
dockerClient.whoAmI(),
Expand All @@ -47,7 +49,7 @@ public void test_run() throws IOException, InterruptedException {
Assert.assertTrue(containerRecord.getContainerName().length() > 0);
Assert.assertTrue(containerRecord.getHost().length() > 0);
Assert.assertTrue(containerRecord.getCreated() > 1000000000000L);
Assert.assertEquals(Collections.<String>emptyList(), dockerClient.getVolumes(launchEnv, containerId));
Assert.assertEquals(Collections.singletonList("d:"), dockerClient.getVolumes(launchEnv, containerId));

// Also test that the stop works and cleans up after itself
Assert.assertNotNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
Expand Down