Docker-compose错误与服务env_file

时间:2017-06-09 18:58:39

标签: docker environment-variables docker-compose dockerfile

我一直在努力通过.env文件将敏感数据(凭据)传递到docker容器。我正在使用docker-compose,我的docker-compose.yml文件如下所示:

 services:
   some-service:
     env_file:
       - .env

但是当我尝试“docker-compose up”时,我收到以下错误:

 ERROR: In file './docker-compose.yml', service 'env_file' must be a mapping not an array.

我的.env文件包含

等数据

id = 1234567890

pwd = my_pwd

我的文件中是否有缩进错误?有关如何解决此错误的任何建议吗?

3 个答案:

答案 0 :(得分:0)

看起来这是您的YAML结构的问题。在curl -X POST http://localhost:8000/products/ -H 'ContentType: application/json' -d '{"pictures": [{"description": "first one"}, {"description": "second one"}], "product_name": "foobar"}'或缺少的服务名称之前,它可能是缺少的空间。试试这个:

- .env

如果您只使用一个环境文件,您也可以写:

services:
  some-service:
    env_file:
      - .env

请参阅:https://docs.docker.com/compose/compose-file/#env_file

答案 1 :(得分:0)

ERROR: In file './docker-compose.yml', service 'env_file' must be a mapping not an array.

错误说docker-compose认为env_file是服务的名称,而不是服务的设置。这表示您没有正确缩进服务中的env_file部分。 YML语法对空白区域敏感,因此请仔细检查线条上的缩进。如果您在缩进时找不到错误,请在您的问题中包含完整的docker-compose.yml文件,该文件与文件中的文件完全一致,以便我们进一步协助。

答案 2 :(得分:0)

问题不在于缩进。好像它现在正在运作。我实际上不得不提到版本和构建路径(它们在我的初始yml文件中丢失了)。所以最终的docker-compose文件如下所示:

version: '3'
services:
  some-service:
    build: .
    env_file:
      - .env

或者如果您愿意,可以将其与特定图像名称关联为:

version: '3'
services:
  some-service:
    build: .
    image: image_name:image_version
    env_file:
      - .env

希望这可以帮助遇到同样问题的人

相关问题