0

On Linux i used to fire this command

docker logs wp-mysql 2>&1 | grep "ready for connections" 

However, I wish to search for the string on Windows

I tried the below two attempts but it shows not just the searched lines but also a few extra lines which I do not need.

docker logs wp-mysql 2>&1 | findstr "ready for connections" 

and

docker logs wp-mysql 2> >(findstr 'ready for connections') 

Can you please suggest a grep alternative for searching string in docker logs on Windows please?

2 Answers 2

1

How findstr works

This command searches for any of the strings "ready", "for" or "connections":

findstr "ready for connections" 

This command searches for the string "ready for connections":

findstr /C:"ready for connections" 

Solution

This command should solve your problems:

docker logs wp-mysql 2>&1 | findstr /C:"ready for connections" 

Alternative solution

Or this one, provided the native windows command find is the first one found in your path, the default install location is in C:\Windows\System32, but some people prefer to put the path to the unix find command before the build-in windows find command:

docker logs wp-mysql 2>&1 | find "ready for connections" 

Info

Use the option /? to show how to use a windows command line utility. Look at the ouput of these two commands:

findstr /? find /? 
Sign up to request clarification or add additional context in comments.

8 Comments

Does not work for me. Doing docker service logs as I have a swarm with multiples replicas. I try to filter logs with docker service logs -t consumer| findstr /C:'consumer.2' but I still get consumer.4 consumer.5 logs etc....
I don't understand your question regarding "but I still get consumer.4 consumer.t logs etc". Also, you should use double quotes not single quotes in the findstr command AND you are using find instead of findstr, and the /C switch works differently between the two. In cmd type help find and help findstr and read about how they work.
Having docker service logs -f consumer | findstr /C: "consumer.1" will give me logs for consumer.2, consumer.3 etc. But I want to only consumer.1. Grep would do just that. The select-string -pattern "consumer.1" does the same, prints logs for all consumers, not just 1. I am unable to find a solution that works exactly as grep, dropping out strings that do not match the given pattern.
@AleksandrMakov You should create your own question for this. You want to just extract a single log from docker, which is not what this question is about.
@AleksandrMakov You can also just get grep for Windows, for example by installing Git on your client, then open a bash shell and do your thing from bash.
|
0

Let me give a practical example of usage.

Here is an example together with Docker logs. I have running a Docker container with container id starting with 42. I want to see why the Docker container fails at start. It tries to connect to Mongoose DB and container fails. I redirect the output and I use select-string to achieve this.

First I run this command :

docker logs 42 2>&1 | select-string -pattern "Error" | select LineNumber,Line 

This then gives me both the LineNumber and the Line, so I can see where in the output I have the pattern with text "Error".

LineNumber Line ---------- ---- 1 connection error: MongooseServerSelectionError: getaddrinfo ENOTFOUND mongodb 31 compatibilityError: null, 42 MongooseServerSelectionError: getaddrinfo ENOTFOUND mongodb 66 error: Error: getaddrinfo ENOTFOUND mongodb 68 name: 'MongoNetworkError' 86 compatibilityError: null, 

You can use this on similar streams such as strings and files that are string based in Powershell.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.