3

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

3
  • Did you try to install it. Like RUN apt install powershell Commented Apr 7, 2019 at 19:29
  • Tried apt-get update && apt-get install -y powershell, but I have an error : Unable to locate package powershell Commented Apr 7, 2019 at 19:34
  • Maybe this link to ms documentation can help. They recommand to install the Microsoft repository GPG keys first Commented Apr 7, 2019 at 19:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

dotnet global tool provide powershell support which can be used as pwsh.

FROM mcr.microsoft.com/dotnet/core/sdk:2.2
USER ContainerAdministrator
WORKDIR /app
RUN dotnet tool install --global PowerShell
RUN setx /M PATH "%PATH%;C:\Users\ContainerUser\.dotnet\tools"
COPY ["publish/"," ."]
ENTRYPOINT ["C:\\Users\\ContainerAdministrator\\.dotnet\\tools\\pwsh.exe", "C:\\app\\PowershellScript.ps1"]

even after setting path i am not able use pwsh (powershell ) directly but after going inside installed path ..it provide powershell functionality

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.