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
3
Answers
You’re missing a
-
to makedepends_on
an array:Notice the
-
beforeapp
. Without it,services.app-test.depends_on
will be an object, not an array. Also pay attention to the amount of spaces: ifcondition
is in the same column asapp
, you will also get an undesired result.When decoding your YAML, this is what you get (in JSON):
With the added
-
:you can do that with a compose file version 2.1, but it was removed in compose file version 3.
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.
in compose version 3 you can use depends long syntax to specify a condition
e.g.
This appears to have been removed in version 3 of the docker compose specification, but then re-introduced in version 3.9.
See https://github.com/compose-spec/compose-spec/blob/master/spec.md#long-syntax-1
Note that this seems to require Compose V2, which is executed as
docker compose
on the latest docker binary.See https://docs.docker.com/compose/#compose-v2-and-the-new-docker-compose-command