Small Tips for Docker with Powershell

This entry expands on a special aspect of my powershell setup on multiple plattforms.
I wrote about this a while back.
Shot with @expeditionxdrone
Photo by chuttersnap / Unsplash


When writing a command over and over again create an alias witch points to a nice function. E.g.

Utility Functions

While using docker for the last years I learned a lot. Now that I'm giving that rough knowledge to my colleagues and while doing that I noticed some things that might be helpful for the general public concerned with docker and/or powershell.

When getting into docker a bit deeper then just starting a single container an destroying that when your done, I noticed that some functions you expect to have are missing from the docker(-machine) CLI.
Following are the examples that I as of now deemed worthy of being turned into a function that is easily callable via an alias:

Remove Every Running Container

When testing docker you will leave containers running. This is especially true when using windows as the host plattform and due to the fact that auto remove via --rm does not work properly in all shells.

For that usecase I wrote the following little function:

function Remove-AllDockerContainers {
  docker ps -aq `
      | ForEach-Object { docker rm -vf $_ }

Executing this will pipe the id of every existing(running or not) container into the remove command with options for removal of volumes being added.

This later evolved into the following:

function Remove-AllDockerContainers {
  docker ps -aq `
      | ForEach-Object { docker stop -t 1 $_ } `
      | ForEach-Object { docker rm -vf $_ }

The stop with little timeout was added due to some special cases where the container process wasn`t willing to die easily.

Remove untaged Images

function Remove-AllUntagedDockerImages {
  docker images `
      | ConvertFrom-String `
      | Where-Object {$_.P2 -eq "<none>"} `
      | ForEach-Object { docker rmi $_.P3 }

This is pretty self explanatory. Search for all the images that have no () tag and remove them.

On board functions

To be fair the docker CLI has tools to do similar things.
docker system prunf (-f) might be the most notable. It removes basically everything that's not directly needed or just everything that doesn't stop any containers from running. For my purposes this is still the rough and for that reason I keep the functions above in my powershell(and pwsh) profile.

Get the content of any registry

function Get-DockerRegistryContent {
  $allRepos = (Invoke-RestMethod -Uri "http://$RegistryEndpoint/v2/_catalog" -Method Get ).repositories 
  $reposMatchingFilter = $allRepos -Match $Filter
  $reposMatchingFilter | ForEach-Object {(Invoke-RestMethod -Uri "http://$RegistryEndpoint/v2/$_/tags/list" -Method Get )}

Speaking of on board functions. This one for some reason is nowhere to be found in the docker CLI. It uses the rest api of any given docker registry to give you and overview of its contents. I`m sure that I miss the bigger picture here but for the last couple of years this baffled me.

As always I`m happy about feedback and will answer questions if there are any. The ways of contact are on this site.

comments powered by Disqus