Skip to content

Instantly share code, notes, and snippets.

@rcmorano
Last active July 28, 2022 13:23
Show Gist options
  • Save rcmorano/9150530 to your computer and use it in GitHub Desktop.
Save rcmorano/9150530 to your computer and use it in GitHub Desktop.
my miscelaneous docker cheat sheet

rcmorano docker cheat sheet

Remove recursively zombie containers

In e.g.: you 'docker run -t -i' a 'bash' session and '.bashrc' is fucked up, container will forever respawn disallowing you to 'docker rmi' the used image .

docker rmi triangle/ubuntu-saucy-with-rvm 2>&1|grep ^Error|awk '{print $10}'|xargs docker rm

This will try to kill any running instance avoiding it to respawn again.

Removing containers

Try to remove every non running container:

docker ps -a | grep -v ^CONTAINER | grep Exit | awk '{print $1}' | xargs docker rm

Remove every container:

docker ps -a | grep -v ^CONTAINER | awk '{print $1}' | xargs -I % sh -c 'docker kill %; docker rm %'

Run a "rvm friendly" bash session

In e.g.: you get the disturbing:

rcmorano@rigodon3:~$ docker run -v "$(pwd):/tmp/cucumber" -t -i triangle/ubuntu-saucy-with-rvm /bin/bash
WARNING: WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: [8.8.8.8 8.8.4.4]
root@fd0a737aec69:/# rvm use --default ruby-head

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
root@fd0a737aec69:/#

You can run the 'bash' session this way:

docker run -t -i triangle/ubuntu-saucy-with-rvm /bin/bash --login

Dettaching from containers

Thanks to jpetazzoni by the explanation in the mailing list.

  • Containers run this way can be detached with ^P^Q and reattached with docker attach:
docker run -t -i
  • Containers run this way cannot be detached with ^P^Q; will disrupt stdin:
docker run -i
  • Containers run this way cannot be detached with ^P^Q; can SIGKILL client; can reattach with docker attach:
docker run

bash aliases

alias docker-container-most-recent='docker ps| grep -v ^CONTAINER | head -n1 | awk "{print \$1}"'
alias docker-container-diff-most-recent='LAST_CONTAINER=$(docker-container-most-recent); if [ ! -z "$LAST_CONTAINER" ]; then docker diff $LAST_CONTAINER; else echo "There are no running containers!"; fi'
alias docker-container-inspect-most-recent='LAST_CONTAINER=$(docker-container-most-recent); if [ ! -z "$LAST_CONTAINER" ]; then docker inspect $LAST_CONTAINER; else echo "There are no running containers!"; fi'
alias docker-container-remove-all='docker ps -a | grep -v ^CONTAINER|awk "{print \$1}" | xargs -I % sh -c "docker kill %; docker rm %"'
alias docker-container-remove-all-non-running='docker ps -a | grep -v ^CONTAINER | grep Exit | awk "{print \$1}" | xargs -I % sh -c "docker kill %; docker rm %"'
alias docker-image-remove-all='docker-container-remove-all; docker images -a | grep -v ^REPOSITORY | awk "{print \$3}" | xargs docker rmi'

real cheat sheets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment