Home > database >  Docker Container stops after running powershell script
Docker Container stops after running powershell script

Time:06-24

I am trying to run a Web application in a container. I needed to run a powershell script when container is instantiated. That script will modify a config file with the environment variable value. For that I added an "ENTRYPOINT" in my docker file.

Docker File

FROM mcr.microsoft.com/windows/servercore/iis:latest
   
# Copy powershell scripts
COPY scripts/ scripts/

#update values    
ENTRYPOINT ["powershell", "C:\\scripts\\updateconfig.ps1"]

When I instantiate a container instance using "docker run" command. It does create a container and executes that powershell script but then it stops the container. I understand that it stopped that container as it completed the execution.

powershell script (updateconfig.ps1)

((Get-Content -Path ".\scripts\appsettings.json" -Raw) -replace '{{SQLSERVER}}', [System.Environment]::GetEnvironmentVariable('SQL_SERVER')) |
 Set-Content -Path ".\scripts\appsettings.json"

After going through so many stackoverflow threads, I tried few things but none of them gave me desired results. I have tried CMD, instead of EntryPoint but that ends up in an error

> CMD powershell "C:\\scripts\\printvar.ps1"

Error:

 Service 'w3svc' has been stopped

APPCMD failed with error code 4312

Failed to update IIS configuration

Following does the same, executes the script and stops the container

> ENTRYPOINT ["powershell", "-NoExit", "-File", "/scripts/printvar.ps1"]

How can I run the container instance continuously ?

CodePudding user response:

"Service Monitor" is a default entry point in IIS image. As I am using that as a base image, I was actually overwriting the default entry point with a powershell script. I added this to my powershell script and now container is running continuously

Invoke-Expression "& `"C:\\ServiceMonitor.exe`" w3svc"
  • Related