How to execute ps1 script using docker run command?
Asked Answered
W

1

5

I need to use the official PowerShell Core Docker image to run a Docker container and make it execute a PowerShell script file.

I know it can be done using a Dockerfile and Docker build, but can't use that in this case.

From reading docs I came up with this, but it does not seem to work:

docker run -it --rm --entrypoint "/tmp/test.ps1" repo/powershell:latest

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/tmp/test.ps1\": stat /tmp/test.ps1: no such file or directory": unknown.
ERRO[0001] error waiting for container: context canceled

Error seems to say that it can't find the file but when running stat "/tmp/test.ps1" manually it works fine.

I feel like the binary pwsh should also be specified but can't find a way how to do it.

Waylon answered 26/11, 2019 at 14:30 Comment(5)
i usually run what inside of the docker like this: docker run -it repo/powershell:latest /tmp/test.ps and since you don't need to stay inside -it is not requiredVoluptuous
Hi, thanks for the comment. Tried it but it gives the same error: docker run repo/powershell:latest /tmp/test.ps1 docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/tmp/test.ps1\": stat /tmp/test.ps1: no such file or directory": unknown.Waylon
Trying stat command afterwards: rob@linuxvm:/tmp$ stat /tmp/test.ps1 File: /tmp/test.ps1 Size: 32 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 931983 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ rob) Gid: ( 1000/ rob) Access: 2019-11-26 15:55:56.966119943 +0000 Modify: 2019-11-26 15:56:07.951610246 +0000 Change: 2019-11-26 15:56:07.951610246 +0000 Birth: -Waylon
just realized stat /tmp/test.ps1: no such file or directory powershell - means windows, and that means that /tmp/test.ps1 is not an absolute path from root. If so you should try something like C:\bla\bla\another_bla\tmp\test.ps1Voluptuous
I am using Ubuntu host with Docker. The PowerShell Core image is from Docker hub. And I believe the ps1 file is in the correct directory, new to Linux so maybe there's a mistake, but doing pwd in the dir returns /tmp/ so it's not /home/user/tmp or something.Waylon
V
6

docker run -it mcr.microsoft.com/powershell pwsh -c "Write-Host 'Hello, World'"

this produces Hello, World

and mcr.microsoft.com/powershell is what google return as official PowerShell Core Docker image

this image doesn't have file /tmp/test.ps1 inside. so not exactly clear what that image repo/powershell:latest have inside.

  • if you are trying to read a file and execute inside of docker than this worked just fine for me:
docker run -it --rm mcr.microsoft.com/powershell pwsh -c $(cat test.ps)
  • or, if you are trying to pass host file into the container and execute you can map local path to path inside of the container and than it would be:
docker run -v /tmp/localdata:/tmp/containerdata  -it --rm mcr.microsoft.com/powershell pwsh /tmp/containerdata/test.ps

assuming that you have on host /tmp/localdata folder which contains test.ps file with Write-Host 'Hello, World!' text.

both ways result is Hello, World

Voluptuous answered 26/11, 2019 at 22:19 Comment(1)
Thank you! The last one with volumes did the trick, I was trying to pass a file from the host, sorry for the confusion caused, I assumed --entrypoint was meant to do exactly that. Somehow missed the part about Volumes in the docs.Waylon

© 2022 - 2024 — McMap. All rights reserved.