skip to Main Content

There is the next section in launchsettings.json of the ASP.NET Core Web API project:

"Docker": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
  "environmentVariables": {
    "ASPNETCORE_URLS": "https://+:443;http://+:80",
    "MYSECRETS__MYDBPASSWORD": "MyPassValue"
  },
  "publishAllPorts": true,
  "useSSL": true
}

I run the app in a docker container and my application works fine taking a password from MYSECRETS__MYDBPASSWORD env variable. But when I try to printenv (as root user) inside the container, I can’t see my env var. I also tried to find it in Docker Desktop -> Inspect but without success.

In Visual Studio 2022 -> Output -> Container Tools I also can’t see that the variable is passed to the container with -e argument.

If I change the password to something wrong in launchsettings.json and re-run the container, application fails because of the wrong authentication. So, the env variable is definitely inside the container but I can’t see it.

Why I can’t see it and how it’s passed inside the container?

2

Answers


  1. Chosen as BEST ANSWER

    It turned out that Visual Studio sets environment variables per process when run Web API project in Docker. Env vars can be seen the next way:

    # Find your dotnet process ID
    ps faux
    # Show env vars for your process ID (PID)
    cat /proc/[PID]/environ | tr '' 'n'
    

    But when I run the application via docker-compose project with setting env vars in docker-compose.override.yml file, they can be seen with simple printenv command.


  2. You mentioned you are using Visual Studio, so you can use docker-compose.override.yml to Override Visual Studio’s Docker Compose configuration. This official article just support Visual Studio in Windows.
    enter image description here

    Sample: docker-compose.override.yml

    version: '3.4'
    
    services:
      your-service-name:
        environment:
          - ASPNETCORE_URLS=https://+:443;http://+:80
          - MYSECRETS__MYDBPASSWORD=MyPassValue
    

    And if you deploy it to docker, you could use below command.

    docker run -e ASPNETCORE_URLS="https://+:443;http://+:80" -e MYSECRETS__MYDBPASSWORD=MyPassValue your-image-name
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search