[英]How can I use an env file to pass environment variables into a standalone vscode remote container?
I am using a standalone Go
vscode remote container for development and would like to load environment variables into the container from a file.我正在使用独立的Go
vscode 远程容器进行开发,并希望将环境变量从文件加载到容器中。
All examples I can find are using Docker Compose and its env_file
option but using Docker Compose seems overkill for a single container.我能找到的所有示例都是使用 Docker Compose 及其env_file
选项,但使用 Docker Compose 对于单个容器来说似乎有点过分了。 Is there any way I can achieve this without using Docker Compose?有什么方法可以在不使用 Docker Compose 的情况下实现这一目标?
In the .devcontainer
directory of your project add a file that declares your environment variables, in this case .env
:在项目的.devcontainer
目录中添加一个声明环境变量的文件,在本例中.env
:
D:. │ .gitignore │ README.md │ ├───.devcontainer │ .env │ devcontainer.json │ Dockerfile │ └───.vscode settings.json
.env
: .env
:
MY_URL=https://my.com/
MY_SECRET=unicorns
Then in your devcontainer.json
you can define runArgs
that pass the .env
file as an env-file
argument to the Docker CLI run
command.然后在您的devcontainer.json
中,您可以定义runArgs
将.env
文件作为env-file
参数传递给 Docker CLI run
命令。 This uses the ${localWorkspaceFolder}
variable that is expanded to the containing directory of the local source code:这使用了${localWorkspaceFolder}
变量,该变量扩展为本地源代码的包含目录:
devcontainer.json
: devcontainer.json
:
{ "name": "Go", "dockerFile": "Dockerfile", "runArgs": [ "--env-file", "${localWorkspaceFolder}/.devcontainer/.env" ], ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.