Skip to content

Instantly share code, notes, and snippets.

@mudphone
Last active August 22, 2016 02:49
Show Gist options
  • Save mudphone/7ef66f1d4d85599e467cd0c84a407355 to your computer and use it in GitHub Desktop.
Save mudphone/7ef66f1d4d85599e467cd0c84a407355 to your computer and use it in GitHub Desktop.
Docker Things

From Docker in Practice, by IAan Miell, Aidan Hobson Sayers

ping each running container

docker ps -q | xargs docker inspect --format='{{.NetworkSettings.IPAddress}}' | xargs -l1 ping -c1

use host's bash history

function basher() {
  if [[ $1 = 'run' ]]
      shift
      /usr/bin/docker run -e HIST_FILE=/root/.bash_history -v $HOME/.bash_history:/root/.bash_history "$@"
  else
    /usr/bin/docker "$@"
  fi 
}
alias docker=basher

delete all containers (running and stopped)

docker ps -a -q | xargs --no-run-if-empty docker rm -f

delete selected containers (exited | running | restarting)

docker ps -a -q --filter status=exited | xargs --no-run-if-empty docker rm

list all containers with a non-zero error code

comm -3 <(docker ps -a -q --filter=status=exited | sort) <(docker ps -a -q --filter=exited=0 | sort) | xargs --no-run-if-empty docker inspect > error_containers

dry run, and remove unmounted volumes

docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --privileged dockerinpractice/docker-cleanup-volumes --dry-run
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --privileged dockerinpractice/docker-cleanup-volumes

watch for down time

watch -n .2 curl test.com
siege test.com

Create Postgres volume container

docker run --name dbdata postgres echo "Data-only container for postgres"

Use Postgres volume container

docker run -d --volumes-from dbdata --name db1 postgres

Backup Postgres volume container

docker run --rm --volumes-from dbdata -v $(pwd):/backup debian tar cvf /backup/backup.tar /var/lib/postgresql/data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment