8

I need to run a [Windows] Docker container as an executable, running a rather involved PowerShell script (invoking Java and .NET applications) and exiting. Docker documentation suggests using ENTRYPOINT for this purpose. So I went ahead and created a Dockerfile with the following contents:

FROM microsoft/dotnet-framework
COPY run.ps1 /
ENTRYPOINT [ "powershell.exe", "C:\\run.ps1" ]

The contents of run.ps1 (uber-simplified for this question):

gci
write-host "looks like everything is good!"

Then, I ran the following commands:

# Build the Docker image
docker build --rm -t testdockerps .

# Create/run container using above image
docker run -it testdockerps

The container ran successfully, displaying the contents of C:\ followed by the message - looks like everything is good!.

I have a couple of questions based on what my observations:

  1. What is the default shell for a Windows based Docker container? Is there any way to set it to PowerShell, so I don't have to specify "powershell" as the first element of the ENTRYPOINT JSON array? Should I be using the SHELL command in the Dockerfile?
  2. Creating and running the container takes about 3-4 seconds which is somewhat understandable, but after the PS1 script completes, it takes nearly a questionable 10 seconds for the container to exit and return to the command prompt. What may be the cause of this delay?
Web User
  • 7,438
  • 14
  • 64
  • 92

1 Answers1

15

Yes you can specify powershell as default shell like below on top of DOCKERFILE

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]

I'm not sure you can do anything about the time it takes to spin down your VM

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Gregory Suvalian
  • 3,566
  • 7
  • 37
  • 66
  • 1
    Adding `SHELL` did not take any effect, i.e. I still needed to specify `"powershell"` as the first argument to `ENTRYPOINT`. Otherwise, the Docker container fails. It looks like `SHELL` is only useful for `RUN` commands defined in the Dockerfile, but it does not actually set PowerShell as the default shell in the container. I learnt something too, that I am using the _exec_ form of `ENTRYPOINT`, so there is apparently no shell. But still, how does the container know which type of runtime environment to pick up if specify `Write-Host "hello world"` versus `echo hello world`? – Web User Apr 17 '18 at 17:18
  • 2
    Below works for me ```FROM microsoft/windowsservercore:1709 SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"] ENTRYPOINT Write-output "hello" ``` – Gregory Suvalian Apr 17 '18 at 21:24
  • Thanks for that clarification. I wanted to set an environment variable in the container and override the entry point to print all the environment variables in the container environment. I used this command but am having trouble with it: `docker run --entrypoint "powershell" --env HOSTNAME=$Env:COMPUTERNAME "gci Env:*"`. I get an error with _unexpected token_ and _missing expression_. But the same command works perfectly fine without the `--env` parameter. Why is that? – Web User Apr 18 '18 at 00:07
  • 1
    This works just fine for me and print `test` as output `docker run --entrypoint "powershell" --env HOSTNAME="test" microsoft/windowsservercore:1709 "gci Env:HOSTNAME"` Try putting you image after --env parameter – Gregory Suvalian Apr 18 '18 at 00:35
  • Yes, placing the image name after `--env` worked. Thanks! – Web User Apr 18 '18 at 05:10