英文:
How Do I Set and Retrieve an Environment Variable with Docker and .NET 7 in VS Code
问题
I created a .NET 7 Web API App and created the Docker configuration files using the command Docker: Add Docker Files to Workspace. When prompted, I also add the Docker compose files. I've added environment variables to my tasks.json
and docker-compose.debug.yml
.
I then start my application using the generated launch configuration Docker .NET Launch.
None of the environment variables are populated when I call Environment.GetEnvironmentVariable("MY_VARIABLE")
in my Program.cs
.
Note that they are populated when I use .NET Core Launch configuration with an env
object.
How do I properly set and retrieve environment variables when using my docker image + container?
Code Examples:
# docker-compose.debug.yml
services:
myservice:
image: myservice
build:
context: .
dockerfile: ./Dockerfile
ports:
- 5000:5000
environment:
- ASPNETCORE_ENVIRONMENT=Development
- FOO=foo
volumes:
- ~/.vsdbg:/remote_debugger:rw
// tasks.json
{
"type": "docker-run",
"label": "docker-run: release",
"dependsOn": [
"docker-build: release"
],
"dockerRun": {
"env": {
"foo": "bar"
}
},
"netCore": {
"appProject": "${workspaceFolder}/Kno2.WebSockets.csproj"
}
}
// Program.cs
var foo = Environment.GetEnvironmentVariable("FOO");
Console.Write(foo); // null
I tried setting environment variables in any file that would take them.
英文:
I created a .NET 7 Web API App and created the Docker configuration files using the command Docker: Add Docker Files to Workspace. When prompted, I also add the Docker compose files. I've added environment variables to my tasks.json
and docker-compose.debug.yml
.
I then start my application using the generated launch configuration Docker .NET Launch.
None of the environment variables are populated when I call Environment.GetEnvironmentVariable("MY_VARIABLE")
in my Program.cs
.
Note that they are populated when I use .NET Core Launch configuration with an env
object.
How do I properly set and retrieve environment variables when using my docker image + container?
Code Examples:
# docker-compose.debug.yml
services:
myservice:
image: myservice
build:
context: .
dockerfile: ./Dockerfile
ports:
- 5000:5000
environment:
- ASPNETCORE_ENVIRONMENT=Development
- FOO=foo
volumes:
- ~/.vsdbg:/remote_debugger:rw
// tasks.json
{
"type": "docker-run",
"label": "docker-run: release",
"dependsOn": [
"docker-build: release"
],
"dockerRun": {
"env": {
"foo": "bar"
}
},
"netCore": {
"appProject": "${workspaceFolder}/Kno2.WebSockets.csproj"
}
}
// Program.cs
var foo = Environment.GetEnvironmentVariable("FOO");
Console.Write(foo); // null
I tried setting environment variables in any file that would take them.
答案1
得分: 1
从Environment.GetEnvironmentVariable的文档中可以看到...
> 在非Windows系统上,环境变量名称是区分大小写的,但在Windows上不区分大小写。
毫无疑问,您的Docker平台正在使用其中一个官方Linux映像,因此需要匹配文本大小写...
"dockerRun": {
"env": {
"FOO": "bar"
}
},
如果您花时间创建了Docker Compose配置,您可以使用Docker Compose任务来代替
{
"version": "2.0.0",
"tasks": [
{
"label": "运行 docker-compose up",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
"services": ["myservice"]
},
"files": [
"${workspaceFolder}/docker-compose.yml",
"${workspaceFolder}/docker-compose.debug.yml"
]
}
}
]
}
英文:
From the documentation for Environment.GetEnvironmentVariable...
> Environment variable names are case-sensitive on non-Windows systems but are not case-sensitive on Windows.
Your Docker platform is no doubt using one of the official Linux images so you need to match the text case...
"dockerRun": {
"env": {
"FOO": "bar"
}
},
If you've taken the time to create a Docker Compose configuration, you could instead use a Docker Compose task
{
"version": "2.0.0",
"tasks": [
{
"label": "Run docker-compose up",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
"services": ["myservice"]
},
"files": [
"${workspaceFolder}/docker-compose.yml",
"${workspaceFolder}/docker-compose.debug.yml"
]
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论