16

I try these below commands in dockerfile but it didnot run the script.so are there any other commands for running the ps script in dockerfile?

ADD Windowss.ps1 .

CMD powershell .\Windowss.ps1;

5 Answers 5

53

To run a PS1 script file, you can do something like this:

SHELL ["cmd", "/S", "/C"]    
RUN powershell -noexit "& ""C:\Chocolatey\lib\chocolatey.0.10.8\tools\chocolateyInstall.ps1"""

You can also do:

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
WORKDIR C:\
RUN .\install_pfx.ps1
Sign up to request clarification or add additional context in comments.

1 Comment

@shad - please accept this as the answer - it is the answer to the question you raised about how to run a powershell script from your docker file. It might not have solved your problem about running a service in a container, but that was not your question! Thanks!
8

You can use RUN.

You can RUN poweshell commands using

RUN powershell -Command Add-WindowsFeature Web-Server

Refer https://learn.microsoft.com/en-us/dotnet/standard/containerized-lifecycle-architecture/design-develop-containerized-apps/set-up-windows-containers-with-powershell

1 Comment

How can I do this, from docker Run though?
1

Yes, there is another command. ENTRYPOINT ["executable", "param1", "param2"] is a command that, according to the documentation, will make the container to run the executable on its start. It can be used alongside with CMD.

4 Comments

I didn't understand. You want to dockerize Windows?
Try to be clear; your first question is also not too clear. See wikihow.com/Ask-a-Question-on-Stack-Overflow
I just want to ask you that can we create the docker image of c# Window service?
1

I had a similar problem which I solved by using the shell directive.

FROM mcr.microsoft.com/windows/servercore:20H2 AS PS
SHELL ["powershell"]
RUN Write-Host "Hello from docker! Today is $(Get-Date)"

Edit: Just noticed the answer above has a bit better of a command than mine. Upvote it! https://stackoverflow.com/a/48804143/190831

Comments

1

I tried this one and it worked,

FROM mcr.microsoft.com/powershell 
WORKDIR /app
COPY . /app
CMD ["pwsh", "-File", "<your_powershellscript>"]

Here we need to mention the executable as pwsh not powershell, For your reference attaching the Github code link: Here(line: 39) Here (line: 56)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.