Skip to content

Instantly share code, notes, and snippets.

@dflock
Last active April 9, 2019 10:57
Show Gist options
  • Save dflock/ed671bd5119af6ec39b3 to your computer and use it in GitHub Desktop.
Save dflock/ed671bd5119af6ec39b3 to your computer and use it in GitHub Desktop.
Docker bash aliases.

docker_aliases

Docker bash aliases - new bash shell commands to make working with Docker containers & images easier.

Installation

Save the docker_aliases.sh file to your home directory. Rename it to .docker_aliases and add this to your ~/.bashrc file somewhere:

if [ -f ~/.docker_aliases ]; then
  source ~/.docker_aliases
fi

Then either close & re-open your terminal windows or do this in each one to make it refresh:

$ . ~/bashrc

Usage

Installing docker_aliases will get you some new commands to use in your bash terminal:

dps

Like docker ps, but with two extra columns on the end, showing the containers local IP address and it's current RAM usage. Looks like this:

  $ dps

  CONTAINER ID        IMAGE           ... NAMES                 IP              RAM       
  0a3359f50829        23f1a66316b3    ... container-one         172.17.0.139         57 MB
  63a1b8ab9fb5        037e0afb42e4    ... container-two         172.17.0.137        490 MB
  c9bbee45d255        872fb65cee6d    ... container-three       172.17.0.135       1696 MB

Accepts the same command line switches as docker ps - i.e. dps -a works.

docker_wipe

Delete all containers & images, reset dockers container linking DB and restart docker. The nuclear option.

NB: Does not prompt for confirmation.

$ docker_wipe

dvol

List the volumes for a given container:

$ dvol <container name|id>
#!/usr/bin/env bash
#
# Docker Aliases
#
# Figure out if we need to use sudo for docker commands
if id -nG "$USER" | grep -qw "docker"; then
DSUDO=''
else
DSUDO='sudo'
fi
#
# List the RAM used by a given container.
# Used by dps().
#
# docker_mem <container name|id>
#
docker_mem() {
if [ -f /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes ]; then
echo $(( $(cat /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes) / 1024 / 1024 )) 'MB'
else
echo 'n/a'
fi
}
#
# Return the ID of the container, given the name.
#
# docker_id <container_name>
#
docker_id() {
ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
if (( $? >= 1 )); then
# Container doesn't exist
ID=''
fi
echo $ID
}
#
# Return the status of the named container.
#
# docker_up <container_name>
#
docker_up() {
UP='Y'
ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
if (( $? >= 1 )); then
# Container doesn't exist
UP='N'
fi
echo "$UP"
}
#
# List the IP address for a given container:
# Used by dps().
#
# docker_ip <container name|id>
#
docker_ip() {
IP=$($DSUDO docker inspect --format="{{.NetworkSettings.IPAddress}}" "$1" 2> /dev/null)
if (( $? >= 1 )); then
# Container doesn't exist
IP='n/a'
fi
echo $IP
}
#
# Enhanced version of 'docker ps' which outputs two extra columns:
#
# IP : The private IP address of the container
# RAM : The amount of RAM the processes inside the container are using
#
# Usage: same as 'docker ps', but 'dps', so 'dps -a', etc...
#
dps() {
tmp=$($DSUDO docker ps "$@")
headings=$(echo "$tmp" | head --lines=1)
max_len=$(echo "$tmp" | wc --max-line-length)
dps=$(echo "$tmp" | tail --lines=+2)
if [[ -n "$dps" ]]; then
printf "%-${max_len}s %-15s %10s\n" "$headings" IP RAM
while read -r line; do
container_short_hash=$( echo "$line" | cut -d' ' -f1 );
container_long_hash=$( $DSUDO docker inspect --format="{{.Id}}" "$container_short_hash" );
container_name=$( echo "$line" | rev | cut -d' ' -f1 | rev )
if [ -n "$container_long_hash" ]; then
ram=$(docker_mem "$container_long_hash");
ip=$(docker_ip "$container_name");
printf "%-${max_len}s %-15s %10s\n" "$line" "$ip" "${ram}";
fi
done <<< "$dps"
fi
}
#
# List the volumes for a given container:
#
# dvol <container name|id>
#
dvol() {
vols=$($DSUDO docker inspect --format="{{.HostConfig.Binds}}" "$1")
vols=${vols:1:-1}
for vol in $vols
do
echo "$vol"
done
}
#
# Wipe and reset Docker - removing all containers & images,
# resetting the linkgraph.db & restarting docker.
#
docker_wipe() {
$DSUDO docker rm -f $(docker ps -a -q)
$DSUDO docker rmi -f $(docker images -q)
sudo rm "/var/lib/docker/linkgraph.db"
sudo restart docker
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment