Skip to content

Instantly share code, notes, and snippets.

@aemloviji
Last active November 30, 2022 17:18
Show Gist options
  • Save aemloviji/74aa0683cc85504ff30a1b7384e5e6cc to your computer and use it in GitHub Desktop.
Save aemloviji/74aa0683cc85504ff30a1b7384e5e6cc to your computer and use it in GitHub Desktop.
Helpful Docker commands and code snippets

###############################################################################

Helpful Docker commands and code snippets

###############################################################################

CONTAINERS

#stop ALL containers
docker stop $(docker ps -a -q)
# remove ALL containers
docker rm -f $(docker ps -a -q) 
# can also filter
docker rm -f $(sudo docker ps --before="container_id_here" -q)

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'

# Remove all in one command with --force
docker rm -f $(docker ps -a -q)
# Go to container command line
docker exec -i -t "container_name_here" /bin/bash

to exit above use 'ctrl p', 'ctrl q' (don't exit or it will be in exited state)

# remove all exited containers
docker rm $(docker ps -q -f status=exited) 

Remove all in one command with --force

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

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