System.NotSupportedException: Globalization Invariant Mode is not supported. from
SqlConnection.Open() on mcr.microsoft.com/dotnet/runtime:10.0-alpine
means Microsoft.Data.SqlClient refuses to run in the invariant mode that image turns on.
Install icu-libs and set DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false.
After those two lines the same Open() got past the check and failed on the
network instead, which is the expected result here: the connection string points at a
port where no SQL Server listens.
The error
PS> docker run --rm --name fix10-dn-q1 fix10-dn-lab-alpine sql
Unhandled exception. System.NotSupportedException: Globalization Invariant Mode is not supported.
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at Program.<Main>$(String[] args) in /src/Program.cs:line 33
Why it happens
The runtime:10.0-alpine image sets DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true.
The stack trace is four lines long and ends in SqlConnection.TryOpen: there is
no TDS parser, socket or login frame in it, so the exception came before any attempt to
reach the server. That makes it easy to tell apart from a real connection problem, which
has a long trace through the TCP provider (shown below).
The first line also names NotSupportedException, not
SqlException. After the fix the failure was a SqlException with
error 35 from the TCP provider. In this test, then, the image problem gave a
NotSupportedException and the unreachable port gave a
SqlException.
Because nothing reached the network, the test used a fictional connection string to
127.0.0.1,15921 with made-up credentials. No database server was involved.
The fix
...
FROM mcr.microsoft.com/dotnet/runtime:10.0-alpine
RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
WORKDIR /app
COPY --from=build /out .
ENTRYPOINT ["dotnet", "ClinicApi.dll"]
PS> docker run --rm --name fix10-dn-q2 fix10-dn-lab-icu sql
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
---> System.Net.Sockets.SocketException (0x80004005): No error information
at Microsoft.Data.SqlClient.ManagedSni.SniTcpHandle.Connect(String serverName, Int32 port, TimeoutTimer timeout, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo)
...
That SqlException is the proof the fix worked: SqlClient now tried to open a
TCP connection, and nothing answered on that port. Only icu-libs was installed for this test; the culture post linked below
also adds icu-data-full for culture formatting.
If you prefer the Debian-family images, the same project on
mcr.microsoft.com/dotnet/runtime:10.0 was not tried with SqlClient here, so
this post makes no claim about it; check whether your base image sets the invariant
variable by running env inside it, as shown in
the
invariant culture post.
How it was reproduced
A console app referencing Microsoft.Data.SqlClient 6.1.1, built on
mcr.microsoft.com/dotnet/sdk:10.0 (SDK 10.0.302) and run on
mcr.microsoft.com/dotnet/runtime:10.0-alpine (Alpine 3.24.4,
Microsoft.NETCore.App 10.0.15). Its sql mode opens a
SqlConnection with Server=127.0.0.1,15921;Database=ClinicDb;User
Id=clinic_app, a placeholder password, TrustServerCertificate=True and
Connect Timeout=3. The same project and Alpine image were used for the time zone
post of this batch. Docker engine 29.9.3 on Windows 11.
Frequently asked
- Why does SqlConnection.Open throw Globalization Invariant Mode is not supported in Docker?
- The runtime:10.0-alpine image runs .NET in globalization invariant mode, and Microsoft.Data.SqlClient 6.1.1 refused to open a connection in that mode.
- How do I use Microsoft.Data.SqlClient on an Alpine .NET image?
- Add RUN apk add --no-cache icu-libs and ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false to the final stage. In the test Open() then went on to the network.
- Does the invariant mode error mean my SQL Server is unreachable?
- No. In the test it was thrown before any network attempt; the stack trace had no TCP provider frames. Fix the image first, then debug the connection.
The same image setting behind a culture error: Couldn't find a valid ICU package installed on the system. More decoded errors in the Fixes category.