Delete all docker instances at once - bash

I'm new to dockers and other concepts. I've lots of unused dockers running and I would want to remove all of them. Now I'm manually killing it. Can we do it at once?

docker rm $(docker ps -a -q)
This command deletes all stopped containers. The command docker ps -a -q above returns all existing container IDs and passes them to the docker rm command which deletes them. Running containers are not deleted.
OR, try this one.
docker system prune -a
Remove all unused images not just dangling ones. also it will remove all build cache

As #Emon mentioned you can either go for docker prune. But keep in mind that pruning the system will remove any stopped containers and all unused images.
Since you've tagged shell, I'm adding this shell script also.
echo -e "******* Start *******"
echo -e "\n=====> Stopping & Cleaning up UNUSED containers.."
for i in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`; do docker stop $i ; docker rm $i ; done
echo -e "[CLEARED]\n"
docker ps -a
echo -e "\n=====> Removing UNUSED container images.."
for i in `docker images | grep none | awk '{print $3}'`; do docker rmi $i ; done
echo -e "[CLEARED]\n"
docker images
This script can be changed accordingly to your need like if you want to remove only containers or even images also.
This can further also be done easily by
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
If you want to remove only the exitted containers alone, you can use it
`docker rm $(docker ps -a -f status=exited -q)`

Related

Grep on docker image names in package.json

I want to set a script in the package.json that starts the container based on image-name that I grep after piping the docker images command, something like this:
"docker:run": "docker run -it -v /$(pwd):/app $(docker images | grep 'online-check-in-frontend' | awk '{ print $1 }') sh",
also tried :
"docker:run": "docker run -it -v /$(pwd):/app $(docker images | grep \"online-check-in-frontend\" | awk \"{ print $1 }\") sh",
The commands above is not working, i get the following error:
Im using windows, and bash. :S
Br

Shell : How to get a container's name containing some string

I have a list of containers where names are like following :
container 1: myApp_ihm.dfgdfgdfgdfvdfdfbvdfvdfv
container 2: myApp_back.uirthjhiliszfhjuioomlui
...
container 3: myApp_database.piyrjfhjyukyujfkgft
I have to execute some string on the container where the name contains ihm (the first one in my example)
In order to exec my commands , I'm used to do:
docker exec -it ihm bash
so ihm should by replaced by some test to get the first one name :
myApp_ihm.dfgdfgdfgdfvdfdfbvdfvdfv
Suggestions?
docker exec -it $(docker ps | grep myApp_ihm | awk '{print $1}') /bin/bash
docker exec -it $(docker ps --format "{{.Names}}" | grep "ihm") bash
This worked for me, added that to a bash script and saved myself 30-60 seconds of typing/copy-pasting every time I want to go into my container.
docker exec -it $(docker ps --format "{{.ID}} {{.Command}}" | grep /home/app/ | awk '{print $1}') /bin/bash

alias zsh: parse error near `do'

I'm facing an issue with an alias for zsh.
I've this alias in my .zprofile file: alias cleanimg="for i in `docker images | grep '<none>' | awk '{print $3}'`; do; docker rmi -f $i; done".
When running cleanimg from my terminal I'm getting this error: zsh: parse error near `do'.
I tried to remove the ; right after the do keyword but it didn't fix the issue.
However, that alias runs correctly if I execute the code directly from a terminal.
Can anybody help me with this syntax error?
Thanks by advance.
Don't try to use an alias for this; use a function.
cleanimg () {
for i in $(docker images | awk '/<none>/ {print $3}'); do
docker rmi -f "$i"
done
}
This saves you from having to get the quoting right so that the command substitution runs when the alias is used, rather than when it is defined.
(Also, grep | awk pipelines can almost always be implemented using awk alone.)
(I also wonder if you can dispense with awk and the loop using the --filters option instead; maybe docker rmi -f $(docker images -q -f "dangling=true")?)
If all you want is to clean your docker images, you can set an alias like this:
alias cleanimg='docker rmi -f $(docker images -q -f "dangling=true")'
The sub commande docker images -q -f "dangling=true" return all your id to remove and since docker rmi accept arguments you can pass them directly
You have an extra semicolon in your for loop, just fix it to -
alias cleanimg="for i indocker images | grep '' | awk '{print $3}'; do docker rmi -f $i; done"

Is there a flag to silence docker when an empty list is provided

Such a feature is useful when running multiple docker commands in one that follow this pattern:
docker do_smth $(docker query_smth)
For example:
docker stop $(docker ps -q)
or
docker rm $(docker ps -a -q)
or
docker network rm $(docker inspect ... --format ...)
If the inner docker command returns an empty list, the outer command will fail because and will display the help.
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags]
Stop one or more running containers
Is there a way to silence docker or make docker not complain on empty lists? Something like: "Kill everybody. If there is no one, job done."
This would be similar to mkdir -p exiting_directory vs mkdir exiting_directory where the former will not complain if the directories exist.
For scripting where the result may be empty, I prefer to use xargs --no-run-if-empty:
docker ps -aq | xargs --no-run-if-empty docker rm

Shell script to cleanup specified docker containers using grep

I want to write a script that is executed by my development build server that will remove any 'similar' docker containers before building and running a new container.
Below is pseudo code for the bash script I need
var name = $1
var number_of_results = # of containers returned from $(docker ps -a | grep "$name")
if(number_of_result > 0)
docker rm -f $(docker ps -a | grep "$name")
You can just use this script in shell:
name="${1?one argument needed}"
ids=$(docker ps -a | awk -v name="$name" '$NF ~ name{print $1}')
[[ -n $ids ]] && docker rm -f $ids

Resources