Skip to content

Commit

Permalink
Parse docker compose file correctly when version is not declared (#9420)
Browse files Browse the repository at this point in the history
`version` is optional in compose file.

Fixes #8109
  • Loading branch information
eddumelendez authored Oct 21, 2024
1 parent cd29df9 commit f52169f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ protected Object constructObject(Node node) {

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

if (composeFileContent.containsKey("services")) {
final Object servicesElement = composeFileContent.get("services");
if (servicesElement == null) {
log.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public void shouldObtainImageNamesV2() {
);
}

@Test
public void shouldObtainImageNamesV2WithNoVersionTag() {
File file = new File("src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml");
ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(file);
assertThat(parsedFile.getServiceNameToImageNames())
.as("all defined images are found")
.contains(
entry("mysql", Sets.newHashSet("mysql")),
entry("redis", Sets.newHashSet("redis")),
entry("custom", Sets.newHashSet("postgres"))
);
}

@Test
public void shouldObtainImageFromDockerfileBuild() {
File file = new File("src/test/resources/docker-compose-imagename-parsing-dockerfile.yml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
redis:
image: redis
mysql:
image: mysql
custom:
build: .
networks:
custom_network: {}

0 comments on commit f52169f

Please sign in to comment.