Docker组成变量替换成为一个数字

问题描述 投票:0回答:1

我试图在docker-compose.yml中使用变量替换来设置CPU的数量。

使用此撰写文件:

test.yml

version: "2.2"

services:
  neo4j:
    image: neo4j:3.2
    cpus: ${MAX_CPUS}

由此脚本运行:

test.是

#!/bin/sh
export MAX_CPUS=4

docker-compose -f test.yml up -d

我收到此错误:

$ ./test.sh 
ERROR: The Compose file './test.yml' is invalid because:
services.neo4j.cpus contains an invalid type, it should be a number

正在正确读取变量。如果使用此撰写文件

version: "2.2"

services:
  neo4j:
    image: neo4j:3.2
    #cpus: ${MAX_CPUS}
    volumes:
      - ${MAX_CPUS}:/foo

然后我得到一个使用替换值的错误:

 ERROR: Named volume "4:/foo:rw" is used in service "neo4j" but no declaration was found in the volumes section.

看起来变量替换被解释为字符串,即使该值是数字。

有没有办法强制它到一个数字,假设这是问题?

我使用docker-machine在Mac OS X上运行。

$ docker-compose --version
docker-compose version 1.17.1, build 6d101fb
$ docker-machine --version
docker-machine version 0.13.0, build 9ba6da9
docker docker-compose
1个回答
0
投票

我搜索了Docker Compose GitHub问题并找到了https://github.com/docker/compose/issues/2730。这听起来与你的问题非常相似。它在拉动请求https://github.com/docker/compose/pull/5291中被修复,其中添加了类型转换器用于内插值。它似乎支持每行cpushttps://github.com/docker/compose/pull/5291/files#diff-b2372f2a81cc5911e04cc7df7d68675bR148选项。

但是,如果我们看看必须匹配https://github.com/docker/compose/pull/5291/files#diff-b2372f2a81cc5911e04cc7df7d68675bR139转换的正则表达式,我们可以看到它是^0[0-9]+$。此正则表达式与单个数字不匹配。但它应该匹配01

所以,听起来很傻,你能尝试将数字改为04而不是4吗?

© www.soinside.com 2019 - 2024. All rights reserved.