首页 > 解决方案 > Docker 容器中的 Windows 服务

问题描述

我正在研究可能的应用程序 dockerisation。该应用程序包括多个 Windows 服务 (.NET WCF)。我还没有尝试为 Windows 服务创建一个 dockerfile。但是,如果有人可以为我提供一些指示是否有效,我们将不胜感激。

标签: dockerwindows-servicesdocker-for-windows

解决方案


在您的情况下,我可能会为每个 Windows 服务创建一个图像。

以下 Dockerfile 非常适合我将 Windows 服务构建到 docker 映像中。

您的所有服务文件都需要位于 docker 上下文的“Installs”文件夹中,以及 InstallUtils.exe 文件的副本(来自 .NET / Visual Studio)。

# escape=\

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-1709

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

COPY ["Installs/", "/Service/"]

WORKDIR "C:/Service/"

RUN "C:/Service/InstallUtil.exe" /LogToConsole=true /ShowCallStack SmartFormsToWorkInjuryReportingService.exe; \
    Set-Service -Name "\"My Windows Service Name\"" -StartupType Automatic; \
    Set-ItemProperty "\"Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\My Windows Service Name\"" -Name AllowRemoteConnection -Value 1

ENTRYPOINT ["powershell"]
CMD Start-Service \""My Windows Service Name\""; \
    Get-EventLog -LogName System -After (Get-Date).AddHours(-1) | Format-List ;\
    $idx = (get-eventlog -LogName System -Newest 1).Index; \
    while ($true) \
    {; \
      start-sleep -Seconds 1; \
      $idx2  = (Get-EventLog -LogName System -newest 1).index; \
      get-eventlog -logname system -newest ($idx2 - $idx) |  sort index | Format-List; \
      $idx = $idx2; \
    }

仅供参考,然后您可以通过以下方式运行该服务:

docker run --rm --net=MyNet --platform=windows -p 80:80 --name MyWindowsServiceContainer mywindowsserviceimage

推荐阅读