PSReadLine: Powershell crashes microsoft/powershell docker container
Running Docker Edge on MAC OSX 10.12.6 From within iterm2 pulled microsoft/powershell:latest container. spin up the container ok and can docker exec into the container ok
run push in container to start powershell and receive the following output:
root@3d90889506e0:/# pwsh
PowerShell v6.0.2
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS />
Oops, something went wrong. Please report this bug with the details below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new
-----------------------------------------------------------------------
Last 0 Keys:
Exception:
System.DivideByZeroException: Attempted to divide by zero.
at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------
Steps to reproduce
docker pull microsoft/powershell docker run --name=mypowershell -itd microsoft/powershell:latest docker exec -it mypowershell /bin/bash
type pwsh at prompt
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 3
- Comments: 16 (6 by maintainers)
My investigation shows that the
DivideByZeroException
exception is caused by having the container detached. When runningdocker run -itd
to start the powershell container, since the container is detached,System.Console.BufferWidth
andSystem.Console.BufferHeight
are both 0, meaning the console windows doesn’t exist, and it’s actually a broken state. That leads to theDivideByZeroException
.I don’t think there is anything to fix in PSReadLine. As a workaround, if you need to start the container in a detached mode, then instead of using the default CMD, which is really just for interactive attached usage, just explicitly specify a command to prevent things from shutting down, such as
Then, you can run
docker exec -it mypowershell /bin/bash
ordocker exec -it mypowershell pwsh
For the “Remote - Container” extension in VSCode, now the extension automatically adds a
docker-compose.yml
file to override the default CMD of a container image. The overriding command is just a loop, to prevents the container from shutting down.So I think you don’t need to do anything extra, and it will just work. If the overriding command is not automatically added, then try adding it by yourself.
The issue seems to be with these lines: https://github.com/PowerShell/PSReadLine/blob/fd1265e385cb4b7b860d8f1452da1ceb347e123e/PSReadLine/Render.cs#L489-L498 i.e. as BufferWidth is zero.
Below’s some additional info (sadly copy-paste is not possible from the Docker for Desktop logs panel, where this info’s available).
<?>Appears to still be a thing in 6.1; I’m seeing the same issue in
The initScript.ps1 just unpacks some files before starting a dotnet application.
If I exec -ti into the container I’ll get this error sometimes (just after starting pwsh, without entering anything):
And then if I kill the container and start the exact same image again, everything might work smoothly with no error except
Error reading or writing history file '/home/appuser/.local/share/powershell/PSReadLine/ConsoleHost_history.txt': Access to the path '/home/appuser/.local/share/powershell/PSReadLine' is denied.
(which doesn’t really concern us). Next restart things might still be fine, maybe the one after that as well, and then suddenly a failure. All running the exact same container with the exact same data in it.