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

4

18

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?

Bridgeport answered 20/8, 2022 at 13:53 Comment(0)
C
35

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.

Couperin answered 20/8, 2022 at 14:33 Comment(0)
L
5

Instead of docker-compose (the standalone tool), you can now use the compose plugin[1]. That works with the "name" parameter.

docker compose up

[1] https://docs.docker.com/compose/#compose-v2-and-the-new-docker-compose-command

Lew answered 24/2 at 21:53 Comment(0)
I
0

I was able to resolve the issue by modifying the service name in the Dockerfile to be in lowercase only. This change resolved the problem and the application is now working correctly.

Illumination answered 30/5, 2023 at 8:52 Comment(0)
F
0

I am on a Debian 12 machine.I received the above message errors. I solved it by installing the correct version of docker as per the official website. Others may not be running a Debian distro, but installing the docke engine via the official instructions will certainly help! Link below.

Install Docker Engine on Debian

Fagin answered 24/7 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.