Docker, .net core and environment variables.

Agustinafassina
3 min readOct 11, 2021

--

We are going to work with environment variables in our service. With Docker to run the service and we will use an extension to see it.

Introduction

In this new article, I will explain how to work with environment variables in a service with .net core. We will test it through a docker and simulate a deployment as production. I will show you some docker and linux commands.

Requirements before starting:

· Download: https://dotnet.microsoft.com/download/dotnet/3.1
· Docker: https://www.docker.com/products/docker-desktop
· Repository: https://github.com/agustinafassina/SoccerNetCore
· Optional: https://git-scm.com/downloads
· Docker extension for visual studio code: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker

Let´s go.

We write by git bash console and in the repository:

ls ->(view folders)
cd SoccerNetCore -> (enter the folder)
code . -> (enter visual studio code)

At the root we have a basic DockerFile:
(To dig deeper with Docker, I recommend: https://docs.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows )

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS buildWORKDIR /app# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY .. ./
RUN dotnet publish -c Release -o /release
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build /release ./
ENTRYPOINT ["dotnet", "SoccerNetCore.dll"]

To test the docker and service, in order:
(It is important that the Docker application is running)

docker build -t soccer-net-core .docker run -p 7001:80 --name soccer-net-core soccer-net-core

https://localhost:5001/api/players * GET

In the appsettings.json file we have SoccerKey:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"CrossOrigins": true,
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=3306;user=root;password=;database=soccernetcore"
},
"SoccerTest": "SOCCER-TEST" *Delete value
}

We are going to delete this variable and ConnectionString. Then we’ll add it to our docker. It is recommended not to leave database credentials in the repository.

The service already has the changes to test the docker in:

var soccerKey = Environment.GetEnvironmentVariable(“SoccerKey”);
var fileNameNow = fileName + "-" + soccerKey + now + fileExtension;

Let’s test the docker, the commands in order:

docker build -t soccer-net-core . docker run -p 7001:80 -e "ASPNETCORE_ENVIRONMENT=Development" -e "ConnectionString=localhost;port=3306;user=root;password=123456;database=soccernetcore" -e "SoccerKey=Soccer_test" --name soccer-net-core soccer-net-core

In the second command with the -e we define the environment variables that will be used in the PlayerService.cs we are going to replace the variable that we have in appsettings.json

To see the docker by the extesions of visual studio code:

http://localhost:7001/api/players * GET

To see the docker by console:

docker ps -a

Building a docker and everything we need for an application, it is really very simple, I hope my contribution helps the community, greetings friends!

Un amigo decia, Docker nos vino a solucionar el famoso problema en ‘mi local anda’ ;)…

I´m sure that the code can be improved a lot, I wait for recommendations so, don’t hesitate to recommend me whatever needed for improving.
I hope you liked the article
My linkedin is https://www.linkedin.com/in/agustina-fassina-458247163.

See you on the next article!!

--

--