Run a PowerShell Script With Dockerfile Image

In this post, we will package a PowerShell script inside a Docker image with Dockerfile.

Running a PowerShell script inside a Docker container can be achieved using a few methods, and today we will cover the most effective way to do it.

Using a Dockerfile, we can create a Docker image that runs on the official PowerShell 7 on Linux. In the following code, we also use the WORKDIR statement that set where the script will be located.

The WORKDIR statement creates a directory on the container file system that the CMD and ENTRYPOINT will look for the script that we will run on startup.

Dockerfile

In the following Dockerfile, we are using the latest PowerShell image to run the image. We create a directory called app on the container using the WORKDIR command. In the third line, we copy a PowerShell script located in the same folder the Dockerfile is located.

The final line of code runs the copied PowerShell script on startup.

FROM mcr.microsoft.com/powershell
WORKDIR /app
COPY . /app
CMD [ "pwsh", "run.ps1"]

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.