Skip to content

Commit b635eb5

Browse files
authored
Fix NPE bug with docker-compose files in working directory (#3866)
1 parent 60865fa commit b635eb5

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private void findImageNamesInDockerfile(Map serviceDefinitionMap) {
120120
final Object contextRelativePath = buildElement.get("context");
121121
if (dockerfileRelativePath instanceof String && contextRelativePath instanceof String) {
122122
dockerfilePath = composeFile
123+
.getAbsoluteFile()
123124
.getParentFile()
124125
.toPath()
125126
.resolve((String) contextRelativePath)
@@ -128,6 +129,7 @@ private void findImageNamesInDockerfile(Map serviceDefinitionMap) {
128129
}
129130
} else if (buildNode instanceof String) {
130131
dockerfilePath = composeFile
132+
.getAbsoluteFile()
131133
.getParentFile()
132134
.toPath()
133135
.resolve((String) buildNode)

core/src/test/java/org/testcontainers/junit/DockerComposeContainerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.testcontainers.containers.DockerComposeContainer;
77

88
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
912
import java.util.Optional;
1013

1114
import static java.lang.String.format;
@@ -53,4 +56,24 @@ public void shouldReturnEmptyResultOnNoneExistingService() {
5356
Optional<ContainerState> result = environment.getContainerByServiceName(notExistingServiceName);
5457
assertFalse(format("No container should be found under service name %s", notExistingServiceName), result.isPresent());
5558
}
59+
60+
@Test
61+
public void shouldCreateContainerWhenFileNotPrefixedWithPath() throws IOException {
62+
String validYaml =
63+
"version: '2.2'\n" +
64+
"services:\n" +
65+
" http:\n" +
66+
" build: .\n" +
67+
" image: python:latest\n" +
68+
" ports:\n" +
69+
" - 8080:8080";
70+
71+
File filePathNotStartWithDotSlash = new File("docker-compose-test.yml");
72+
filePathNotStartWithDotSlash.createNewFile();
73+
filePathNotStartWithDotSlash.deleteOnExit();
74+
Files.write(filePathNotStartWithDotSlash.toPath(), validYaml.getBytes(StandardCharsets.UTF_8));
75+
76+
final DockerComposeContainer<?> dockerComposeContainer = new DockerComposeContainer<>(filePathNotStartWithDotSlash);
77+
assertNotNull("Container could not be created using docker compose file", dockerComposeContainer);
78+
}
5679
}

0 commit comments

Comments
 (0)