如何在运行在Visual Studio Code docker容器中的.devcontainer使用的环境文件中引用另一个环境变量? [英] How to reference another environment variable inside an env file used by .devcontainer running inside a Visual Studio Code docker container?

查看:133
本文介绍了如何在运行在Visual Studio Code docker容器中的.devcontainer使用的环境文件中引用另一个环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio Code运行docker容器并将环境变量作为文件传递.

I am using Visual Studio Code to run a docker container and passing my environment variables as a file.

我正在尝试从其他环境变量动态设置字符串格式,并且无法解析该字符串.

I am trying to format a string dynamically from other environmental variables and having trouble resolving the string.

我能够构建容器,并且调试终端选项卡显示没有问题.

I am able to build the container and debug terminal tab shows no problems.

我目前不使用 docker-compose.yml ,而是使用Visual Studio .devcontainer 设置.

I am currently not using docker-compose.yml, but rather the Visual Studio .devcontainer settings.

devcontainer.json

...

"runArgs": [
        "--env-file", "${localWorkspaceFolder}/.env.dev"
, 

...

.env.dev

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=${DATABASE_DRIVER}://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DATABASE}

输出

当我在bash中键入 env 时,它会显示:

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=${DATABASE_DRIVER}://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DATABASE}

我希望它显示在哪里:

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=postgresql://user:password@$localhost:5432/database

问题

$ {VARIABLE_NAME} 是不正确的语法吗?

推荐答案

从文件中设置环境变量与在终端中进行设置不同.

Setting env variables from files works differently than setting them in a terminal.

每行只是一个在"="上分割的字符串.将该拆分的右侧分配为值,左侧分配为键.

Each line is just a string that gets split on "=" with the right hand side of that split being assigned as the value, and the left hand side being assigned as the key.

如果您查看运行命令的源代码:

If you look at the source code for the run command:

https://github.com/docker/cli/blob/master/cli/command/container/run.go

您将看到未通过bash命令设置环境变量.这只是一系列的字符串分配.

you'll see that environment variables aren't set set through bash commands. It's just a series of string assignments.

env文件几乎总是这种情况,它们是字符串,并且是这样设置的.您尝试使用的语法仅在试图将环境文件作为一系列bash命令运行的平台上有效,但docker不会这样做.这与VS代码无关,这只是env文件通常的工作方式.

This is pretty much always the case with env files.They're strings, and are set as such. The syntax you're trying to use only works on platforms that make an effort to run env files as a series of bash commands, but docker doesn't do that. This has nothing do with with VS code, it's just how env files ususally work.

一种可能的解决方法是将这种分配从您的env文件移至Dockerfile或docker-compose.这些指令是作为bash命令运行的,因此您的变量调用将起作用.

One potential workaround would be to move this assignment from your env file to your Dockerfile or docker-compose. Those instructions are run as bash commands, so your variable call will work.

Dockerfile

ENV FIRST=ONE
ENV SECOND=${FIRST}

在这里,在您的容器中回显$ {SECOND}将打印出一个.

Here, inside your container echo ${SECOND} will print out ONE.

docker-compose

Docker撰写环境变量的工作方式略有不同.它不会按顺序设置撰写文件中的变量,因此引用在同一服务定义中设置的变量不起作用.但是,如果将变量设置为与撰写文件位于同一目录的.env文件中,则可以通过以下操作访问这些值:

Docker compose environment variables work a bit differently. It doesn't set the variables in your compose file sequentially, so referencing a variable that you set in the same service definition doesn't work. However, If your variables are set in a .env file in the same directory as your compose file, then you can access those values by doing:

services:
  myapp:
    environment:
      - VAR_IN_MY_CONTAINER=${VAR_ON_MY_HOST}

之所以会发生这种现象,是因为compose会将其目录中的任何.env文件都视为主机环境的一部分.

This behavior happens because compose considers any .env file in its directory to be part of the host environment.

这篇关于如何在运行在Visual Studio Code docker容器中的.devcontainer使用的环境文件中引用另一个环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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