首页 > 解决方案 > 如何重新启动 Windows 容器

问题描述

我正在尝试从 Windows 容器运行旧版 ASP.Net (.Net 4.7.1) 应用程序。要求之一是将系统文化、区域设置和位置设置为 en-GB。我不允许触摸代码,如果绝对需要,只能触摸 web.config。

考虑以下方法:

  1. 创建包含所有证书的基础映像,并应用文化设置(需要重新启动)
  2. 重新启动基础映像(使用 Windows 重新启动或容器重新启动,无论是否有效)
  3. 运行基础映像以确保正确应用区域性设置
  4. 保存基本图像
  5. 使用之前生成的基础图像创建一个包含我的应用程序的新图像

我的基础镜像的 Dockerfile 是:

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2
ARG site_root=.
WORKDIR /scripts
COPY scripts/ .

RUN powershell -f install-certificates.ps1

RUN powershell C:/Windows/System32/inetsrv/appcmd.exe set config /commit:WEBROOT /section:globalization /culture:en-GB
RUN powershell C:/Windows/System32/inetsrv/appcmd.exe set config /commit:WEBROOT /section:globalization /uiCulture:en-GB

RUN powershell Set-Culture en-GB
RUN powershell Set-WinSystemLocale en-GB
RUN powershell Set-WinHomeLocation -GeoId 242
RUN powershell Set-WinUserLanguageList en-GB -Force

然后构建并运行容器。

docker build -t tmpaspnet .
docker run -it --name tmpcontainer --entrypoint powershell tmpaspnet
# inside the container
Restart-Computer
# container will exit, wait a few seconds
docker start tmpcontainer
docker exec tmpcontainer powershell Get-WinSystemLocale
# verify if system locale is correct set
# commit changes and save them to a new image
docker commit -m 'set system locale to en-GB' tmpcontainer myrepo/aspnet:latest

不幸的是,容器要么忽略 Restart 并没有完全成功。当我Get-WinSystemLocale在容器内运行时,总是返回“en-US”。

TL,DR:重新启动 Windows 容器的正确方法是什么?

我正在使用以下容器 mcr.microsoft.com/dotnet/framework/aspnet:4.7.2

关于设置语言包时失败的附加说明https://github.com/sanguedemonstro/docker-playground/blob/master/langpack-on-servercore2019.md

谢谢

标签: dockerdocker-for-windowswindows-container

解决方案


你真的不应该Restart-Computer在你的容器内使用。

如果您需要重新启动它,您甚至不需要在容器内生成 shell。

首先,您需要找到您的容器 ID。要查找容器,您可以使用主机终端中的docker ps 命令

获得容器 ID 后,只需运行docker restart {containerId} 命令


要改变文化,你可以在你的网络中使用全球化标签,像这样配置。

<system.web>
    <globalization culture="en-GB" uiCulture="en-GB" />
</system.web>

推荐阅读