Mustafa
Mustafa

Reputation: 6134

Testcontainers DockerComposeContainer with Docker Compose version v2.0.0

I'm trying to use testcontainers with a DockerComposeContainer. My docker-compose.yaml content is this:

version: '3.8'

services:
  postgresql:
    image: postgres:13
    environment:
      POSTGRES_USER: ordering
      POSTGRES_PASSWORD: ordering
      POSTGRES_DB: ordering

I'm stating the container using the following spring-boot initializer code:

  static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
      DockerComposeContainer<?> environment = new DockerComposeContainer<>(DOCKER_COMPOSE_FILE)
          .withExposedService(DB_SERVICE_NAME, DB_PORT)
          .withLocalCompose(true)
          .start();
}

This used to work until a recent update of docker-desktop on MacOS.

Now it throws the following error:

Caused by: org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container l2whc7cxqkzd_postgresql_1 as it is not running
    at org.testcontainers.containers.GenericContainer.applyConfiguration(GenericContainer.java:812)
    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:363)
    ... 40 more

If I put a break point in the code before it throws the exception and check my running docker containers I see a container with name l2whc7cxqkzd-postgresql-1. The difference is the use of dashes instead of underscores in the name. I guess this a recent change in the docker-compose behaviour in regards to project-identifier separator.

My question is, is there a way to use testcontainers with Docker Compose 2.0.0?

My testcontainers dependency version is 1.16.0 and docker desktop version 4.1.0.

Upvotes: 2

Views: 4842

Answers (1)

Mustafa
Mustafa

Reputation: 6134

One workaround that seems to work is to pass the compatibility option to the docker compose command. Like:

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
      DockerComposeContainer<?> environment = new DockerComposeContainer<>(DOCKER_COMPOSE_FILE)
          .withOptions("--compatibility")
          .withExposedService(DB_SERVICE_NAME, DB_PORT)
          .start();
   }
}

Upvotes: 6

Related Questions