Skip to content

Instantly share code, notes, and snippets.

@je2ryw
Forked from garystafford/helpful-docker-commands.sh
Created February 23, 2020 22:52
Show Gist options
  • Save je2ryw/5e10906960c697d55343f602d7a3b5d0 to your computer and use it in GitHub Desktop.
Save je2ryw/5e10906960c697d55343f602d7a3b5d0 to your computer and use it in GitHub Desktop.
My list of helpful docker commands
###############################################################################
# Helpful Docker commands and code snippets
###############################################################################
# install docker first using directions for installing latest version
# https://docs.docker.com/installation/ubuntulinux/#ubuntu-trusty-1404-lts-64-bit
# 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
# list images and containers
docker images # all images
docker ps -a # all containers
docker images | grep <search_term>
# start and attach to container
docker start <container>
docker attach <container>
# stop and remove container
docker stop <container>
docker rm <container>
#stop and remove ALL containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rm $(sudo docker ps --before="<container>" -q) # can also filter
# Remove all in one command with --force
# helps with error: 'unexpected end of JSON input'
docker rm -f $(docker ps -a -q)
# remove image(s) (must remove associated containers first)
docker rmi <image>
docker rmi $(docker images | grep <container>)
docker rmi $(docker images | grep "<search_term>") # ie. "2 days ago"
# remove all <none> images
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
# remove ALL images!!!
docker rmi $(docker images -q)
# to only stop 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
# stop and remove containers and associated images with common grep search term
docker ps -a --no-trunc | grep "<search_term>" | awk "{print $1}" | xargs -r --no-run-if-empty docker stop && \
docker ps -a --no-trunc | grep "<search_term>" | awk "{print $1}" | xargs -r --no-run-if-empty docker rm && \
docker images --no-trunc | grep "<search_term>" | awk "{print $3}" | xargs -r --no-run-if-empty docker rmi
# 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