I'm building a Docker image based from microsoft/powershell:ubuntu16.04
. It builds properly without any error, however when I go to use it it doesn't have the imported module that I stated in the Dockerfile (which is a psd1 file from a GitHub repo cloned in the image.
I tried with a simple import of Microsoft's powershell management as well (seen below), and it also doesn't get imported. I ran the image interactively and the commands one by one and everything imported fine, so I don't know why it doesn't work through the Dockerfile build itself.
FROM microsoft/powershell:ubuntu16.04
WORKDIR /workdir
RUN pwsh -command "Import-Module Microsoft.PowerShell.Management"
CMD [ "pwsh", "-Command", "Get-Module" ]
Example psd1 file
FROM microsoft/powershell:ubuntu16.04
RUN apt-get update && apt-get install git -y
RUN git clone https://github.com/DTW-DanWard/PowerShell-Beautifier.git
RUN cp -a PowerShell-Beautifier/src/. /opt/microsoft/powershell/6.0.0-rc/Modules
ENTRYPOINT [ "pwsh", "-c" ]
CMD [ "Get-Help", "Edit-DTWBeautifyScript" ]
Because it happens in a wrong "context". For this to work the way you want it to work you need to use both these commands in 1 powershell session:
pwsh -command "Import-Module Microsoft.PowerShell.Management; Get-Module"
else it creates a layer where it ran that command, but when powershell stops all the imports are gone (and when the layer is done, it shuts down the container, so it doesnt preserve session state, only os state).
My dockerfile (working example):
FROM microsoft/powershell:ubuntu16.04
RUN pwsh -c "Install-Module AzureRM.netcore -Force"
CMD [ "pwsh" ]