2.3: Manipulating images at the terminal

Contents

DockerNow that we know where the images are stored, it’s time to learn how to manage them from the terminal.

Docker It provides several simple commands for managing images: searching for those available in a registry, downloading them to the machine, displaying those stored locally, and deleting those that are no longer needed.

In this lesson, we’ll explore four essential commands:

  • docker search : search for an image
  • docker pull : download an image
  • docker images : view locally stored images
  • docker rmi : delete an image

docker searchBefore pulling an image, it may be helpful to check if it exists in the registry and which variants are available. To do this,Docker provides the command.

Example:

docker search nginx

Docker then queries the registry (DockerHub by default) and displays a list of images matching the search.

Example result:

NAME               DESCRIPTION                     STARS
nginx              Official build of Nginx         20000
nginx/unit         NGINX Unit                      1200
bitnami/nginx      Bitnami Nginx container         300

This table allows you to quickly identify available images and get an idea of their popularity thanks to the STARS column.

Useful parameters

You can limit the number of results returned:

docker search nginx --limit 5

You can also filter the results, for example to display only images with a certain number of stars:

docker search nginx --filter stars=100

These filters can help you quickly identify the most popular or best-maintained images.

Downloading an image: docker pull

docker pullOnce the image has been identified, it must be downloaded to the machine. This is the role of the command.

Example:

docker pull nginx

Docker will then contact the registry, download the image’s various layers, and store them locally.

During the download, you can see the different layers being retrieved.

Example:

Pulling from library/nginx
a603fa5e3b41: Pull complete
c39e8cda9f4a: Pull complete
Digest: sha256:xxxxxxxx
Status: Downloaded newer image for nginx:latest

Useful parameters

In many cases, it is best to specify the exact version of the image to download. This version is called a tag.

docker pull nginx:1.27

**latest**If no tag is specified,Docker will automatically use , which generally corresponds to the latest published version.

It is also possible to specify the target architecture, for example when working with different platforms:

docker pull --platform linux/amd64 nginx

List local images: docker images

After downloading several images, it may be useful to see what is present on the machine. The commanddocker images displays the list of local images.

docker images

Example output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    6efc10a0510f   2 weeks ago    187MB
mariadb      11        b3a1e2c4e5f6   3 weeks ago    410MB
hello-world  latest    feb5d9fea6a5   6 months ago   13kB

Each line corresponds to a locally stored image.

Column Description
REPOSITORY Image name
TAG Image Version
IMAGE ID Unique ID
CREATED Creation date
SIZE Image size

Useful settings

You can filter the display to see only certain images.

For example:

docker images nginx

This command displays only images related to nginx.

You can also display only the image IDs:

docker images -q

This option is often used in scripts.

Finally, the option--allallows you to display all images present, including those no longer directly used by a container.

docker images --all

This view is particularly useful for identifying orphaned or unused images that are still taking up disk space.

Deleting an image: docker rmi

docker rmiOver time, some images become unnecessary and can take up disk space.Docker allows you to delete them using the command.

Example:

docker rmi nginx

It is also possible to delete an image using its ID:

docker rmi 6efc10a0510f

Useful settings

If the image is being used by a container,Docker will refuse to delete it. In some situations, it may be necessary to force the deletion:

docker rmi -f nginx

It is also possible to delete multiple images with a single command:

docker rmi nginx mariadb redis

When a large number of unused images accumulate,Docker also offers a global cleanup command that we’ll cover later:

docker image prune

Key takeaways

DockerThese four commands form the basis for managing images.

Command Purpose
docker search Search for an image in a registry
docker pull Download an image
docker images display local images
docker rmi delete an image

In practice, you will often**docker pull** use to retrieve images and**docker images** to check what is available on your machine.