December 13, 2024

Unset Docker inherited variable

Today I was rubbing my head over a situation where an environment variable was defined in a base image. More exactly, the image microsoft/dotnet:2.1-runtime now sets the ASPNETCORE_URLS variable and the images based on that inherit it. For my case, this was problematic because my application uses this to determine whether to run over HTTPS and the port of the application.

If we inspect the microsoft/dotnet:2.1-runtime image, we can see that the environment looks like this:

docker inspect microsoft/dotnet:2.1-runtime
..
"Env": [    
   "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
   "ASPNETCORE_URLS=http://+:80",
   "DOTNET_RUNNING_IN_CONTAINER=true",
   "DOTNET_VERSION=2.1.2"
],
..

Having defined the URLS like that can be cool enough but it wasn’t my case, as it made my app crash without knowing why. It took me a while to discover that the URLS is set by the image, unlike previous versions of the dotnet runtime.

Of course I could replace the variable at build time, thus making the error disappear. But I was quite a slug and tried to do it the hard way: by getting rid of this variable. I didn’t want to change to much in my code so I started  searching over the internet. I discovered that there’s no way to unset this variable in inherited images. Tried the following:

RUN unset ASPNETCORE_URLS
CMD unset ASPNETCORE_URLS

But to no luck. None of the instructions above worked so what I did was to replace the value with an empty one:

ENV ASPNETCORE_URLS=

Conclusion

In conclusion, until Docker will support something similar, we’ll need to adapt our code to take into consideration scenarios like this. I saw there is a proposal here, but didn’t reach production unfortunately.

Cheers folks! 😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →