dotnet / dotnet-docker

Docker images for .NET and the .NET Tools.
https://hub.docker.com/_/microsoft-dotnet
MIT License
4.45k stars 1.93k forks source link

Avoid silent failures in Dockerfiles when executing PowerShell #3943

Open mthalman opened 2 years ago

mthalman commented 2 years ago

Our Dockerfiles are authored to execute multi-statement PowerShell scripts with statement semi-colon separators. This allows for multiple statements to be executed in a single command line. The issue with this is that it doesn't handle error conditions like we would want. If one of the statements is executing a native command, not a PowerShell cmdlet, and that command fails, execution of the PowerShell script will continue. This is in spite of the $ErrorActionPreference variable which only applies to cmdlets and functions. The exit code of the native command would not to be explicitly checked to handle things correctly. An example of a native command that gets called like this is tar which may end up failing.

Note that PowerShell 7 has support for pipeline chain operators like && and || that do account for the exit code of native commands. However, we're often needing to execute PowerShell commands before PowerShell Core is even installed, so we are making use of the PowerShell 5 version installed in Windows Server Core.

Options for solving this:

NikolaMilosavljevic commented 3 weeks ago

I've prepared some changes for this work-item and shared in https://github.com/NikolaMilosavljevic/dotnet-docker/tree/powershell.templates

The changes were tested, but not sufficiently.

This work uses the second approach - to split Powershell commands into individual native commands, where possible. The only remaining Powershell command is for calculating the checksum.

This work would need to be modified and partly redone, due to changes I'm making for https://github.com/dotnet/dotnet-docker-internal/issues/5952