Search code examples
linuxdockerubuntupipeline

Command `docker` can not work with pipeline. Do not work with `|`, `>`, `>>`


I want to search some text output by command docker, but it seems not work with pipeline.

My expected result of docker > a should write all output in the terminal to file a. And expected result of docker | grep info should print something that contains info Display system-wide information.

I added myself to the group docker. The following is test cases.

lala@ubu:~/projects/docker 14:19:42
$ grep docker /etc/group
docker:x:999:lala
lala@ubu:~/projects/docker 14:19:47
$ ls -al 
总用量 8
drwxr-xr-x  2 lala lala 4096 1月  10 14:15 .
drwxr-xr-x 10 lala lala 4096 1月  10 10:21 ..
lala@ubu:~/projects/docker 14:19:57
$ ls -al | grep x
drwxr-xr-x  2 lala lala 4096 1月  10 14:15 .
drwxr-xr-x 10 lala lala 4096 1月  10 10:21 ..
lala@ubu:~/projects/docker 14:20:01
$ docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:

...... unnecessary output

Run 'docker COMMAND --help' for more information on a command.
lala@ubu:~/projects/docker 14:20:03
$ docker | grep x

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:

...... the same as above. unnecessary output

Run 'docker COMMAND --help' for more information on a command.
lala@ubu:~/projects/docker 14:20:15
$ docker > a

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:

...... the same as above again. unnecessary output

Run 'docker COMMAND --help' for more information on a command.
lala@ubu:~/projects/docker 14:21:26
$ cat a
lala@ubu:~/projects/docker 14:21:28
$ file a
a: empty
lala@ubu:~/projects/docker 14:21:30
$ 

docker >> a did not work, either.


Solution

  • that is because , docker command need some arguments that is it returing errors, you can use this:

    docker 2>&1| grep info
    

    output:

     -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      -v, --version            Print version information and quit
      info        Display system-wide information
      inspect     Return low-level information on Docker objects
      version     Show the Docker version information
    Run 'docker COMMAND --help' for more information on a command.
    

    anyway the correct way is just to type:

    docker info --help
    

    output:

    Usage:  docker info [OPTIONS]
    
    Display system-wide information
    
    Options:
      -f, --format string   Format the output using the given Go template