Search code examples
windowsdockerlogginggrepfindstr

search for a string in docker logs in Windows


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?


Solution

  • 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 /?