Skip to content

Inconsistent docker-compose path resolution in multi-module project #45169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
devmizz opened this issue Apr 11, 2025 · 2 comments
Closed

Inconsistent docker-compose path resolution in multi-module project #45169

devmizz opened this issue Apr 11, 2025 · 2 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@devmizz
Copy link

devmizz commented Apr 11, 2025

Hello,
I’m currently using the docker-compose dependency in a multi-module project.

In one of the submodules, I configured the application.yaml to reference the path to the Docker Compose file, and it works correctly when running the main application. However, I encountered an issue when writing tests that also rely on the same docker-compose setup: an exception is thrown indicating that the Compose file cannot be found at the specified path.

For example, assuming the root project is named root, the submodule is app, and the Docker Compose file is located at root/app/docker/docker-compose.yaml

root/app/docker/docker-compose.yaml

services:
    mysql:
        container_name: practice-mysql
        image: mysql:8.0
        environment:
            MYSQL_DATABASE: practice
            MYSQL_USER: user
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password

root/app/src/main/resources/application.yaml

spring:
    docker:
        compose:
            enabled: true
            service: spring-practice
            stop:
                command: down
            skip:
                in-tests: false
            file: app/docker/docker-compose.yaml
  • When running the main application, FS.userDir is set to root
  • But when running tests, FS.userDir is set to root/app

Because of this difference, the path resolution behaves inconsistently between main and test executions. As a workaround, I ended up duplicating the application.yaml under the test resources and customizing the path manually for tests.

root/app/test/resources/application.yaml

spring:
    docker:
        compose:
            # ...
            file: docker/docker-compose.yaml

Even though the Docker Compose file is located in the same path, the behavior varies depending on whether the main application or tests are being run. This inconsistency can lead to confusion and misconfiguration.

Would it be possible to improve this so that the Compose file path resolution is consistent within the same module, regardless of whether it’s running as part of the main application or during tests?

Here is my sub-module link

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 11, 2025
@nosan
Copy link
Contributor

nosan commented Apr 13, 2025

Hello @devmizz

Unfortunately, you haven't provided details about exactly how you're running your application, but I'll assume you're using IntelliJ IDEA. When you run AppApplication via IntelliJ IDEA, the working directory automatically defaults to the project's root directory, which in your case is spring-boot-practice. Therefore, the file app/docker/docker-compose.yaml is correctly found, allowing all required Docker containers to start properly.

However, when executing tests, IntelliJ IDEA uses the module directory as the working directory ($MODULE_WORKING_DIR$). In your specific scenario, this corresponds to spring-boot-practice/app. Consequently, the path app/docker/docker-compose.yaml becomes incorrect, resulting in the error: Docker Compose file 'app/docker/docker-compose.yaml' does not exist.

I've tried running your application directly via Gradle:

cd app
gradle bootRun

However, it fails due to the same issue:

/Users/dmytronosan/IdeaProjects/spring-boot-practice/app % gradle :bootRun


> Task :bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.4)

2025-04-13T13:21:36.516+03:00  INFO 73706 --- [           main] co.app.AppApplicationKt                  : Starting AppApplicationKt using Java 21.0.4 with PID 73706 (/Users/dmytronosan/IdeaProjects/spring-boot-practice/app/build/classes/kotlin/main started by dmytronosan in /Users/dmytronosan/IdeaProjects/spring-boot-practice/app)
2025-04-13T13:21:36.517+03:00  INFO 73706 --- [           main] co.app.AppApplicationKt                  : No active profile set, falling back to 1 default profile: "default"
2025-04-13T13:21:36.591+03:00 ERROR 73706 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalArgumentException: Docker Compose file 'app/docker/docker-compose.yaml' does not exist

If you remove the application.yaml file from your test resources and update your application.yaml and docker-compose.yaml files accordingly:

application.yaml:

spring:
    docker:
        compose:
            enabled: true
            service: spring-practice
            lifecycle-management: start_only
            stop:
                command: down
            skip:
                in-tests: false
            file: docker/docker-compose.yaml

docker-compose.yaml:

services:
    mysql:
        container_name: practice-mysql
        image: mysql:8.0
        ports:
            - '3308:3306'
        environment:
            MYSQL_DATABASE: practice
            MYSQL_USER: user
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password
        restart: always
        command:
            - --character-set-server=utf8mb4
            - --collation-server=utf8mb4_unicode_ci
            - --skip-character-set-client-handshake

    redis:
        image: redis:alpine
        ports:
            - "6380:6379"

Then both Gradle tasks, bootRun and build will work correctly.

You can also specify the working directory when running your Spring Boot application:

Image

@devmizz
Copy link
Author

devmizz commented Apr 14, 2025

@nosan Thank you very much for your detailed response. It seems the issue was caused by how IntelliJ IDEA sets the working directory at runtime. I’ll make sure to configure the working directory properly when setting up the Docker environment in a multi-module project.

Thanks again for the thorough explanation — I’ll close the issue.

@devmizz devmizz closed this as completed Apr 14, 2025
@mhalbritter mhalbritter added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 14, 2025
@mhalbritter mhalbritter closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants