Detecting if dotnet core app is running inside docker container

In old days (not so old, albeit like a one or two year(s) ago), we used to insert a docker environment variable like IS_DOCKER_CONTAINER using dockerfile in docker images. This was used to determine if an application is running inside a docker container or not. This helped to set certain attributes of the application like logging level and methods, modify certain environmental settings etc and helped controlling behavior of the application. It can also be useful in other situations like to determine whether you want to run selenium tests or not (as selenium web drivers would not be available inside docker container) to run as part of the integration testing. 

This is a method we still follow today. Few days back, when we were reviewing the environmental variables inside new dotnet core images based on microsoft/dotnet:2.1-aspnetcore-runtime, we observed one of the built-in settings was as below:

DOTNET_RUNNING_IN_CONTAINER=true

We later checked the dockerfiles and it seemed to be introduced in one of the alpine versions:

https://github.com/dotnet/dotnet-docker/blob/master/2.1/runtime-deps/alpine3.7/amd64/Dockerfile

Since aspnetcore-runtime is based on alpine images only, it is then carried forward. In a nutshell, this setting can be useful to determine if your .NET core app is running inside container and taking certain decisions. Also, you do not need to insert it using dockerfile anymore. 🙂

Also do note that they also introduce some other useful variables like:

ENV ASPNETCORE_VERSION 2.1.3
ENV ASPNETCORE_URLS=http://+:80

which can be useful too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s