Home > Blockchain >  Docker Compose: depends_on with condition -> invalid type, should be an array
Docker Compose: depends_on with condition -> invalid type, should be an array

Time:02-10

I have the following compose file:

version: "3"

services:

  zookeeper:
    image: docker-dev.art.intern/wurstmeister/zookeeper:latest
    ports:
      - 2181:2181

  kafka:
    image: docker-dev.art.intern/wurstmeister/kafka:latest
    ports:
      - 9092:9092
    environment:
      - KAFKA_LISTENERS=PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092
      - KAFKA_ADVERTISED_HOST_NAME=kafka
      - KAFKA_ADVERTISED_PORT=9092
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
    depends_on:
      - zookeeper

  app:
    build:
      context: ./
      dockerfile: app/Dockerfile
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4020/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 5
    depends_on:
      - kafka
      - zookeeper

  app-test:
    build:
      context: ./
      dockerfile: test/Dockerfile
    depends_on:
      app:
        condition: service_healthy

As you can see im implementing a healthcheck for the app and I use service_healthy condition. But that leads to the error:

The Compose file '.\docker-compose.yml' is invalid because:
services.app-test.depends_on contains an invalid type, it should be an array

Is there a way to fix that issue?

If I change to array sanytax:

...

  app-test:
    build:
      context: ./
      dockerfile: test/Dockerfile
    depends_on:
      - app:
          condition: service_healthy

The error changes to:

The Compose file '.\docker-compose.yml' is invalid because:
services.app-test.depends_on contains an invalid type, it should be a string

CodePudding user response:

You're missing a - to make depends_on an array:

  app-test:
    build:
      context: ./
      dockerfile: test/Dockerfile
    depends_on:
      - app:
          condition: service_healthy

Notice the - before app. Without it, services.app-test.depends_on will be an object, not an array. Also pay attention to the amount of spaces: if condition is in the same column as app, you will also get an undesired result.

When decoding your YAML, this is what you get (in JSON):

{
  "app-test": {
    "build": {
      "context": "./",
      "dockerfile": "test/Dockerfile"
    },
    "depends_on": {
      "app": {
        "condition": "service_healthy"
      }
    }
  }
}

With the added -:

{
  "app-test": {
    "build": {
      "context": "./",
      "dockerfile": "test/Dockerfile"
    },
    "depends_on": [
      {
        "app": {
          "condition": "service_healthy"
        }
      }
    ]
  }
}

CodePudding user response:

you can do that with a compose file version 2.1, but it was removed in compose file version 3.

There are several things to be aware of when using depends_on:

  • depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

yet, will advise you not to downgrade your compose file. but rather handle it appropriately with a wrapper script.

you might find wait-for-it, dockerize, and/or wait-for handy.

  • Related