aspnet-core | docker | sql-server

Docker - SQL Error on ASP.NET Core Alpine

Having trouble to connect to a remote SQL server from an ASP.NET Core app running on top of Alpine dotnet on a linux container?

Abhith Rajan
Abhith RajanMay 07, 2019 · 2 min read · Last Updated:

My ASP.NET Core app was running fine on mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim on my linux container. And the build image size was 195MB. In order to reduce the file size, I switched to mcr.microsoft.com/dotnet/core/sdk:2.2.203-alpine3.9 and the new image size was 98.6MB. WOW !!!

But then there came this problem,

System.InvalidOperationException: Internal connection fatal error

The SQL client was having trouble in this build. After the regular research, find out that in the alpine image

The microsoft/dotnet Alpine images are purposefully configured to use the .NET Core 2.0 Globalization Invariant Mode. This is done to reduce the size of the image substantially. It seems as though the SqlClient does not support running with the Globalization Invariant Mode.

If you are thinking what is .NET Core 2.0 Globalization Invariant Mode,

.NET Core 2.0 Globalization Invariant Mode enables you to remove application dependencies on globalization data and globalization behavior. This mode is an opt-in feature that provides more flexibility if you care more about reducing dependencies and the size of distribution than globalization functionality or globalization-correctness.

But there is a small catch,

The drawback of running in the invariant mode is applications will get poor globalization support.

So solution for this was to add the following in Dockerfile just before the ENTRYPOINT,

1RUN apk add --no-cache icu-libs
2ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

In the first step, we are adding the icu-libs Alpine linux package, which is the international components for unicode library (libraries) and in the next step, we are setting DOTNET_SYSTEM_GLOBALIZATION_INVARIANT to false to enable the globalization support.

Additional Resources

Abhith Rajan

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than nine years of experience and has a proven successful track record of delivering technology-based products and services.
Connect

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related SnippetsView All

Related ArticlesView All

Related VideosView All

SQL Server 2017 Graph Database

A Journey into .NET Microservices with Steeltoe

.NET Microservices with Steeltoe

Related StoriesView All

Related Tools & ServicesView All

sqlfum.pt

sequel fumpt

sqlfmt is an online SQL formatter. It is pronounced sequel fumpt. Its purpose is to beautifully format SQL statements.
flurl.dev

Flurl

Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library for .NET.