Last active
April 20, 2025 11:08
-
Star
(225)
You must be signed in to star a gist -
Fork
(123)
You must be signed in to fork a gist
-
-
Save garystafford/f0bd5f696399d4d7df0f 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 to create, start, and stop containers | |
| ############################################################################### | |
| # 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) | |
| # other great tips: http://www.centurylinklabs.com/15-quick-docker-tips/ | |
| # fix fig / docker config: https://gist.github.com/RuslanHamidullin/94d95328a7360d843e52 |
Just what I was looking for . Thank You
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]")
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
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 rmiWhat is the usecase for:
docker exec -it $(docker container ls | grep '<seach_term>' | awk '{print $1}') shCompared to:
docker exec -it <container_name> sh?
Thank you for this this would be very helpful!
very helpful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome