Skip to content

Commit

Permalink
Fix host directory mounts on Windows
Browse files Browse the repository at this point in the history
On Windows, it is not possible to mount host directories to a subfolder
of a non-C: drive. Due to this restriction, having a workspace on
another drive will make the docker run execution fail since the plugin
tries to map the workspace into the container.

This fixes that by collecting all the non-C: drive volume mappings and
handling those explicitly by mapping the entire drive into the
container.
  • Loading branch information
maass-tv committed Jun 9, 2021
1 parent b174d46 commit 4e271bf
Showing 1 changed file with 34 additions and 1 deletion.
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

0 comments on commit 4e271bf

Please sign in to comment.