How to Set Up a Docker Container with PowerShell on Windows?

0
0
Asked By DockerDude42 On

Hey everyone! I'm trying to set up a Docker container using a Microsoft Windows image, and I need to make sure that PowerShell is included so I can run some specific scripts. Unfortunately, I'm running into a roadblock because PowerShell isn't readily available in the container. I'm currently using the Windows Server Core image (which should have PowerShell) but I'm considering a few approaches: 1. Using the server core image, 2. Downloading and installing PowerShell Core, or 3. Trying different base images. Here's the Dockerfile I'm working with:

```Dockerfile
FROM mcr.microsoft.com/windows/servercore:ltsc2022

# Download PowerShell Core
ADD https://github.com/PowerShell/PowerShell/releases/download/v7.4.1/PowerShell-7.4.1-win-x64.zip C:/PowerShell.zip

# Extract PowerShell Core using PowerShell
SHELL ["C:WindowsSystem32WindowsPowerShellv1.0powershell.exe", "-Command"]
RUN Expand-Archive -Path C:PowerShell.zip -DestinationPath C:PowerShell;
Remove-Item -Path C:PowerShell.zip

# Set PowerShell Core as the shell
SHELL ["C:PowerShellpwsh.exe", "-Command"]

# Install MicrosoftTeams module
RUN Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted;
Install-Module -Name MicrosoftTeams -Force -AllowClobber

# Set working directory
WORKDIR C:/app

# Copy script
COPY service.ps1 .

# Expose port
EXPOSE 8081

# Run script
CMD ["C:PowerShellpwsh.exe", "-File", "C:/app/service.ps1"]
```

2 Answers

Answered By LinuxLover99 On

Have you thought about using a Linux image instead? You can build a PowerShell image on Linux! Check out the official guide on installing PowerShell on Linux for tips.

DockerDude42 -

Honestly, I'm flexible with the base image as long as I can run PowerShell and install the modules I need. Just looking to get PowerShell running in any setup!

Answered By TechSavvyCat On

I find this approach quite fascinating! But just curious, why did you choose a Docker container instead of a virtual machine? Running Windows like this is a bit unusual in Docker, don’t you think?

DockerDude42 -

Great question! I have another container running a FastAPI service that needs to process incoming data. The specific actions I need can't be done through the Microsoft Graph API and require some PowerShell scripting. To keep things neat and modular, I opted to run a separate container just for PowerShell. That way, my FastAPI container can communicate with the PowerShell container for those tasks.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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