Run a PowerShell Script With Dockerfile Image

In this Docker blog post, I will show you how to run a PowerShell script with Dockerfile image.

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 an 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 where the CMD and ENTRYPOINT will look for the script we will run on startup.

Run a PowerShell Script With Dockerfile Image

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 where 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"]