Skip to content

Ignore container_name in docker-compose files instead of throwing exception#11376

Closed
ranjitm2001 wants to merge 1 commit intotestcontainers:mainfrom
ranjitm2001:fix/2472-support-container-name-in-docker-compose
Closed

Ignore container_name in docker-compose files instead of throwing exception#11376
ranjitm2001 wants to merge 1 commit intotestcontainers:mainfrom
ranjitm2001:fix/2472-support-container-name-in-docker-compose

Conversation

@ranjitm2001
Copy link
Copy Markdown

Summary

Fixes #2472

Changes

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.

Implementation Details

  • ParsedDockerComposeFile.java: Changed validateNoContainerNameSpecified() to log a warning instead of throwing IllegalStateException
  • ParsedDockerComposeFileValidationTest.java: Updated tests to verify that container_name is now ignored rather than rejected

Testing

All existing tests pass, and the updated tests verify the new behavior:

  • shouldIgnoreContainerNameV1() - Tests file-based compose with container_name
  • shouldIgnoreContainerNameInMapV1() - Tests v1 format map with container_name
  • shouldIgnoreContainerNameV2() - Tests v2 format with container_name

Motivation

As discussed in #2472, many users want to reuse their production docker-compose files for testing. The container_name property is valid Docker Compose syntax and should not cause Testcontainers to fail. Instead, it should be gracefully ignored with a warning.

…eption

Fixes testcontainers#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
@ranjitm2001 ranjitm2001 requested a review from a team as a code owner December 29, 2025 03:03
@meck-gd
Copy link
Copy Markdown
Contributor

meck-gd commented Feb 10, 2026

I tested your branch because I was running into a similar problem. With this change, the container_name is not ignored. Compose still sets it on the containers and the names that testcontainers generates dynamically are ignored instead. This leads to trouble with method calls like waitingFor() because it can't find the referenced service.

What your change does allow is using docker compose override files with container_name: !reset null for every service. The prod file can stay untouched and the additional file makes the stack compatible with testcontainers. The exception would previously not allow this, because the check doesn't account for overrides.

@eddumelendez
Copy link
Copy Markdown
Member

Docker compose support it was supposed to help with transition from compose files to Testcontainers' modules. So, this is an opinionated implementation to support Compose in Testcontainers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support container_name in docker-compose file

3 participants