Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Forked from sixeyed/Set-ProfileForDocker.ps1
Last active January 10, 2021 05:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/62ab08e13ed9ae264f4816ee5fc85aec to your computer and use it in GitHub Desktop.
Save pythoninthegrass/62ab08e13ed9ae264f4816ee5fc85aec to your computer and use it in GitHub Desktop.
PowerShell aliases for working with Docker on Windows - save to $profile. Ignore PoshGit and Chocolatey if neither is installed.
Rename-Item Function:\Prompt PoshGitPrompt -Force
function Prompt() {if(Test-Path Function:\PrePoshGitPrompt){++$global:poshScope; New-Item function:\script:Write-host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) " -Force | Out-Null;$private:p = PrePoshGitPrompt; if(--$global:poshScope -eq 0) {Remove-Item function:\Write-Host -Force}}PoshGitPrompt}
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# Docker Module
Install-Module posh-docker -Scope CurrentUser
# Docker Aliases
# @ https://www.ctl.io/developers/blog/post/15-quick-docker-tips/
# @ https://gist.github.com/sixeyed/adce79b18c5f572feaf34ae9e90513c2
# dl
function Return-ContainerID {(docker ps -l -q)}
# docker rm $(docker ps -a -q)
function Remove-StoppedContainers {
foreach ($id in & docker ps -a -q) {
& docker rm $id }
}
# docker rmi $(docker images -f "dangling=true" -q)
function Remove-DanglingImages {
foreach ($id in & docker images -q -f 'dangling=true') {
& docker rmi $id }
}
# docker volume rm $(docker volume ls -qf dangling=true)
function Remove-DanglingVolumes {
foreach ($id in & docker volume ls -q -f 'dangling=true') {
& docker volume rm $id }
}
# docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' <id>
function Get-ContainerIPAddress {
param (
[string] $id
)
& docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' $id
}
New-Alias dl Return-ContainerID
New-Alias drm Remove-StoppedContainers
New-Alias drmi Remove-DanglingImages
New-Alias drmv Remove-DanglingVolumes
New-Alias dip Get-ContainerIPAddress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment