How to read environment variables passed through the docker CLI using ASP.NET Core 2.x?
Hey guys,
I am writing a small API in ASP.NET Core 2.1 that sends an email when it receives a request. In order to determine the right message and email address to send to, I am using environment variables to dynamically have the correct credentials and message. Before setting up Docker for the API, everything works as I added the environment variables on my dev machine. However, when I dockerized my app, those environment variables are not exposed to the container; thus, I realized that I need to pass environment variables into the container. I decided to use a command like the following:
docker run -d -p 8080:80 --name myapi-4 myapi --env [EMAIL_USERNAME=](mailto:EMAIL_USERNAME=antrix.edge@gmail.com)'username' --env EMAIL_PASSWORD='password' --env ASPNETCORE_ENVIRONMENT='Production'
However, when trying to read the environment variables in my code like the following, it returns null.
var launchProfile = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Does anyone have experience of using ASP.NET Core 2.x and Docker to pass and read environment variables? I've looked at a bunch of tutorials but haven't found a working solution yet. Any help is appreciated!
Thank you in advance!
0 comments:
Post a Comment