Adding Docker Containers to Windows Terminal

On my latest project I’ve been debugging Windows Server 2016 & Windows Server 2019 issues using the Microsoft Server Core docker containers.

I’m also a huge fan of the (not-so) new Windows Terminal, and wanted to find a way to incorporate a “fresh debugging environment” into a new tab.

2020-12-29-Adding-Docker-to-Windows-Terminal

Windows Terminal Settings

Those of you that have already customised your Windows Terminal know that the “settings screen” is a JSON code block that opens in your default editor (VS Code for me). It looks something like this: Windows Terminal settings

Add your new Docker commands as Profiles

In the list of Windows Terminal “Profiles” you’ll see all the default ones it was able to detect on your system, normally PowerShell and cmd.exe.

To add a new profile, just copy one of the existing entries and change the GUID and the name values. I’ve prefixed my docker container profiles with a whale (🐳) so that I know it’s hosted in docker.

{
    "guid": "{a25a83da-5bd2-4c68-97b8-a8720a80cff7}",
    "name": "🐳 Windows Server 2016",
    "commandline": "docker run --rm -it --name windows-terminal-2016 mcr.microsoft.com/windows/servercore:ltsc2016 powershell",
    "suppressApplicationTitle": true
},

The key value here is the commandline argument! We can pass in any command that we want cmd.exe to run!

Breaking down the command value

{
    ...
    "commandline": "docker run --rm -it --name windows-terminal-2016 mcr.microsoft.com/windows/servercore:ltsc2016 powershell",
    ...
}

To break down the command, what we’re asking is:

  1. launch docker,
  2. ask it to run a container,
  3. ask it to remove the container when it exits (--rm),
  4. ask it to attach the standard inputs (-it),
  5. give the container a name (--name windows-terminal-2016)
  6. and then using this base image: mcr.microsoft.com/windows/servercore:ltsc2016
  7. run powershell to give us a prompt!

Yay, that’s it!

Small gotchas…

  1. Even though we asked Docker to remove the container when it exits, that only happens when you actually exit from Powershell in the container. If you close the tab (Ctrl+F4, or clicking the cross) then the container doesn’t catch the exit signal and keeps on running. This means that next time you try and load the “tab” it’ll fail with an “container with that name already exists”-error. You can avoid this error by removing the name from the command (it’ll automatically generate a distinct one), but I’d prefer it like this for now.

  2. If you’re downloading a large container for the first time (or the container image has a significant update) this can lock up Windows Terminal until the image download is complete. I would recommend adding --pull never to the command if this is a problem for you.

  3. Asking for the latest servercore image doesn’t work (BY DESIGN), so you’ll have to find your specific version at on this page and reference it explicitly!

  4. I’ve set suppressApplicationTitle to true in order to leave our title-bar alone and for me to easily locate the debugging tab. It’ll have the whale 😁

Extending this logic

Knowing that you can run ‘any’ command, I’ve also added a divider to separate out my docker containers:

Windows Terminal drop-down menu

The ‘profile’ that I set up for the divider (if it’s accidentally launched) is this:

{
    "name": "---- Dockerised Containers ----",
    "commandline": "powershell -NoLogo -NoExit -Command \"& { Write-Warning 'Why did you launch me!? Ah well, here are all ya dockers:'; docker ps -as }\""
},

I hope this helps if you’re looking for a quick and easy way to launch a dockerised debugging environment!


I'm Pat Hermens, an Australian that's spent a decent amount of time living & working in the Netherlands.
I am a .NET developer (for the last 20 years or so), a technical manager, a public speaker, a happy husband and father, but most of all; I'm just a nerd.
I'm currently a Principal at Slalom Build in Sydney, Australia, and I was previously Development Manager at Coolblue in Rotterdam, NL.

Leave a comment

avatar