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.