Docker Community Forums

Share and learn in the Docker community.

How do I run provisioning script at container start


(Artisticcheese) #1

Hello,

I want to run some powershell script every time my container starts which modifies password for local admin account and modifies some IIS settings unique to container host.
I use “microsoft/iis” image which already specifies “CMD” parameter and it’s needed for image to run. So how do I run my custom script during container startup and pass some ENV parameters to it to modify container runtime.

G


(Michael Friis) #2

You should probably override the CMD of the iis base image. You can either string together commands with ; or include a .ps1 script that does both what you need and then does what the iis image already does.


(Artisticcheese) #3

How do I pass arguments to my custom command in such cases? I want to pass some environment variables from container host to container image during startup


(Michael Friis) #4

docker run -e FOO=bar user/yourimage


(Artisticcheese) #5

So how my DOCKER will will look like if I go with multiple command in CMD statement. Default IIS images has “c:\servicemonitor w3svc” command.
My docker file is below

RUN powershell.exe -executionpolicy bypass c:\windows\temp\script.ps1
CMD [powershell.exe, -executionpolicy, bypass, c:\windows\temp\startup.ps1;C:\ServiceMonitor.exe, w3svc]

(Michael Friis) #6

This works:

FROM microsoft/windowsservercore

ADD hello.ps1 .
ADD world.ps1 .

CMD powershell .\hello.ps1 ; .\world.ps1

Here’s an example: https://gist.github.com/friism/f063ecb1c8ba7259a426a53547fe5e18


How to string together multiple powershell commands in RUN?
(Artisticcheese) #7

I’m trying to add those to to container but they seemed not be added after “EntryPoint” command for container image. Microsoft/IIS specifies “c:\servicemonitor.exe w3svc” as EntryPoint. So when I add “CMD” to docker file it seems to be appended directly after entry point and hence never exececuted. Do I need to modify EntryPoint instead?

My dockerfile

CMD powershell.exe .\startup.ps1;C:\ServiceMonitor.exe w3svc

Running container

PS C:\Users\admin> (Get-Container -Name iis).Command
C:\ServiceMonitor.exe w3svc cmd /S /C 'powershell.exe .\startup.ps1;C:\ServiceMonitor.exe w3svc'

(Mansing2) #8

How do I pass Parameters with values to .PS1 file in Dockerfile?


(Dazinator) #9

I found one way to achieve this is to override the entry point and run some script at container start the approach I took was:

  1. Add a start.bat file to your image.
  2. Override the EntryPoint and run start.bat:

    ENTRYPOINT [“CMD /C start.bat”]

  3. In the start.bat file, carry out your additional logic, and then make the same call as the original EntryPoint in the base image:

    echo “starting up”
    start C:/registry.exe serve /config/config.yml
    echo “finished starting up”