Skip to content

Commit f52169f

Browse files
authored
Parse docker compose file correctly when version is not declared (#9420)
`version` is optional in compose file. Fixes #8109
1 parent cd29df9 commit f52169f

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ protected Object constructObject(Node node) {
7878

7979
private void parseAndValidate() {
8080
final Map<String, ?> servicesMap;
81-
if (composeFileContent.containsKey("version")) {
82-
if ("2.0".equals(composeFileContent.get("version"))) {
83-
log.warn(
84-
"Testcontainers may not be able to clean up networks spawned using Docker Compose v2.0 files. " +
85-
"Please see https://github.com/testcontainers/moby-ryuk/issues/2, and specify 'version: \"2.1\"' or " +
86-
"higher in {}",
87-
composeFileName
88-
);
89-
}
81+
if (composeFileContent.containsKey("version") && "2.0".equals(composeFileContent.get("version"))) {
82+
log.warn(
83+
"Testcontainers may not be able to clean up networks spawned using Docker Compose v2.0 files. " +
84+
"Please see https://github.com/testcontainers/moby-ryuk/issues/2, and specify 'version: \"2.1\"' or " +
85+
"higher in {}",
86+
composeFileName
87+
);
88+
}
9089

90+
if (composeFileContent.containsKey("services")) {
9191
final Object servicesElement = composeFileContent.get("services");
9292
if (servicesElement == null) {
9393
log.debug(

core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ public void shouldObtainImageNamesV2() {
111111
);
112112
}
113113

114+
@Test
115+
public void shouldObtainImageNamesV2WithNoVersionTag() {
116+
File file = new File("src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml");
117+
ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(file);
118+
assertThat(parsedFile.getServiceNameToImageNames())
119+
.as("all defined images are found")
120+
.contains(
121+
entry("mysql", Sets.newHashSet("mysql")),
122+
entry("redis", Sets.newHashSet("redis")),
123+
entry("custom", Sets.newHashSet("postgres"))
124+
);
125+
}
126+
114127
@Test
115128
public void shouldObtainImageFromDockerfileBuild() {
116129
File file = new File("src/test/resources/docker-compose-imagename-parsing-dockerfile.yml");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
redis:
3+
image: redis
4+
mysql:
5+
image: mysql
6+
custom:
7+
build: .
8+
networks:
9+
custom_network: {}

0 commit comments

Comments
 (0)