maxfridbe
maxfridbe

Reputation: 5970

Windows Server 2016 Dockerfile install service

I am attempting to install a service in a docker container on windows server2016.

Simply placing the service there and Powershelling:

New-Service -Name Bob -StartupType Automatic -BinaryPathName .\SVCHost.exe

Adds the service however in the container I get the result:

PS C:\Program Files\COMPANY\Repository> start-service -Name bob
start-service : Service 'bob (Bob)' cannot be started due to the following error: Cannot start service Bob on computer '.'.
At line:1 char:1
+ start-service -Name bob
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException

I have attempted creating a user and setting the startup user credentials but same issue.

Looking at https://github.com/Microsoft/sql-server-samples/blob/master/samples/manage/windows-containers/mssql-server-2016-express-windows/dockerfile shows that they use sqlexpress to do the install of the service.

Long story short... How do I register a service in a Windows server 2016 Docker container

Upvotes: 1

Views: 876

Answers (2)

Elton Stoneman
Elton Stoneman

Reputation: 19154

Also, look at the Dockerfile for microsoft/iis. The real work in the container is done in the IIS Windows Service, but the entrypoint is a binary called ServiceMonitor.exe. The monitor checks the Windows Service, if the Service fails the exe fails, so Docker knows the container is unhealthy.

Upvotes: 1

maxfridbe
maxfridbe

Reputation: 5970

Fully qualifying the install name works. thanks @Elton Stoneman

or figured out this works too in my program

        public static bool Install(string serviceName, string serviceDescription, string logonUsername, string logonPassword, string exeFile)
        {
            string managementPath = @"\\.\ROOT\CIMV2:Win32_Service";

            ManagementClass mc = new ManagementClass(managementPath);
            ManagementBaseObject inParams = mc.GetMethodParameters("create");
            inParams["Name"] = serviceName;
            inParams["DisplayName"] = serviceDescription;
            inParams["PathName"] = exeFile + " -name " + "\"" + serviceName + "\"";
            inParams["ServiceType"] = ServiceType.Win32OwnProcess;
            inParams["ErrorControl"] = 0;
            inParams["StartMode"] = ServiceStartMode.Automatic;
            inParams["DesktopInteract"] = false;
            inParams["StartName"] = logonUsername;
            inParams["StartPassword"] = logonPassword;
            inParams["LoadOrderGroup"] = null;
            inParams["LoadOrderGroupDependencies"] = null;
            inParams["ServiceDependencies"] = null;

            ManagementBaseObject outParams = mc.InvokeMethod("create", inParams, null);
            string status = outParams["ReturnValue"].ToString();
            return (status == "0" || status == "23");
        }

Upvotes: 0

Related Questions