Skip to content

Commit d4c784f

Browse files
author
Ranjith Muniyappa
committed
Ignore container_name in docker-compose files instead of throwing exception
Fixes #2472 Previously, Testcontainers would throw an IllegalStateException when parsing a docker-compose file that contained the 'container_name' property. This prevented users from reusing their production docker-compose files without modification. This change modifies the behavior to log a warning instead of throwing an exception, allowing users to use docker-compose files with container_name properties. The container_name is simply ignored since Testcontainers manages container naming internally. Changes: - ParsedDockerComposeFile: Changed validateNoContainerNameSpecified() to log a warning instead of throwing IllegalStateException - ParsedDockerComposeFileValidationTest: Updated tests to verify that container_name is now ignored rather than rejected
1 parent 43c6a97 commit d4c784f

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ private void parseAndValidate() {
131131

132132
private void validateNoContainerNameSpecified(String serviceName, Map<String, ?> serviceDefinitionMap) {
133133
if (serviceDefinitionMap.containsKey("container_name")) {
134-
throw new IllegalStateException(
135-
String.format(
136-
"Compose file %s has 'container_name' property set for service '%s' but this property is not supported by Testcontainers, consider removing it",
137-
composeFileName,
138-
serviceName
139-
)
134+
log.warn(
135+
"Compose file {} has 'container_name' property set for service '{}'. " +
136+
"This property is not supported by Testcontainers and will be ignored.",
137+
composeFileName,
138+
serviceName
140139
);
141140
}
142141
}

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,35 @@ class ParsedDockerComposeFileValidationTest {
2222
public Path temporaryFolder;
2323

2424
@Test
25-
void shouldValidate() {
25+
void shouldIgnoreContainerNameV1() {
2626
File file = new File("src/test/resources/docker-compose-container-name-v1.yml");
27-
assertThatThrownBy(() -> {
28-
new ParsedDockerComposeFile(file);
29-
})
30-
.hasMessageContaining(file.getAbsolutePath())
31-
.hasMessageContaining("'container_name' property set for service 'redis'");
27+
// container_name should be ignored (with a warning log) instead of throwing an exception
28+
assertThatNoException().isThrownBy(() -> new ParsedDockerComposeFile(file));
3229
}
3330

3431
@Test
35-
void shouldRejectContainerNameV1() {
36-
assertThatThrownBy(() -> {
37-
new ParsedDockerComposeFile(ImmutableMap.of("redis", ImmutableMap.of("container_name", "redis")));
38-
})
39-
.hasMessageContaining("'container_name' property set for service 'redis'");
32+
void shouldIgnoreContainerNameInMapV1() {
33+
// container_name should be ignored (with a warning log) instead of throwing an exception
34+
assertThatNoException()
35+
.isThrownBy(() ->
36+
new ParsedDockerComposeFile(ImmutableMap.of("redis", ImmutableMap.of("container_name", "redis")))
37+
);
4038
}
4139

4240
@Test
43-
void shouldRejectContainerNameV2() {
44-
assertThatThrownBy(() -> {
41+
void shouldIgnoreContainerNameV2() {
42+
// container_name should be ignored (with a warning log) instead of throwing an exception
43+
assertThatNoException()
44+
.isThrownBy(() ->
4545
new ParsedDockerComposeFile(
4646
ImmutableMap.of(
4747
"version",
4848
"2",
4949
"services",
5050
ImmutableMap.of("redis", ImmutableMap.of("container_name", "redis"))
5151
)
52-
);
53-
})
54-
.hasMessageContaining("'container_name' property set for service 'redis'");
52+
)
53+
);
5554
}
5655

5756
@Test

0 commit comments

Comments
 (0)