Skip to content

Commit f033c13

Browse files
authored
Always use Path for MountableFile (#3514)
Also, be conscious on Windows when adding host bind for Docker socket. Make sure ContainerisedDockerCompose consciously uses Unix path when necessary. Fixes #3493
1 parent c3a8ca7 commit f033c13

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.testcontainers.utility.DockerLoggerFactory;
3434
import org.testcontainers.utility.LogUtils;
3535
import org.testcontainers.utility.MountableFile;
36+
import org.testcontainers.utility.PathUtils;
3637
import org.testcontainers.utility.ResourceReaper;
3738
import org.zeroturnaround.exec.InvalidExitValueException;
3839
import org.zeroturnaround.exec.ProcessExecutor;
@@ -618,12 +619,13 @@ public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
618619
// Map the docker compose file into the container
619620
final File dockerComposeBaseFile = composeFiles.get(0);
620621
final String pwd = dockerComposeBaseFile.getAbsoluteFile().getParentFile().getAbsolutePath();
621-
final String containerPwd = MountableFile.forHostPath(pwd).getFilesystemPath();
622+
final String containerPwd = convertToUnixFilesystemPath(pwd);
622623

623624
final List<String> absoluteDockerComposeFiles = composeFiles.stream()
624625
.map(File::getAbsolutePath)
625626
.map(MountableFile::forHostPath)
626627
.map(MountableFile::getFilesystemPath)
628+
.map(this::convertToUnixFilesystemPath)
627629
.collect(toList());
628630
final String composeFileEnvVariableValue = Joiner.on(UNIX_PATH_SEPERATOR).join(absoluteDockerComposeFiles); // we always need the UNIX path separator
629631
logger().debug("Set env COMPOSE_FILE={}", composeFileEnvVariableValue);
@@ -669,6 +671,12 @@ public void invoke() {
669671
StringUtils.join(this.getCommandParts(), ' '));
670672
}
671673
}
674+
675+
private String convertToUnixFilesystemPath(String path) {
676+
return SystemUtils.IS_OS_WINDOWS
677+
? PathUtils.createMinGWPath(path).substring(1)
678+
: path;
679+
}
672680
}
673681

674682
/**

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import lombok.SneakyThrows;
6464
import org.apache.commons.io.FileUtils;
6565
import org.apache.commons.lang.StringUtils;
66+
import org.apache.commons.lang.SystemUtils;
6667
import org.jetbrains.annotations.NotNull;
6768
import org.jetbrains.annotations.Nullable;
6869
import org.junit.runner.Description;
@@ -946,9 +947,14 @@ public void addEnv(String key, String value) {
946947
*/
947948
@Override
948949
public void addFileSystemBind(final String hostPath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) {
950+
if (SystemUtils.IS_OS_WINDOWS && hostPath.startsWith("/")) {
951+
// e.g. Docker socket mount
952+
binds.add(new Bind(hostPath, new Volume(containerPath), mode.accessMode, selinuxContext.selContext));
949953

950-
final MountableFile mountableFile = MountableFile.forHostPath(hostPath);
951-
binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode, selinuxContext.selContext));
954+
} else {
955+
final MountableFile mountableFile = MountableFile.forHostPath(hostPath);
956+
binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode, selinuxContext.selContext));
957+
}
952958
}
953959

954960
/**

core/src/main/java/org/testcontainers/utility/MountableFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static MountableFile forClasspathResource(@NotNull final String resourceN
105105
* @return a {@link MountableFile} that may be used to obtain a mountable path
106106
*/
107107
public static MountableFile forHostPath(@NotNull final String path, Integer mode) {
108-
return new MountableFile(new File(path).toURI().toString(), mode);
108+
return forHostPath(Paths.get(path), mode);
109109
}
110110

111111
/**

0 commit comments

Comments
 (0)