Running PowerShell in a Docker container has many benefits, such as being able to run isolated environments, access to a large number of pre-built images, and more. However, one of the challenges of using PowerShell in Docker is being able to use custom modules. This can be especially important if you're relying on specific scripts or functions that are not included in the default set of PowerShell modules. In this article, we'll cover the steps for running PowerShell in Docker with custom modules, so you can take full advantage of the benefits that containers offer for your PowerShell scripts.
One of the reasons was that PowerShell “broke down” on my Windows 10 machine, or rather, it works, but the Get-Credential command returns an error. The second reason is simply the desire to experiment on the topic.
It turned out the truth is not so simple, details below. To run PowerShell in a Docker container with pre-loaded modules, you need to perform the following steps:
- Enable Virtualization Support in BIOS
- Install Hyper-V, you can do it like this:
- Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
DISM /Online /Enable-Feature /All /FeatureName: Microsoft-Hyper-V
- Install Docker Desktop for Windows
- Create an empty folder, say D:\powershell-docker
- Add the required modules to the subfolder D:\powershell-docker\Modules
- Create a Dockerfile in D:\powershell-docker as follows
# Use an existing docker image as a base
# Copy Cisco UCS Power Tools to the standard modules location
COPY ./Modules ./usr/local/share/powershell/Modules
# Create a path in advance, then add required path to the file
RUN mkdir -p /root/.config/powershell/RUN echo `Import-Module Cisco.UCSManager`> /root/.config/powershell/Microsoft.PowerShell_profile.ps1
- Open CMD or PowerShell and go to D:\powershell-docker
- Run the docker build -t powershell-docker command
- Start our container: docker run -it powershell-docker
The main problem was that when PowerShell Core started a container, it created a new session and importing modules via ENTRYPOINT or CMD did not lead to anything, the modules were accessible via Get-Module -ListAvailable, but they had to be imported manually, because autoloading does not work when using commands from available but not imported modules.
Another oddity/bug found is that the $profile variable (if you just see the value of this variable in a container running PowerShell Core points to the path /root/.config/powershell/Microsoft.PowerShell_profile.ps1, but this file does not exist.
As I said above, it does not make sense to redefine a profile, since at the start of a new session, $profile will again point to the same. Therefore, as an option to create this path and file and specify in it the list of modules to import, which was done above in the Dockerfile.
I hope the information was useful, if you have any questions, ask in the comments, and send any code improvements as a pull request to the github, where the source code is posted.