Skip to content

Instantly share code, notes, and snippets.

@omartrigui
Created April 26, 2019 21:09
Show Gist options
  • Save omartrigui/a53bcff8830a333d7bb37fee4fe0f14b to your computer and use it in GitHub Desktop.
Save omartrigui/a53bcff8830a333d7bb37fee4fe0f14b to your computer and use it in GitHub Desktop.
Docker aliases
alias dockercleancontainers="docker ps -aq | xargs docker rm"
alias dockercleanimages="docker images -aq -f dangling=true | xargs docker rmi"
alias dockerclean="dockercleancontainers && dockercleanimages"
alias docker-killall="docker ps -q | xargs docker kill"
# runs docker exec in the latest container
function docker-exec-last {
docker exec -ti $( docker ps -a -q -l) /bin/bash
}
function docker-get-ip {
# Usage: docker-get-ip (name or sha)
[ -n "$1" ] && docker inspect --format "{{ .NetworkSettings.IPAddress }}" $1
}
function docker-get-id {
# Usage: docker-get-id (friendly-name)
[ -n "$1" ] && docker inspect --format "{{ .ID }}" "$1"
}
function docker-get-image {
# Usage: docker-get-image (friendly-name)
[ -n "$1" ] && docker inspect --format "{{ .Image }}" "$1"
}
function docker-get-state {
# Usage: docker-get-state (friendly-name)
[ -n "$1" ] && docker inspect --format "{{ .State.Running }}" "$1"
}
function docker-memory {
for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB ; done
}
# keeps the commmand history when running a container
function basher() {
if [[ $1 = 'run' ]]
then
shift
docker run -e HIST_FILE=/root/.bash_history -v $HOME/.bash_history:/root/.bash_history "$@"
else
docker "$@"
fi
}
# backup files from a docker volume into /tmp/backup.tar.gz
function docker-volume-backup-compressed() {
docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie tar -czvf /backup/backup.tar.gz "${@:2}"
}
# restore files from /tmp/backup.tar.gz into a docker volume
function docker-volume-restore-compressed() {
docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie tar -xzvf /backup/backup.tar.gz "${@:2}"
echo "Double checking files..."
docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie ls -lh "${@:2}"
}
# backup files from a docker volume into /tmp/backup.tar
function docker-volume-backup() {
docker run --rm -v /tmp:/backup --volumes-from "$1" busybox tar -cvf /backup/backup.tar "${@:2}"
}
# restore files from /tmp/backup.tar into a docker volume
function docker-volume-restore() {
docker run --rm -v /tmp:/backup --volumes-from "$1" busybox tar -xvf /backup/backup.tar "${@:2}"
echo "Double checking files..."
docker run --rm -v /tmp:/backup --volumes-from "$1" busybox ls -lh "${@:2}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment