Forked from garystafford/helpful-docker-commands.sh
Created
September 1, 2018 19:15
-
-
Save cryptosecdev/f96a9c39b723666d291fbd35a7880f88 to your computer and use it in GitHub Desktop.
My list of helpful docker commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################################################### | |
| # Helpful Docker commands and code snippets | |
| ############################################################################### | |
| ### CONTAINERS ### | |
| docker ps -a # list all containers | |
| docker stop $(docker ps -a -q) #stop ALL containers | |
| docker rm $(docker ps -a -q) # remove ALL containers | |
| docker rm $(sudo docker ps --before="container_id_here" -q) # can also filter | |
| # helps with error: 'unexpected end of JSON input' | |
| docker rm -f $(docker ps -a -q) # Remove all in one command with --force | |
| docker exec -i -t "container_name_here" /bin/bash # Go to container command line | |
| # to exit above use 'ctrl p', 'ctrl q' (don't exit or it will be in exited state) | |
| ### IMAGES ### | |
| # list images and containers | |
| docker images # all images | |
| docker images | grep "search_term_here" | |
| # remove image(s) (must remove associated containers first) | |
| docker rmi image_id_here # remove image(s) | |
| docker rmi $(docker images -q) # remove ALL images!!! | |
| docker rmi $(docker images | grep "^<none>" | awk "{print $3}") # remove all <none> images | |
| docker rmi $(docker images | grep "search_term_here") # ie. "2 days ago" | |
| ### BOTH IMAGES AND CONTAINERS ### | |
| docker images && docker ps -a | |
| # stop and remove containers and associated images with common grep search term | |
| docker ps -a --no-trunc | grep "search_term_here" | awk "{print $1}" | xargs -r --no-run-if-empty docker stop && \ | |
| docker ps -a --no-trunc | grep "search_term_here" | awk "{print $1}" | xargs -r --no-run-if-empty docker rm && \ | |
| docker images --no-trunc | grep "search_term_here" | awk "{print $3}" | xargs -r --no-run-if-empty docker rmi | |
| # stops only exited containers and delete only non-tagged images | |
| docker ps --filter 'status=Exited' -a | xargs docker stop docker images --filter "dangling=true" -q | xargs docker rmi | |
| ### NEW IMAGES/CONTAINERS ### | |
| # create new docker container, ie. ubuntu | |
| docker pull ubuntu:latest # 1x pull down image | |
| docker run -i -t ubuntu /bin/bash # drops you into new container as root | |
| ### OTHER ### | |
| # install docker first using directions for installing latest version | |
| # https://docs.docker.com/installation/ubuntulinux/#ubuntu-trusty-1404-lts-64-bit | |
| # other great tips: http://www.centurylinklabs.com/15-quick-docker-tips/ | |
| # fix fig / docker config: https://gist.github.com/RuslanHamidullin/94d95328a7360d843e52 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment