首页 > 解决方案 > 对 Windows 服务进行 Docker 化

问题描述

我是 docker 新手,我有一个包含一组 Windows 服务 (.NET) 的应用程序。我想将它运行到 docker 容器中。我应该怎么办 ?

标签: dockerwindows-servicescontainersdocker-for-windows

解决方案


我已使用以下 Dockerfile 成功地将 Windows 服务放入 docker 容器中。替换MyWindowsServiceName为您自己的 Windows 服务的名称。

# escape=\

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

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

COPY ["MyWindowsServiceName/bin/Release/", "/Service/"]

WORKDIR "C:/Service/"

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

ENTRYPOINT ["powershell"]
CMD Start-Service \""MyWindowsServiceName\""; \
    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; \
    }

注意1:我的 Windows 服务记录到 Windows 事件系统。因此,根据 Docker 约定,此文件最后包含一些不错的代码,用于将 EventLog 信息打印到控制台。您自己的服务可能需要也可能不需要此部件。如果不是,请仅使用第一行减去“\”。

注意2:Windows 服务的名称可能与其可执行文件名称不同。也就是说,“MyWindowsServiceName.exe”的服务名称可能是“我的 Windows 服务名称”或“Fred”,您需要同时知道这两个名称。


推荐阅读