2

I am running multiple docker containers on a remote server which are tagged by my username. The containers run some code with printed outputs. I want to do a string search for string "xyz" using grep on the output of all the containers that belong to me(filter by username). Below is my attempt doing this which doesn't seem to work:

docker ps -a -q --filter name=${USER} --format="{{.ID}}" | xargs -d ' ' docker logs | grep xyz 

BONUS: I want a python script that does the above for me on all the servers I am running my containers on, from my macbook, something like this:

 os.system('ssh {}@{} \"docker ps -a -q --filter name={} --format={{{{.ID}}}} | xargs -d ' ' docker logs | grep xyz\"'.format(usern[server], server, usern[server])) 

Where "server" is the server name and "usern" is a dict storing my username for that server. My ssh keys are stored.

Obviously I am struggling to make this work too.

2
  • What does docker ps -a -q --filter name=${USER} --format="{{.ID}}" output? Then, what does docker ps -a -q --filter name=${USER} --format="{{.ID}}" | xargs -d ' ' docker logs output? Commented Jun 19, 2019 at 17:00
  • @JacobIRR Tried this already, first one works fine and lists all the container ID for me. Second one throws "Error: No such container:<container ID of 1st listed container>". Commented Jun 19, 2019 at 17:05

1 Answer 1

1
docker ps -a -q --filter name=${USER} | while read c; do docker logs $c; done | grep xyz 

You should also note that -q and --format="{{.ID}}" are synonyms so only one of them is enough.

Sign up to request clarification or add additional context in comments.

2 Comments

Any idea why xargs version fails?
I can't give you all the details but in my experience xargs can't handle multi-line inputs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.