.NET Docker项目西里尔字符显示为�的问题求助
.NET 8 Docker容器中西里尔字符显示乱码问题
问题现象
- 本地Docker Desktop中西里尔字符显示正常,部署到Debian服务器后所有西里尔字符显示为
� - 进入容器执行
echo "Приветик!"可正常显示字符,但.NET接口返回内容乱码 - 执行控制台编码检测代码时,俄语提示文字乱码,但编码名称显示为
Unicode (UTF-8)和utf-8
测试代码
接口测试代码
app.MapGet("/", () => "Приветик!");
控制台编码检测代码
Console.WriteLine($"Кодировка: {Console.OutputEncoding.EncodingName}"); Console.WriteLine($"Кодировка: {Console.OutputEncoding.WebName}");
现有配置文件
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 8080 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["PatiVerCore.WebApi/PatiVerCore.WebApi.csproj", "PatiVerCore.WebApi/"] COPY ["PatiVerCore.Application/PatiVerCore.Application.csproj", "PatiVerCore.Application/"] COPY ["PatiVerCore.Domain/PatiVerCore.Domain.csproj", "PatiVerCore.Domain/"] COPY ["PatiVerCore.Infrastructure/PatiVerCore.Infrastructure.csproj", "PatiVerCore.Infrastructure/"] COPY ["PatiVerCore.Persistence/PatiVerCore.Persistence.csproj", "PatiVerCore.Persistence/"] RUN dotnet restore "./PatiVerCore.WebApi/./PatiVerCore.WebApi.csproj" COPY . . WORKDIR "/src/PatiVerCore.WebApi" RUN dotnet build "./PatiVerCore.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./PatiVerCore.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "PatiVerCore.WebApi.dll"]
docker-compose.yml
services: pativercore.webapi: image: ${DOCKER_REGISTRY-}pativercorewebapi container_name: pativer-core build: context: . dockerfile: PatiVerCore.WebApi/Dockerfile restart: always ports: - "5001:8080" depends_on: - redis_cache redis_cache: image: redis:7.4.0-alpine3.20 container_name: redis-cache restart: always ports: - "6379:6379" command: redis-server
已尝试的Locale配置
RUN apt-get update && apt-get install -y locales # Locale RUN sed -i -e \ 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen \ && locale-gen ENV LANG ru_RU.UTF-8 ENV LANGUAGE ru_RU:ru ENV LC_LANG ru_RU.UTF-8 ENV LC_ALL ru_RU.UTF-8 # +Timezone ENV TZ Europe/Moscow RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
解决方案
1. 强制.NET响应头使用UTF-8编码
在Program.cs中添加配置,确保接口响应明确指定UTF-8编码:
var builder = WebApplication.CreateBuilder(args); // 配置JSON序列化使用UTF-8 builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; }); var app = builder.Build(); // 全局设置响应Content-Type编码 app.Use(async (context, next) => { if (!context.Response.ContentType?.Contains("charset") ?? true) { context.Response.ContentType = $"{context.Response.ContentType ?? "text/plain"}; charset=utf-8"; } await next(); }); app.MapGet("/", () => "Приветик!"); app.Run();
2. 修正Dockerfile的Locale配置位置
将Locale配置移至base阶段,确保所有后续构建阶段都继承该配置:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 8080 # 提前在base阶段安装并配置Locale RUN apt-get update && apt-get install -y locales RUN sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && locale-gen ENV LANG ru_RU.UTF-8 ENV LANGUAGE ru_RU:ru ENV LC_ALL ru_RU.UTF-8 ENV TZ Europe/Moscow RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["PatiVerCore.WebApi/PatiVerCore.WebApi.csproj", "PatiVerCore.WebApi/"] COPY ["PatiVerCore.Application/PatiVerCore.Application.csproj", "PatiVerCore.Application/"] COPY ["PatiVerCore.Domain/PatiVerCore.Domain.csproj", "PatiVerCore.Domain/"] COPY ["PatiVerCore.Infrastructure/PatiVerCore.Infrastructure.csproj", "PatiVerCore.Infrastructure/"] COPY ["PatiVerCore.Persistence/PatiVerCore.Persistence.csproj", "PatiVerCore.Persistence/"] RUN dotnet restore "./PatiVerCore.WebApi/./PatiVerCore.WebApi.csproj" COPY . . WORKDIR "/src/PatiVerCore.WebApi" RUN dotnet build "./PatiVerCore.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./PatiVerCore.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "PatiVerCore.WebApi.dll"]
3. 配置Debian服务器系统Locale
确保宿主机本身支持UTF-8和俄语Locale:
sudo apt-get install locales sudo sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen sudo locale-gen sudo update-locale LANG=ru_RU.UTF-8 LC_ALL=ru_RU.UTF-8
4. 显式设置.NET应用编码
在Program.cs开头添加编码强制配置:
// 强制控制台和应用使用UTF-8编码 System.Console.OutputEncoding = System.Text.Encoding.UTF8; System.Console.InputEncoding = System.Text.Encoding.UTF8; System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
内容的提问来源于stack exchange,提问作者Harmont
相关产品推荐
相关产品推荐

