Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a docker image from dotnet core 2.2 and powershell core?

I'm trying to "dockerize" a dotnet standard library, for now I'm using a simple docker file, to build and pack it using just dotnet cli commands.

# Build stage
FROM microsoft/dotnet:2.2-sdk as build
ARG Version
WORKDIR /src
COPY . .

RUN dotnet restore
RUN dotnet build -p:Version=$Version -c Release --no-restore

RUN dotnet pack -p:Version=$Version -c Release --include-symbols --no-restore --no-build -o /src/.artifacts

I want to be able to run a powershell script ci.ps1 from my docker file, but there is no powershell core installed in the dotnet:2.2-sdk.

Is there any examples out there on how to run powershell core scripts from inside a dotnet image ? How can I create my own image from powershell core and dotnet sdk ?

Thanks

like image 959
user3574857 Avatar asked Nov 19 '25 02:11

user3574857


1 Answers

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.

like image 145
JT_ Avatar answered Nov 21 '25 16:11

JT_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!