【发布时间】:2020-06-17 20:05:14
【问题描述】:
我是 docker 新手,我有一个包含一组 Windows 服务 (.NET) 的应用程序。我想将它运行到 docker 容器中。我该怎么办 ?
【问题讨论】:
标签: docker windows-services containers docker-for-windows
我是 docker 新手,我有一个包含一组 Windows 服务 (.NET) 的应用程序。我想将它运行到 docker 容器中。我该怎么办 ?
【问题讨论】:
标签: docker windows-services containers docker-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; \
}
NOTE1:我的 Windows 服务记录到 Windows 事件系统。因此,根据 Docker 约定,此文件最后包含一些不错的代码,用于将 EventLog 信息打印到控制台。您自己的服务可能需要也可能不需要这部分。如果不是,只使用第一行减去'\'。
注意2:Windows 服务的名称可能与其可执行文件名称不同。也就是说,“MyWindowsServiceName.exe”的服务名称可能是“My Windows Service Name”或“Fred”,您需要同时知道这两个名称。
【讨论】:
unattended 标志,它将提示输入登录凭据。
一般来说,你应该选择一个已经安装了必要库的基础镜像,而不是使用一个非常基础的镜像,比如普通的 Linux 或 Windows 并在上面安装。
在你的情况下,选择一个安装了 .NET 的 docker 镜像。This image for instance 理想的流程如下。
docker build -t YourRepoName . 在 Dockerfile 的位置运行此代码docker run YourImage
Dockerfile 这是我为 Springboot 编写的 dockerfile 之一。你可以参考一下。请注意,我只将此处的 jar 文件复制到我的容器上,而不是源代码,因为在构建 docker 容器时,jar 文件是可用的。您可以选择在 Dockerfile 中包含用于复制源代码和创建可执行文件的命令。
【讨论】: