How to handle Docker images

What is a Docker image ?

Docker images form the bedrock of containerized applications, providing a streamlined solution for seamless deployment across diverse environments. Functioning as a snapshot of a complete filesystem, a Docker image encompasses every vital aspect needed for an application to operate independently and efficiently. It encapsulates the application code, runtime, libraries, system tools, and configurations. This holistic approach empowers Docker images to facilitate consistent and portable application deployment.

How to check the available Docker Images on the host machine

“docker images” command lists all locally available Docker images.

discoveringsystems@Ubuntu:~$ sudo docker images
REPOSITORY                      TAG               IMAGE ID       CREATED         SIZE
nginx                           latest            d453dd892d93   2 months ago    187MB
ubuntu                          latest            6b7dfa7e8fdb   13 months ago   77.8MB

How to download  or pull images from the docker hub registry 

To pull or download the docker images from the docker hub , we can use the “docker pull”  command

discoveringsystems@Ubuntu:~$ sudo docker pull ubuntu:20.04

20.04: Pulling from library/ubuntu
527f5363b98e: Pull complete
Digest: sha256:f2034e7195f61334e6caff6ecf2e965f92d11e888309065da85ff50c617732b8
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04

In this example:

docker pull -  initiates the process of pulling an image from a registry.
ubuntu - is the name of the Docker image.
20.04 -  is the tag specifying the version or variant of the image.

You can replace “ubuntu” with the name of the image you want to pull or  adjust the tag according to the specific version you want . 

discoveringsystems@Ubuntu:~$ sudo docker images
REPOSITORY                      TAG               IMAGE ID       CREATED         SIZE
ubuntu                          20.04             f78909c2b360   3 weeks ago     72.8MB
nginx                           latest            d453dd892d93   2 months ago    187MB
ubuntu                          latest            6b7dfa7e8fdb   13 months ago   77.8MB

How to list all available tags of a specific docker image locally

You can use the  “docker images” command to see the available tags for that image locally 

discoveringsystems@Ubuntu:~$ sudo docker images ubuntu
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ubuntu       20.04     f78909c2b360   3 weeks ago     72.8MB
ubuntu       latest    6b7dfa7e8fdb   13 months ago   77.8MB

Inorder to search for available tags of a Docker image on Docker Hub, you can use the curl command with the Docker Hub API. Please note that “jq” needs to be installed to filter the json response. 

discoveringsystems@Ubuntu:~$  curl -s https://registry.hub.docker.com/v2/repositories/library/ubuntu/tags | jq -r '.results[].name'
latest
noble-20231221
noble
.
.
.

Here’s a breakdown of the command:

curl -s: Fetches the data silently (without progress or error messages).
https://registry.hub.docker.com/v2/repositories/library/<image_name>/tags: Fetches information about the image and its tags from the Docker Hub API.

jq -r '.results[].name': Uses jq to extract and display the names of all available tags.

Adjust the <image_name> placeholder with the actual name of the Docker image you want to explore.

Note: The Docker Hub API may show the tags with names instead of the tags as version number. So in case you were not able to find the version number in the “jq” filtered data , please check on the official docker hub page of the image for all supported tags part of an image . In our case we used ubuntu , so you can check https://hub.docker.com/_/ubuntu

How to search for available image names part of the docker hub registry 

You can use the docker search command with the image name  to get the list of images with that name. We can also –format “{{.Name}}:{{.StarCount}}” to format it better 


discoveringsystems@Ubuntu:~$ sudo docker search  ubuntu
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   16753     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   296       [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   115       [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   105       [OK]
.
.
discoveringsystems@Ubuntu:~$ sudo docker search ubuntu --format "{{.Name}}:{{.StarCount}}"
ubuntu:16753
websphere-liberty:296
ubuntu-upstart:115
neurodebian:105
ubuntu/nginx:104
ubuntu/squid:74
ubuntu/apache2:70
.
.
.

How to save docker images to the tar archive 

The docker save command is used to save one or more Docker images to a tarball archive file. This can be useful when you want to share Docker images with someone who doesn’t have direct access to a Docker registry or when you need to transfer images between different environments

discoveringsystems@Ubuntu:~$ sudo docker save ubuntu:20.04 -o saved-ubuntu-20.04.tar
discoveringsystems@Ubuntu:~$ ls | grep -i saved
saved-ubuntu-20.04.tar

How to load the tar archive images to docker host

After saving the image, you can transfer the tarball to another system and use the docker load command to load the image back into Docker.

discoveringsystems@Ubuntu:~$ sudo docker load -i saved-ubuntu-20.04.tar
3a03f09d2129: Loading layer [==================================================>]  75.18MB/75.18MB
Loaded image: ubuntu:20.04

discoveringsystems@Ubuntu:~$ sudo docker images
REPOSITORY                      TAG               IMAGE ID       CREATED         SIZE
ubuntu                          20.04             f78909c2b360   3 weeks ago     72.8MB
nginx                           latest            d453dd892d93   2 months ago    187MB
ubuntu                          latest            6b7dfa7e8fdb   13 months ago   77.8MB

How to remove  the docker images

We can use the docker rmi command to remove the images from the local docker host image collections 

discoveringsystems@Ubuntu:~$ sudo docker rmi ubuntu:20.04
Untagged: ubuntu:20.04
Deleted: sha256:f78909c2b360d866b3220655c0b079838258b8891a12ac25fc670f0cbb54229f
Deleted: sha256:3a03f09d212915b240e9d216069aba5652ed4765c7e4b098c65e71860d47b8e1

Conclusion

In this article we have explored the commands that should provide a solid starting point for working with Docker images . As you become more comfortable, you can explore additional Docker features and commands for more advanced usage.

Leave a Comment

Your email address will not be published. Required fields are marked *