This is the exact use case I recently needed to implement, and you can indeed get Powershell Core up and running from a .NET Core SDK image - in my case the below was done against image mcr.microsoft.com/dotnet/core/sdk:2.2-stretch.
The full solution looks something like as follows:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
...
FROM build AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet tool install --global PowerShell \
&& ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh
Where:
dotnet tool install --global PowerShell installs PowerShell into the container. However this alone is not quite good enough because as observed in @amirtharaj-rs answer, the command pwsh will not work unless invoked from the PowerShell installation directory.
For example, you may encounter something like:
root@a1d30119664d:/app# pwsh
bash: pwsh: command not found
ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh creates a link called pwsh which points to where PowerShell is installed. This is the key line which enables pwsh to be invoked from anywhere:
root@a1d30119664d:/app# pwsh
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
If the above does not work, then you may need to ensure that the execute bit is set on /usr/bin/pwsh. You can check this by running ls -l /usr/bin/pwsh.
If you do need to set the execute bit, add chmod 755 /root/.dotnet/tools/pwsh to the Dockerfile RUN command. So the complete RUN command would look something like:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
...
FROM build AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet tool install --global PowerShell \
&& ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh
&& chmod 755 /root/.dotnet/tools/pwsh
If interested, I learnt the above via the discussion history on this Github issue: https://github.com/Microsoft/azure-pipelines-agent/issues/1862.
RUN apt install powershell