How can I run a powershell 7 script as a main entry point for docker?

1

My goal is to build a docker container that will run a powershell file as its main entry point. I need the latest 7.x version of powershell rather than the standard out-of-the-box 5.1 version. So far, nothing I've tried is able to make this work.

This is my dockerfile

# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN mkdir 'c:\tools' ; `
    (New-Object System.Net.WebClient).DownloadFile('https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/PowerShell-7.1.3-win-x64.msi', 'c:\tools\Powershell7-x64.msi') ; `
    Start-Process 'msiexec' -ArgumentList '/i c:\tools\Powershell7-x64.msi /quiet /qn /norestart /log c:\tools\installPowershell7.log' ; `
    Start-Sleep -s 30 ; `
    Set-ExecutionPolicy RemoteSigned ; `
    Remove-Item c:\tools\*.msi -force

COPY *.ps1 c:\tools\
RUN c:\tools\fixpath.ps1

ENTRYPOINT [ "pwsh", "c:\tools\helloworld.ps1" ]

this is fixpath.ps1:

[System.Environment]::SetEnvironmentVariable('Path',"$($Env:Path);c:\Program Files\PowerShell\7\")

and this is helloworld.ps1:

write-host "hello world"

I build it with the usual: docker build -t issue1 .

I can confirm that the files are all there and pwsh starts powershell by running docker run -it --entrypoint pwsh issue1. However, as soon as I try to run it normally, using the entrypoint defined in the dockerfile (i.e. docker run issue1), I get this error:

Unable to find type [pwsh,c:\tools\helloworld.ps1]. Details: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) At line:1 char:76

  • ... rogressPreference = 'SilentlyContinue'; [ pwsh, c:\tools\helloworld.ps1 ]
  •                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (pwsh,c:\tools\helloworld.ps1:Type Name) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TypeNotFoundWithMessage

What can I do to resolve or work around this issue?

What else have I tried?:

  • ENTRYPOINT vs CMD - both produce the same error message
  • This is a minimal reproduction of the issue
  • running docker run -it --entrypoint powershell issue1 and then running pwsh c:\tools\helloworld.ps1 within that shell works properly.
windows
docker
powershell
asked on Stack Overflow Apr 28, 2021 by davidpricedev

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0