Last active
May 18, 2020 11:11
-
-
Save transcranial/be0f8f0d80e464f4032e to your computer and use it in GitHub Desktop.
Remove old unused docker containers and images
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
#!/usr/bin/env bash | |
# remove old unused docker containers | |
docker rm $(docker ps -a | grep -v latest | grep -v CONTAINER | awk '{print $1}') | |
# remove old unused docker images | |
docker rmi $(docker images -a | grep '<none>' | awk '{print $3}') | |
# Delete all stopped containers | |
docker rm $(docker ps -a -q) | |
# Remove unused, dangling images | |
docker rmi $(docker images -q -f dangling=true) | |
# Remove all but X images with a specific name (old image cleanup), | |
# in this case, we leave the 2 newest images. | |
$(docker images | grep my-docker-image | tail -n+3 | awk '{ print $1":"$2; }') | |
# Specific commands for Elastic Beanstalk | |
EB_CURRENT_CONTAINER=$(docker ps | grep current-app | awk '{ print $1}') | |
# Show logs for the current running deployment | |
docker logs $EB_CURRENT_CONTAINER | |
# Jump into the current running container. This gives you an | |
# interactive shell within the same container as the currently | |
# running 'current-app'. The command will fail if no container | |
# with that name is currently running. | |
docker exec -it $EB_CURRENT_CONTAINER /bin/bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment