I can find a docker container by name: docker ps --filter='name=mvn_repo'
. Is there a way (without resorting to bash/awk/grep etc.) to negate this filter and list all containers except the one with the given name?
You can use docker inspect
to do this, I created a container with --name=test111, it appears as /test111, so if I do
docker inspect -f '{{if ne "test111" .Name }}{{.Name}} {{ end }}' $(docker ps -q)
/test111
/sezs
/jolly_galileo
/distracted_mestorf
/cranky_nobel
/goofy_turing
/modest_brown
/serene_wright
/fervent_lalande
but if I do a filter with the /, so it becames
docker inspect -f '{{if ne "/test111" .Name }}{{.Name}} {{ end }}' $(docker ps -q)
/sezs
/jolly_galileo
/distracted_mestorf
/cranky_nobel
/goofy_turing
/modest_brown
/serene_wright
/fervent_lalande
I do not get it.
Kudos to Adrian Mouat for his reference post on docker inspect
http://container-solutions.com/docker-inspect-template-magic/
And, as he says
"(Unfortunately Docker prints a new-line for each container, whether it matches the if or not)." If I put a blank line the formatting is lost.
I came here trying to figure out a similar problem... found part of the solution in Mykola Gurov's comment, which I modifed to get to this:
docker ps -aq | grep -v -E $(docker ps -aq --filter='name=mvn_repo' | paste -sd "|" -)
Mykola's solution only works if there is one match with the docker ps filter. So if you've got a whole bunch of containers you want to exclude that match a filter pattern - it will fail because grep
can only work on a single expression.
The solution I've provided converts the output of that into a regular expression and then uses grep with extended regexp.
To break it down...
docker ps -aq --filter='name=mvn_repo' | paste -sd "|" -
produces output like this:
d5b377495a58|2af19e0029a4
where this is the result of each of the container id's joined with a |
character to formulate a regular expression that can then be used with grep.
Then when used in a sub-shell like this with grep:
grep -v -E $(docker ps -aq --filter='name=mvn_repo' | paste -sd "|" -)
resolves to something like this:
grep -v -E d5b377495a58|2af19e0029a4
then when we can then pipe the the output of docker ps -aq
to get something like this:
docker ps -aq | grep -v -E d5b377495a58|2af19e0029a4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With