docker-compose: Not accepting extensions starting with "x-" (Invalid top-level property "x-...")

Viewed 1211

I'm trying to deploy a service which uses docker-compose files and I've been seeing the following error:

Invalid top-level property "x-...". Valid top-level sections for this Compose file are: secrets, version, volumes, services, configs, networks, and extensions starting with "x-". You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions ...

I reproduced the error using this simple docker-compose.yml file:

version: "3.5"     
                   
x-secrets: &secrets
  secrets:         
    - foo          
                   
services:          
  a:               
    <<: *secrets   
    image: a-image 
                   
secrets:           
  foo:             
    external: true 

The command docker-compose -f docker-compose.yml up

Gives the following output:

ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid top-level property "x-secrets". Valid top-level sections for this Compose file are: secrets, version, volumes, services, configs, networks, and extensions starting with "x-".

You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

Versions:

docker --version
Docker version 19.03.6, build 369ce74a3c
docker-compose --version
docker-compose version 1.17.1, build unknown

Running on Ubuntu 18.04.

Thanks for any input!

2 Answers

Resolved this by upgrading docker-compose to

docker-compose version 1.27.4, build 40524192

This means that either the format of your docker-compose.yml file is not correct or your version of docker-compose is too old.

It's always a good practice to upgrade to the latest version of docker-compose - you can do it like this:

  1. Get the latest version

    VERSION=$(curl https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)
    
  2. Install it

    sudo curl -L "https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    
  3. Give system permissions

    sudo chmod +x /usr/local/bin/docker-compose
    
  4. Logout, then login again

Now that you have the latest version, check if the format of your docker-compose.yml file is correct by comparing it to the examples in the official documentation (again, check the format for the latest version). Note that small things like single- vs double-space and different special characters matter a lot!

Related