Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service in Docker container

I am looking at possible dockerisation of an application. The application includes multiple Windows Services (.NET WCF). I am yet to try out creating a dockerfile for the windows services. But shall appreciate if someone may provide me with some pointer whether this works well.

like image 647
Kangkan Avatar asked Nov 29 '18 09:11

Kangkan


1 Answers

In your situation, I'd probably create one image for each Windows Service.

The following Dockerfile works well for me in building a Windows Service into a docker image.

All your service files need to be in the 'Installs' folder of the docker context, plus a copy of the InstallUtils.exe file (from .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; \
    }

FYI, you can then run the service by:

docker run --rm --net=MyNet --platform=windows -p 80:80 --name MyWindowsServiceContainer mywindowsserviceimage
like image 101
QA Collective Avatar answered Oct 16 '22 23:10

QA Collective