'Visual Studio and ASP.NET Core 6 -> pass environment variables inside *.env from host environment to Dockerfile

I do not want to commit my mysecretstuff.env file to git containing my secrets.

mysecretstuff.env:

ONEKEY=MY_SECRET
ANOTHERKEY=MY_SECRET

This file needed to be added to .csproj file to work:

<DockerfileRunEnvironmentFiles>mysecretstuff.env</DockerfileRunEnvironmentFiles>

And my Dockerfile exposes the ENV like this:

ENV ONEKEY=$ONEKEY
ENV ANOTHERKEY=$ANOTHERKEY

I found a solution using docker compose, but I want to be able to continue using Dockerfile if possible ...

It would be nice to be able to do something like:

    ONEKEY=$PASSMYENVIRONMENTFROMHOST

I thought this would be easy but I could not find a simple solution to this when searching around..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source