简体   繁体   English

如何使用 env 文件将环境变量传递到独立的 vscode 远程容器中?

[英]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.

相关问题 如何使用 Dockerfile 在容器中传递环境变量,但使用单独的环境变量文件(如 .env) - How pass environment variables in the container using Dockerfile but with a separate file for environment variables like .env 如何将Jenkins环境变量传递给Docker容器? - How can I pass Jenkins environment variables to docker container? 如何将环境变量传递给Terraform的Docker容器? - How can I pass environment variables to a Docker container with Terraform? 如何在 nginx 容器中使用带有 lua 的环境变量 - How can I use environment variables with lua in nginx container 如何通过env文件传递docker中的环境变量? - How to pass through environment variables in docker run through an env file? 如何使用 kubernetes env 访问容器环境变量? - How to access container environment variables with kubernetes env? 如何使用.env文件中的环境变量来填写同一个.env文件中的其他环境变量 - How to Use Environment Variables in a .env file to fill out other environment variable in the same .env file 如何在docker-compose.yml的.env文件中使用环境变量? - How do I use environment variables in .env file in docker-compose.yml? 重新创建容器后如何让 PostgresQL 使用 .env 文件中的环境变量(不删除卷) - How to make PostgresQL use environment variables from .env file after re-creating the container (without removing the volume) 我可以使用 Cockpit 将环境变量传递给 Docker 容器吗? - Can I pass environment variables to a Docker container using Cockpit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM