How to use `jq` with `docker inspect`?

0398

Using jq with docker inspect allows you to filter and format the JSON output for better readability and to extract specific information. Here’s how to do it:

Steps to Use jq with docker inspect

  1. Install jq: Ensure that jq is installed on your system. You can install it using your package manager. For example:

    • On Ubuntu:
      sudo apt install jq
      
    • On macOS (using Homebrew):
      brew install jq
      
  2. Run docker inspect with jq: Use the docker inspect command and pipe the output to jq. Here are some examples:

    • Get All Information:

      docker inspect my-image | jq
      

      This will pretty-print the entire JSON output for the image named my-image.

    • Extract Specific Fields: You can specify which fields you want to extract. For example, to get the image ID and the layers:

      docker inspect my-image | jq '.[].Id, .[].RootFS.Layers'
      
    • Get the Image Name:

      docker inspect my-image | jq '.[].RepoTags'
      
    • Get the Creation Date:

      docker inspect my-image | jq '.[].Created'
      

Example

If you want to check the SHA numbers of the layers for an image named custom-nginx, you can run:

docker inspect custom-nginx | jq '.[].RootFS.Layers'

Summary

Using jq with docker inspect enhances your ability to parse and manipulate the JSON output, making it easier to extract relevant information. This combination is powerful for managing Docker images and containers effectively.

If you have more questions or need further assistance, feel free to ask!

0 Comments

no data
Be the first to share your comment!