How to search for containers that do not match the results of docker ps -filter?

I can find the docker ps --filter='name=mvn_repo' container by name: docker ps --filter='name=mvn_repo' . Is there a way (without resorting to bash / awk / grep, etc.) to cancel this filter and list all containers except one with the specified name?

+10
source share
3 answers

You can use docker inspect for this, I created a container with --name = test111, it displays as / test 111, 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 make a filter with /, then it became

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 understand.

Kudos to Adrian Muatu for his reference post at the docker examination

http://container-solutions.com/docker-inspect-template-magic/

And as he says

"(Unfortunately, Docker prints a new line for each container, regardless of whether it matches if or not.) If I put an empty string, formatting will be lost.

+7
source

Is there any way (without resorting to bash / awk / grep, etc.) Cancel this filter and list all containers except the container with the given name?

The Docker command-line reference does not mention a way to do this. Therefore, I came to the conclusion that filter negation is not currently supported by the Docker command-line interface.

+2
source

I came here trying to find a similar problem ... found part of the solution in a comment by Nikolai Gurov, which I modified to understand:

 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 for the docker ps filter. Therefore, if you have a whole bunch of containers, you want to exclude this match from the filter pattern - it will fail, because grep can only work with one expression.

The solution I provided converts the output of this to a regular expression and then uses grep with an extended regular expression.

To break it ...

 docker ps -aq --filter='name=mvn_repo' | paste -sd "|" - 

produces a conclusion as follows:

 d5b377495a58|2af19e0029a4 

where is the result of each container identifier associated with the symbol | , to formulate a regular expression that can then be used with grep.

Then, when it is used in a subdomain using grep:

 grep -v -E $(docker ps -aq --filter='name=mvn_repo' | paste -sd "|" -) 

resolves something like this:

 grep -v -E d5b377495a58|2af19e0029a4 

then when we can then output the output of docker ps -aq to get something like this:

 docker ps -aq | grep -v -E d5b377495a58|2af19e0029a4 
+2
source

All Articles