Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My list of helpful docker commands
###############################################################################
# Helpful Docker commands and code snippets
###############################################################################
### CONTAINERS ###
docker stop $(docker ps -a -q) #stop ALL containers
docker rm -f $(docker ps -a -q) # remove ALL containers
docker rm -f $(sudo docker ps --before="container_id_here" -q) # can also filter
# exec into container
docker exec -it $(docker container ls | grep '<seach_term>' | awk '{print $1}') sh
# exec into container on windows with Git Bash
winpty docker exec -it $(docker container ls | grep '<seach_term>' | awk '{print $1}') sh
# 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)
docker rm $(docker ps -q -f status=exited) # remove all exited containers
### IMAGES ###
# list images and containers
docker images | grep "search_term_here"
# remove image(s) (must remove associated containers first)
docker rmi -f image_id_here # remove image(s)
docker rmi -f $(docker images -q) # remove ALL images!!!
docker rmi -f $(docker images | grep "^<none>" | awk '{print $3}') # remove all <none> images
docker rmi -f $(docker images | grep 'search_term_here' | awk '{print $1}') # i.e. 2 days ago
docker rmi -f $(docker images | grep 'search_1\|search_2' | awk '{print $1}')
### DELETE 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
### DELETE NETWORKS AND VOLUMES ###
# clean up orphaned volumes
docker volume rm $(docker volume ls -qf dangling=true)
# clean up orphaned networks
docker network rm $(docker network ls -q)
### 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
@ssoliveirasp

This comment has been minimized.

Copy link

@ssoliveirasp ssoliveirasp commented Nov 28, 2019

awesome

@harshaisgud

This comment has been minimized.

Copy link

@harshaisgud harshaisgud commented Apr 10, 2020

Just what I was looking for . Thank You

@marrionluaka

This comment has been minimized.

Copy link

@marrionluaka marrionluaka commented May 19, 2020

This is great, Thank you! May I add this one as well:

# Removes all unused volumes that contain numeric values (i.e: bbd0c58ae90b5a63615388543180cc703d6aaf96d268068833e4ecb4ac1e3d5d).
# It will not remove the volumes that are currently in use
docker volume rm $(docker volume ls | grep "[0-9]")
@Sachinkumar706

This comment has been minimized.

Copy link

@Sachinkumar706 Sachinkumar706 commented Jul 3, 2020

I have list of containers with different container name and now I want to first grep all the containers with similar container name and stop all containers running since last 3 days. Can anyone please help me with command?
I tried:

docker ps -a | grep -i "ngnix" | xargs docker container prune --force --filter "until=3days"

nginx - say keyword in the name of containers

@jimoconnell

This comment has been minimized.

Copy link

@jimoconnell jimoconnell commented May 26, 2021

The awk filters on lines 36, 37 & 39 did not work on my Ubuntu 20.04 host, (bash and zsh tested,) as they use double quotes. Swapping them for single quotes worked for me:

# 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
@deepfriedmind

This comment has been minimized.

Copy link

@deepfriedmind deepfriedmind commented Jun 1, 2021

What is the usecase for:

docker exec -it $(docker container ls  | grep '<seach_term>' | awk '{print $1}') sh

Compared to:

docker exec -it <container_name> sh

?

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