windowsdockerwindows-container

Encode in windows container?


I have an EXE program for file format conversion. The input file name sometimes contains Chinese or other characters, but I encountered some environment encoding issues during the docker deployment process. The program can run normally on Windows 10/11, where the language environment is zh-CN and the code page displayed by the chcp command is 936.

The docker environment uses the image mcr.microsoft.com/windows/servercore:ltsc2022, and the host environment is Windows 11 with the parameter --isolation=process. In the container, using Get-WinSystemLocale shows zh-CN, which is the same as the host. However, the chcp command still shows 437, so I think it’s a code page issue. I’m not sure if this is correct.

I tried to run it on a newly installed Windows 10 with English language, but it failed. After setting Control Panel -> Region -> Administrative Language for non-Unicode programs to Chinese Simplified, it works.

Update: During my test on @GChuf 2 solutions, i found this may not be chcp problem. After change chcp to 936 sucessfully in docker, if myprogram.exe take Chinese characters as input, it can't output right content.

So, I simplified the problem a bit and wrote a helloworld test program. The program receives parameters that may contain Chinese characters and then prints out the parameters.

#include <iostream>

int main(int argc, char* argv[]) {
    std::cout << argv[1] << std::endl;
    return 0;
}

In Windows host, chcp:936, Get-WinSystemLocale:zh-CN

.\helloworld.exe hello // hello
.\helloworld.exe 你好  // 你好

In docker, chcp:936, Get-WinSystemLocale:zh-CN

.\helloworld.exe hello // hello
.\helloworld.exe 你好  // ??

Solution

  • I found the following two helpful reference links. According to what was said above, using the following command on my_program.exe worked.

    1. touch my_program.1.mainfest file
    <?xml version="1.0" encoding="utf-8"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
        ...
        <asmv3:application>
            <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">
                <activeCodePage>Legacy</activeCodePage>
            </asmv3:windowsSettings>
        </asmv3:application>
        ...
    </assembly>
    
    1. In powershell, exec this command
    mt.exe -manifest my_program.1.manifest -outputresource:.\my_program.exe;#1
    
    1. Copy my_program.exe to docker container, and it work well.

    Reference:

    1. Encoding inside container
    2. The activeCodePage manifest element can be used for more than just setting UTF-8 as the active code page
    3. code-page-identifiers
    4. Installing Language Packs for Windows in containers
    5. Publish Server Core image with da-DK culture