dockerdocker-compose

'name' does not match any of the regexes: '^x-' while trying to set the project name in a docker-compose.yml


I put the following line at the top of my docker-compose.yml, to get a pre-set names for my containers spun by that particular docker-compose:

name: my_project_name

The goal is, for example, to have my database service container named "my_project_name_database".

However, after trying to start it with docker-compose up, I get the following error:

ERROR: The Compose file './docker-compose.yml' is invalid because: 'name' does not match any of the regexes: '^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/

This keeps happening no matter what version of docker-compose I specify. What am I doing wrong in here? Is this the right way to set the project name at the docker-compose.yml level?


Solution

  • I'd recommend not setting the top-level name: key, but do set the COMPOSE_PROJECT_NAME environment variable if you need it.

    export COMPOSE_PROJECT_NAME=my_project_name
    docker-compose up -d
    

    The project name also defaults to the base name of the current directory, so renaming it is potentially an option.

    cd ..
    mv project my_project_name
    cd my_project_name
    docker-compose up -d
    

    Docker has done some confusing things with the Compose file format. They've introduced a Compose specification which has some incompatibilities with the existing tooling, and then labeled the existing versions that are well-supported by all reasonably-current versions of the Compose tool as "legacy". The top-level name: key is new in the "specification" version, but not present in the "legacy" versions.

    Neither Compose file version 2 nor 3 supports a top-level name: key. If you're running the standalone docker-compose tool, there just isn't an option to set the project name in the Compose file. But $COMPOSE_PROJECT_NAME works with all reasonably current versions of Compose.

    If you run docker-compose version and it prints

    $ docker-compose version
    docker-compose version 1.29.2, build 5becea4c
    docker-py version: 5.0.0
    CPython version: 3.9.0
    OpenSSL version: OpenSSL 1.1.1h  22 Sep 2020
    

    then you can't use the Compose specification, but you can use the well-supported "legacy" versions. But, if you run docker compose version (note, three words, no hyphen) and it prints

    $ docker compose version
    Docker Compose version v2.6.0
    

    then you can use either the "legacy" versions or the "specification" version.

    That is, if you use the "specification" extensions, then you must be using the most-recent version of Docker with the Compose extension installed, but if you use version: '2.4' or version: '3.8' then it will work with any more-or-less-current version of Compose.


    The usual use case I see for setting $COMPOSE_PROJECT_NAME is to run multiple copies of the same Compose file at the same time. For this you'll need to set the environment variable (and also to use environment variables for published ports:, and avoid manually setting container_name: or volume or network names); if it was a constant in the file then you'd have to edit it when working with one instance or the other of the application.