DevOpsDocker

Docker Images - List of commands with example

After installation of Docker, it's time to move further and understand about Docker Hub & Images. We will explain Docker Hub first and then will proceed with Docker Images with some examples for better understanding purpose.

What are Docker Images & Docker Hub?

The Docker images are the set of templates The Docker images are stored at Docker Hub. The Docker Hub is already integrated in the Docker setup at the time of installation and can not be removed or replaced. So whenever we have to install an image on our machine, the local machine will connect with Docker Hub to find the available images. Please refer to this official link for Docker Hub quick start guide.

Suppose if we want to install Ubuntu image then we can go the Docker Hub website and search with the ubuntu keyword. The search will display all the matching results for Ubuntu.

To login in the Docker Hub using command line:

By using docker login command you can login into the docker hub to push your custom images.

Command:

[geeks@terminal ~]$ docker login

Output:

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxxxxxx
Password:
WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Note: The login in Docker Hub is not required for downloading and installation images.

Let's start exploring various docker images command with example.

To get list of installed docker images:

By using docker images or docker image command you can check the Docker images present on the your local machine.

Example:

We will execute below command to list all images present on the local machine with the output.

Command:

[geeks@terminal ~]$ docker images

or

[geeks@terminal ~]$ docker image ls

Output:

REPOSITORY TAG IMAGE ID CREATED SIZE

We can conclude from the output that no Docker image is present on the machine.

To search docker images:

By using docker search command we can can search the docker images available on the Docker Hub.

Example:

Let's execute below command to search images for the ubuntu operating system with output.

Command:

[geeks@terminal ~]$ docker search ubuntu

Output:

The command will search for the images with string as ubuntu in its name and will list all the search results.

NAME DESCRIPTION STARS
OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 10241
[OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 369
[OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 236
[OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 199
[OK]
ubuntu-upstart Upstart is an event-based replacement for th… 102
[OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 98
[OK]
neurodebian NeuroDebian provides neuroscience research s… 61
[OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50
[OK]

To download and install docker images:

The next step will be to install image from the official Docker Hub registry using docker pull command.
The syntax is  docker pull image_name:tag

Example:

Let's download and install ubuntu operating system image on the local machine where the image name is gathered from the search result.

Command:

[geeks@terminal ~]$ docker pull ubuntu

In the above command, the docker will download latest image of Ubuntu as nothing is specified after image name ubuntu.

Output:

This command will by default download and install the latest Ubuntu image available on the Docker Hub.

Using default tag: latest
latest: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete
c1bbdc448b72: Pull complete
8c3b70e39044: Pull complete
45d437916d57: Pull complete
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for ubuntu:latest

The below command will download and install trusty version of Ubuntu image from docker hub instead of latest available.

[geeks@terminal ~]$ docker pull ubuntu:trusty

Let's check the available Docker images on the local system now.

[geeks@terminal ~]$ docker images

Output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              775349758637        5 weeks ago         64.2MB
ubuntu              trusty              2c5e00d77a67        6 months ago        188MB

Where
REPOSITORY - This is the name of the repository in the docker hub.
TAG − Used to logically tag images.
IMAGE ID − A unique identifier for the image.
CREATED − The number of days since the image was created.
SIZE − The size of the image.

To list only ID of docker images:

[geeks@terminal ~]$ docker images -q

Output:

775349758637
2c5e00d77a67

To check history of docker image:

By using docker image history command we can check the history of modifications made in the image

Example:

Let's check history of Ubuntu image using it's image ID

Command:

[geeks@terminal ~]$ docker image history 775349758637

Output:

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
775349758637        5 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
                    5 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B
                    5 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B
                    5 weeks ago         /bin/sh -c [ -z "$(apt-get indextargets)" ]     987kB
                    5 weeks ago         /bin/sh -c #(nop) ADD file:a48a5dc1b9dbfc632…   63.2MB

To inspect docker image:

By using docker image inspect command we can check the detailed information of the image.

Syntax:

[geeks@terminal ~]$ docker image inspect IMAGE ID

Example:

Let's inspect Ubuntu image.

Command:

[geeks@terminal ~]$ docker image inspect 775349758637

In the above command, the details of the image with IMAGE ID 775349758637 will be displayed.

To delete docker images:

By using docker rmi command you can delete docker images present on your local machine.

Syntax:

[geeks@terminal ~]$ docker rmi IMAGE ID

Example:

Let's delete the Ubuntu trusty image from local machine.

Command:

[geeks@terminal ~]$ docker rmi 2c5e00d77a67

In the above command we are using IMAGE ID of Ubuntu Trusty image for deletion from local machine.

Output:

Untagged: ubuntu:trusty
Untagged: [email protected]:3590458403b522985068fa21888da3e351e5c72833936757c33baf9555b09e1e
Deleted: sha256:2c5e00d77a67934d5e39493477f262b878f127b9c01b491f06d8f06f78819578
Deleted: sha256:664a2bb343be8b1a691e0ce9c563ee654c30a1c694dc1b25ebb5467fa3d074fd
Deleted: sha256:4ac0c5a114c714b429ff471f5415974618e0b6fc743070d445cd0e2ac586cc7b
Deleted: sha256:5f200444c0009c41eb39bc1cf37a5b3c2e953478f2f373c5a1873dee820e0ced
Deleted: sha256:b057ab380990c219581e3b074919413ebe31079cbd0d615f63872c471b4dc633

To delete docker image forcefully:

[geeks@terminal ~]$ docker rmi -f 2c5e00d77a67

The command will untag all images matching with image ID 2c5e00d77a67 and deletes all those the images.

To delete dangling images:

Dangling images are those images which are not associated with any tag. These unused images are just present on your disk consuming space and it's safe to delete them

Command:

Get the list of dangling images present on your local machine

docker images -f dangling=true

Delete all those dangling images

docker images purge

Please use comment section for posting any queries, feedback.

Abhijit Sandhan

Loves Open Source, Blogging, Traveling, Hiking and sharing Knowledge!

Leave a Reply

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

Back to top button
Close