How to use docker search command to find images

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker search command to find container images on Docker Hub. You will begin by performing basic searches for images by name.

Building upon the basic search, you will then explore options to display the full, non-truncated descriptions of search results. Furthermore, you will learn how to refine your searches by filtering results based on criteria such as the number of stars and official status. Finally, you will discover how to limit the number of results displayed and format the search output using a Go template for customized viewing.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ImageOperationsGroup -.-> docker/search("Search Images in Repository") subgraph Lab Skills docker/search -.-> lab-555219{{"How to use docker search command to find images"}} end

In this step, you will learn how to search for Docker images on Docker Hub using the docker search command. Docker Hub is a cloud-based registry service provided by Docker that allows you to find and share container images.

The basic syntax for the docker search command is:

docker search [OPTIONS] TERM

Where TERM is the search term you want to use to find images.

Let's start by searching for images related to "ubuntu". Open your terminal in the LabEx environment and run the following command:

docker search ubuntu

You should see a list of images related to Ubuntu. The output typically includes the image name, description, stars (number of likes), official status, and automated build status.

The output might look something like this (the exact output may vary):

NAME                                                   DESCRIPTION                                       STARS     OFFICIAL   AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys...   15000     [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for th...   50        [OK]
ubuntu/apache2                                         Apache2 web server                                60                   [OK]
ubuntu/nginx                                           Nginx web server                                  50                   [OK]
...

This command searches Docker Hub for images whose names or descriptions match the term "ubuntu". The results are displayed in a table format.

In the previous step, you searched for Docker images and saw that the description column might be truncated, meaning the full description is not displayed. To see the complete description, you can use the --no-trunc option with the docker search command.

Let's search for "ubuntu" again, but this time, we will use the --no-trunc flag to display the full description.

docker search --no-trunc ubuntu

Observe the output. You should now see the complete description for each image, without any truncation. This is helpful when you need to read the full details about an image before deciding to use it.

The --no-trunc option is useful for getting the complete information in the output of various Docker commands, not just docker search.

In this step, you will learn how to filter the search results based on criteria like the number of stars and whether the image is official. This helps you find more relevant and trustworthy images.

You can use the --filter option to apply filters to your search. The --filter option takes a key=value pair.

To filter by the number of stars, use the stars key followed by the minimum number of stars you want. For example, to find Ubuntu images with at least 1000 stars, run:

docker search --filter stars=1000 ubuntu

Observe the output. You should now only see Ubuntu images that have 1000 or more stars.

You can also filter for official images using the is-official key with a value of true. Official images are maintained by Docker or the original software vendors and are generally considered more reliable.

To find official Ubuntu images, use the following command:

docker search --filter is-official=true ubuntu

The output will show only the official Ubuntu images.

You can combine multiple filters by using the --filter option multiple times. For example, to find official Ubuntu images with at least 1000 stars, you would use:

docker search --filter is-official=true --filter stars=1000 ubuntu

This command will narrow down the results to only show official Ubuntu images that have received at least 1000 stars.

In this step, you will learn how to limit the number of search results displayed by the docker search command. This is useful when you are searching for a broad term and want to see only the top results.

You can use the --limit option to specify the maximum number of results to display. The --limit option takes an integer value.

For example, to search for "ubuntu" and limit the results to the top 5, run the following command:

docker search --limit 5 ubuntu

Observe the output. You should now see a maximum of 5 results for your search query.

You can combine the --limit option with other options like --filter and --no-trunc. For instance, to find the top 3 official Ubuntu images with at least 1000 stars and display their full descriptions, you would use:

docker search --filter is-official=true --filter stars=1000 --limit 3 --no-trunc ubuntu

This command demonstrates how you can combine different options to refine your search and control the output.

In this step, you will learn how to format the output of the docker search command using a Go template. This allows you to customize the displayed information and its presentation.

The --format option allows you to specify a Go template to format the output. Go templates are powerful and flexible for controlling output.

Let's format the output to display only the image name and the number of stars, separated by a tab. The template will look like this: {{.Name}}\t{{.Stars}}.

Run the following command to search for "ubuntu" and format the output:

docker search --format "{{.Name}}\t{{.Stars}}" ubuntu

You should see a list where each line contains the image name followed by a tab and the number of stars.

Let's try a slightly more complex template to display the name, official status, and description, each on a new line. The template will be Name: {{.Name}}\nOfficial: {{.IsOfficial}}\nDescription: {{.Description}}\n.

docker search --format "Name: {{.Name}}\nOfficial: {{.IsOfficial}}\nDescription: {{.Description}}\n" --limit 3 ubuntu

This command will display the name, official status, and description for the top 3 Ubuntu images, with each piece of information on a separate line. We also included the --limit 3 option to keep the output concise.

Using Go templates provides a powerful way to extract and present the specific information you need from the docker search results.

Summary

In this lab, you learned how to use the docker search command to find Docker images on Docker Hub. You started by performing a basic search for images by name, observing the default output format including name, description, stars, official status, and automated build status.

You then explored how to display the full, non-truncated description of search results using the --no-trunc option. Furthermore, you learned to refine your searches by filtering results based on the number of stars and official status, and how to limit the number of results displayed. Finally, you discovered how to customize the output format of the search results using a Go template.