3

I'm looking to use tools along the lines of:

  • grep
  • sed
  • AWK

to work with Docker.

Listing containers:

docker container ls | awk '{print $1}'

Results:

CONTAINER
490e3d669259
a44230a617e1

How can I omit the "header"?

Here's the full output for docker container ls should that prove useful:

CONTAINER ID  IMAGE  COMMAND                 CREATED        STATUS        PORTS                                        NAMES
490e3d669259  jetty  "/docker-entrypoint.…"  3 minutes ago  Up 3 minutes  0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp  quirky_antonelli
a44230a617e1  jetty  "/docker-entrypoint.…"  4 minutes ago  Up 4 minutes  8080/tcp                                     goofy_hamilton

I am just looking for the values under the container heading.

  • 2
    You know that the -q flag gives output well suited for further processing? – Thorbjørn Ravn Andersen Aug 4 at 18:22
  • yes @ThorbjørnRavnAndersen but it's also to try and learn a bit of awk usage – Nicholas Saunders Aug 5 at 5:22
25

With awk, you can skip the first line (or record) using NR > 1:

docker container ls | awk 'NR > 1 {print $1}'

But you don't need that. You can just tell docker to format the output:

% docker container ls --format '{{.ID}}'
bd1299b0dd5a
8fcbcd67a843
| improve this answer | |
  • 1
    This is a good answer since format is really powerful for more complicated cases, but it's worth knowing that for simple cases like thisdocker container ls -q will do exactly what you want. – jdf Aug 5 at 22:14
3

The command you're looking for is tail -n +2:

docker container ls | awk '{print $1}' | tail -n +2

Explanation here (there is also an option using sed - guess you have to try which is fastest).

| improve this answer | |
1

Docker gives you more control so that you don't actually need to use a second command piped to do post-processing. In this instance you can just use the --quiet|-q option.

$ docker container ls -q
490e3d669259
a44230a617e1

Many command line tools use similar conventions like --help that give more detailed information about usage options. Here is the output of docker container ls --help:

# docker container ls --help

Usage:  docker container ls [OPTIONS]

List containers

Aliases:
  ls, ps, list

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.