Skip to content

Instantly share code, notes, and snippets.

@bihe
Created January 1, 2025 15:44
Show Gist options
  • Save bihe/ba030bd493fca1d08aef5d281fb7d30f to your computer and use it in GitHub Desktop.
Save bihe/ba030bd493fca1d08aef5d281fb7d30f to your computer and use it in GitHub Desktop.
Docker helper functions / aliases

Helpful docker commands which should be aliased to save some keystrokes

alias docker-container-clear='docker rm -f $(docker ps -a -q)'
alias docker-image-clear='docker image remove -f $(docker images -a -q)'
alias docker-clear='docker system prune'

Windows/powershell (please use the modern variant: https://github.com/PowerShell/PowerShell) cannot create aliases for commands with arguments. To work-around this issue the best approach is to create a function for the command and alias the function.

function Get-SystemEventlog {Get-Eventlog -LogName System}
Set-Alias -Name syslog -Value Get-SystemEventlog

Using this approach the docker helper commands can be used like this:

function RemoveDockerContainers { docker ps -a -q | foreach { docker rm -f $_ } }
Set-Alias -name docker-container-clear -Value RemoveDockerContainers

function RemoveDockerImages { docker images -aq | foreach {docker rmi $_} }
Set-Alias -name docker-image-clear -Value RemoveDockerImages

function PruneDocker { docker system prune }
Set-Alias -name docker-clear -Value PruneDocker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment