在docker-compose.yml文件中设置环境变量与在.env文件中设置该变量相同吗? [英] Setting an environmental variable in a docker-compose.yml file is the same as setting that variable in a .env file?

查看:859
本文介绍了在docker-compose.yml文件中设置环境变量与在.env文件中设置该变量相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 docker-compose.yml 文件中设置特定的环境变量。我不认为我的 docker-compose up 正在正确注册/读取它。

  apigateway.web:
图像:traefik
命令:--api --docker#启用Web UI并告诉Traefik收听docker
环境:
- COMPOSE_CONVERT_WINDOWS_PATHS = 1
端口:
- 80#HTTP端口
- 8080#Web UI(由--api启用)
卷:
-/var/run/docker.sock:/var/run/docker.sock#这样Traefik可以收听Docker事件

我知道我可以在 .env 文件中进行设置。这基本上不是一回事吗?当然, .env 文件就像为 all 自动添加 environment:部分一样我的 docker-compose.yml 文件中的服务... vs ...仅为该服务设置了该变量。但是除此之外,是同一回事,对吗?我问这是因为,当我将单个环境变量添加到该服务中时,我不认为它会被挑选/读取'',因为此错误仍然出现:

 正在重新创建3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ... 
正在重新创建3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ...错误
错误:3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1无法为服务apigateway.web创建容器:b'Mount拒绝:\n源路径 \var\run\ \\\\docker.sock:/var/run/docker.sock \nis不是有效的Windows路径'
错误:apigateway.web不能为服务apigateway.web创建容器:b'挂载被拒绝:\n源路径 \var\run\docker.sock:/var/run/docker.sock \ nis并非有效的Windows路径'
在启动项目时遇到错误。.

这个错误似乎是在GitHub问题中讨论的 ->答案是:添加环境变量 COMPOSE_CONVERT_WINDOWS_PATHS = 1



注意:




  • 我在Windows机器上。

  • Docker作为Linux容器运行。



所以-我是对的还是我做的事情是完全错误的,实际上我确实需要创建一个 .env 文件。



预先感谢!

解决方案

我相信您想在主机上设置COMPOSE_CONVERT_WINDOWS_PATHS = 1,以便docker-compose知道将Windows样式转换为Unix样式的卷定义。



$ Env:COMPOSE_CONVERT_WINDOWS_PATHS = 1
docker-compose up


I'm trying to set a specific environmental variable in my docker-compose.yml file. I don't think my docker-compose up is registering/reading it correctly.

  apigateway.web:
    image: traefik
    command: --api --docker # Enables the web UI and tells Traefik to listen to docker
    environment:
      - COMPOSE_CONVERT_WINDOWS_PATHS=1
    ports:
      - "80"   # The HTTP port
      - "8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events

I know I could set it in a .env file. Isn't this basically the same thing? Of course, the .env file is like auto adding an environment: section to all of my services in my docker-compose.yml file ... vs ... just setting that variable for 1 service. But besides that, it's the same thing, right? an environmental variable is an environmental variable.

I'm asking this because when I add that single environmental variable to that service I don't believe it's getting 'picked up / read' because this error still occurs:

Recreating 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ... 
Recreating 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ... error
ERROR: for 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1  Cannot create container for service apigateway.web: b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis not a valid Windows path'
ERROR: for apigateway.web  Cannot create container for service apigateway.web: b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis not a valid Windows path'
Encountered errors while bringing up the project..

And this error seems to be talked about in this GitHub issue -> with the answer being: add an environmental variable COMPOSE_CONVERT_WINDOWS_PATHS=1.

Notes:

  • I'm on a windows machine.
  • Docker is running as Linux containers.

So -- am I right or what I'm doing is totally wrong and I actually do need to create a .env file because of .

thanks in advance!

解决方案

I believe you want to set COMPOSE_CONVERT_WINDOWS_PATHS=1 on your host machine so docker-compose knows to convert the windows-style to unix-style volume definitions.

$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "1" docker-compose up

这篇关于在docker-compose.yml文件中设置环境变量与在.env文件中设置该变量相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆