简体   繁体   English

“名称”与任何正则表达式都不匹配:“^x-”尝试在 docker-compose.yml 中设置项目名称时

[英]'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:我将以下行放在 docker-compose.yml 的顶部,以获得由特定 docker-compose 旋转的容器的预设名称:

name: my_project_name

The goal is, for example, to have my database service container named "my_project_name_database".例如,目标是让我的数据库服务容器命名为“my_project_name_database”。

However, after trying to start it with docker-compose up, I get the following error:但是,在尝试使用 docker-compose 启动它后,我收到以下错误:

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.错误:Compose 文件 './docker-compose.yml' 无效,因为:'name' 与任何正则表达式都不匹配:'^x-' 您可能会看到此错误,因为您使用了错误的 Compose 文件版本. Either specify a supported version (eg "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/指定支持的版本(例如“2.2”或“3.3”)并将您的服务定义放在services键下,或者省略version键并将您的服务定义放在文件的根目录以使用版本 1。撰写文件格式版本,请参阅https://docs.docker.com/compose/compose-file/

This keeps happening no matter what version of docker-compose I specify.无论我指定什么版本的 docker-compose,这种情况都会发生。 What am I doing wrong in here?我在这里做错了什么? Is this the right way to set the project name at the docker-compose.yml level?这是在 docker-compose.yml 级别设置项目名称的正确方法吗?

I'd recommend not setting the top-level name: key, but do set the COMPOSE_PROJECT_NAME environment variable if you need it.我建议不要设置顶级name:键,但如果需要,请设置COMPOSE_PROJECT_NAME环境变量。

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. Docker 对 Compose 文件格式做了一些令人困惑的事情。 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".他们引入了一个Compose 规范,该规范与现有工具有一些不兼容,然后将所有合理当前版本的 Compose 工具都很好支持的 现有 版本标记为“旧版”。 The top-level name: key is new in the "specification" version, but not present in the "legacy" versions.顶级name: key 在“规范”版本中是新的,但在“旧”版本中不存在。

Neither Compose file version 2 nor 3 supports a top-level name: key. Compose 文件版本23都不支持顶级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.如果您正在运行独立的docker-compose工具,则在 Compose 文件中没有设置项目名称的选项。 But $COMPOSE_PROJECT_NAME works with all reasonably current versions of Compose.但是$COMPOSE_PROJECT_NAME适用于所有当前合理的 Compose 版本。

If you run docker-compose version and it prints如果您运行docker-compose version并打印

$ 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.那么你就不能使用 Compose 规范,但是你可以使用得到良好支持的“遗留”版本。 But, if you run docker compose version (note, three words, no hyphen) and it prints但是,如果您运行docker compose version (注意,三个单词,没有连字符),它会打印

$ 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.也就是说,如果您使用“规范”扩展,那么您必须使用安装了 Compose 扩展的 Docker 的最新版本,但如果您使用version: '2.4'version: '3.8'那么它将适用于任何或多或少当前版本的 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.我看到的设置$COMPOSE_PROJECT_NAME的常见用例是同时运行同一个 Compose 文件的多个副本。 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);为此,您需要设置环境变量(并且还需要为已发布的ports: ,并避免手动设置container_name:或卷或网络名称); 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.如果它是文件中的一个常量,那么您必须在使用应用程序的一个实例或另一个实例时对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM