0

我通过 dotnet core 2.1 创建了一个 web api。

我在 launchsettings.json 文件中使用了 ASPNETCORE_ENVIRONMENT 变量。我的 launchsettings.json 文件如下。我放了 3 个变量 Development、Staging 和 Production,因为我想在运行时更改环境变量。

 "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:15705",
      "sslPort": 44367
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Development": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Production": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },    
    "Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },    
    "seed": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

我可以使用 dotnet run --launch-profile Staging 更改本地计算机中的 ASPNETCORE_ENVIRONMENT。

现在的问题是我正在使用 Docker 容器来运行这些 dotnet 核心应用程序,并且我想在 docker 容器启动/运行时更改环境变量。

我的 DockerFile 如下:

FROM microsoft/dotnet:latest
COPY . .
WORKDIR /src/testService

RUN ["dotnet", "restore"]

RUN dotnet build -c Release -o /app --no-restore

#RUN export ASPNETCORE_ENVIRONMENT=Staging
ENV ASPNETCORE_URLS //*:5000
EXPOSE 5000/tcp

ENTRYPOINT ["dotnet", "run"]

我尝试使用 ENTRYPOINT ["dotnet", "run", "--launch-profile=Staging"] 但它不起作用。

我正在使用 Azure Devops CI-CD Pipeline 构建 docker 映像并在 kubernetes 上运行该映像。

我想实现一次构建,到处部署的策略。所以我应该能够在多个环境中部署图像。

请帮助我使用 kubernetes 更改 ASPNETCORE_ENVIRONMENT 变量运行时。我也在 kubernetes yaml 文件中使用了命令和参数,但它没有用。

4

0 回答 0