CodexBloom - Programming Q&A Platform

PowerShell 7.3 - Difficulty in Running a Script with External Module Dependencies in a Docker Container

👀 Views: 72 💬 Answers: 1 📅 Created: 2025-06-29
powershell docker modules

I'm trying to implement I'm trying to run a PowerShell script inside a Docker container that relies on some external modules... I'm using PowerShell 7.3 and my Dockerfile is set up to install the necessary modules, but when I try to execute the script, I encounter the behavior `Import-Module : The term 'ModuleName' was not recognized as the name of a cmdlet`. I've verified that the modules are actually installed by running `Get-Module -ListAvailable` inside the container, and they appear to be present. Here's a snippet of my Dockerfile: ```dockerfile FROM mcr.microsoft.com/powershell:7.3-ubuntu-20.04 # Set the working directory WORKDIR /app # Copy the script and module files into the container COPY script.ps1 . COPY Modules/ .\Modules # Install the required modules RUN pwsh -Command "Install-Module -Name ModuleName -Force -SkipPublisherCheck" # Entry point ENTRYPOINT ["pwsh", "-File", "script.ps1"] ``` The vital part is that the `script.ps1` file has a line at the top that tries to import the module using `Import-Module -Name ModuleName`. However, when I run the container with `docker run mycontainer`, the behavior pops up, indicating it want to find the module. I’ve also tried specifying the full path to the module like this: ```powershell Import-Module -Name .\Modules\ModuleName.psd1 ``` But I still get the same behavior. I suspect it might be related to the execution policy or the way the modules are being loaded in the container environment. Any suggestions on how to resolve this scenario and successfully run my script with the needed module dependencies in a Docker container?