Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

Working with Docker Images - Explained with Examples

docker tutorial, docker images explained, docker images tutorial, docker images examples, working with docker images, docker images layer tutorial
This tutorial post will help you to understand everything about working with Docker Images. End of this post, you will be able to understand,

1. What is Docker Images?
2. How to Create or Customize a Docker Images?
3. How to get Docker Images?
4. How to pull / download docker images?
5. How to list available docker images?
6. How to know detailed information about a Docker Images?
7. How to get history of a Docker Images?
8. How to Save or Backup a Docker Images?
9. How to Restore a Docker Images from a Backup?
10. How to remove or delete Docker Images?

In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.

What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail

Working with Docker Images - Explained with Examples

Lets get started.

Also Watch this Tutorial video on our YouTube Channel.

What is Docker Images?

Docker Images are just a templates of a Docker Containers and it is very similar to snapshot image with smaller in size. Singe Docker Image can be used to create multiple containers for different environment like development, UAT and Production.

Docker Images are very lightweight, small, and fast to deploy the containers.

Docker Images Explained with Examples - Docker Tutorial

Docker Images are consists of many layers with unique Image ID (eg : e34fs4553) from Base Images. Each Layers may have some changes committed on top of a existing layers.

Docker Images are read only layer of Docker Containers and Docker Containers are read write layer of Docker Images.

How to Create or Customize Docker Images?

Docker Images can be created from the Container itself, when some changes are done on the container or you can create the Docker Images using a Dockerfile with specific instructions.

But remember, We must need a existing docker images for both the cases, either to create a container or to customize a new docker images. Lets see how to get these docker existing base images.

How to get Docker Images?

Docker Images are available in Docker Registry hub (https://hub.docker.com/) over the internet, it is a repository to manage the docker images. So Docker Images can be pulled (download) and pushed (Upload) to the repository at anytime.

But Account registration is required for uploading your Docker images not for downloading. Account Registration is free and pretty straight forward.

How to pull / download docker images?

Docker is the tool used to manage the entire docker environment. I assume that you have installed and configured Docker in your server, if not please refer this link How to Install Docker on Linux.

Lets take an example that, I want "Hello-world" and "CentOS" docker images.

We must know the exact repository name to pull corresponding docker images. So use "docker search" command to find the correct repository name to download the particular docker images.

For Hello-World:
[root@docker-host ~]# docker search hello-world

For CentOS:
[root@docker-host ~]# docker search centos

Above commands will give the list of available docker images from docker hub registry with name, description, number of stars awarded, whether the image is official, and whether it is automated as shown in the below image.

docker search command examples

Get repository path from the name column to download the particular docker images. Similarly you can try for other docker images too.

For our example,
Hello-World has "docker.io/hello-world"
centos has "docker.io/centos"

Use "docker pull" command to download the particular docker images. By default, it will check the local docker hub regsitry. If it didnt find the requested images locally, then it goes to download from online docker hub as below. This will download each layer of docker images.

For Hello-World Docker Image:
[root@docker-host ~]# docker pull docker.io/hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Image is up to date for docker.io/hello-world:latest
[root@docker-host ~]#

For Centos Docker Image:
[root@docker-host ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for docker.io/centos:latest
[root@docker-host ~]#

How to list available docker images?

Use "docker images" command to know the list of docker images available in your local docker host.

[root@docker-host ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        latest              49f7960eb7e4        2 weeks ago         200 MB
docker.io/hello-world   latest              e38bc07ac18e        2 months ago        1.85 kB
[root@docker-host ~]#

Above "docker images" command displays the list of docker images with following information.

Repository - Name of the Repository
Tag - Version of the Docker Images
Image ID - Unique Docker Images ID to manage the Docker Images
Created - When Docker Images is created
Size - Size of the Docker Images

How to know detailed information about a Docker Images?

Use "docker inspect" command to know detailed information about a Docker Images available locally using Docker Image ID.

This will give lot of information about Docker Images as below.

[root@docker-host ~]# docker inspect 49f7960eb7e4
[
{
"Id": "sha256:49f7960eb7e4cb46f1a02c1f8174c6fac07ebf1eb6d8deffbcb5c695f1c9edd5",
"RepoTags": [
"docker.io/centos:latest"
],
"RepoDigests": [
"docker.io/centos@sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322"
],
"Parent": "",
"Comment": "",
"Created": "2018-06-04T22:19:34.475342266Z",
"Container": "ef04c6fb40e53318c38014531a0bb5647e2d659dbe8a5e375374d4bbac57a02d",
"ContainerConfig": {
"Hostname": "ef04c6fb40e5",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD ["/bin/bash"]"
],
"ArgsEscaped": true,
"Image": "sha256:40ef8420eda09fa48ca36df234cb2b68387b5e13abb352e6390da642afe0351d",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.schema-version": "= 1.0     org.label-schema.name=CentOS Base Image     org.label-schema.vendor=Cent
OS     org.label-schema.license=GPLv2     org.label-schema.build-date=20180531"
}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:40ef8420eda09fa48ca36df234cb2b68387b5e13abb352e6390da642afe0351d",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.schema-version": "= 1.0     org.label-schema.name=CentOS Base Image     org.label-schema.vendor=Cent
OS     org.label-schema.license=GPLv2     org.label-schema.build-date=20180531"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 199678471,
"VirtualSize": 199678471,
"GraphDriver": {
"Name": "overlay2",
"Data": {
"MergedDir": "/var/lib/docker/overlay2/a51f11ede4b40dc6c2838d56e979d0c7a5486a54b4e1925e4152392a7ef3c34b/merged",
"UpperDir": "/var/lib/docker/overlay2/a51f11ede4b40dc6c2838d56e979d0c7a5486a54b4e1925e4152392a7ef3c34b/diff",
"WorkDir": "/var/lib/docker/overlay2/a51f11ede4b40dc6c2838d56e979d0c7a5486a54b4e1925e4152392a7ef3c34b/work"
}
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:bcc97fbfc9e1a709f0eb78c1da59caeb65f43dc32cd5deeb12b8c1784e5b8237"
]
}
}
]
[root@docker-host ~]#

How to get history of Docker Images?

Use "docker history" command along with Docker Image ID to get a history of a Docker Images.
[root@docker-host ~]# docker history e38bc07ac18e
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
e38bc07ac18e        2 months ago        /bin/sh -c #(nop)  CMD ["/hello"]               0 B
<missing>           2 months ago        /bin/sh -c #(nop) COPY file:3c3ca82dfdb40d...   1.85 kB
[root@docker-host ~]#

Where "e38bc07ac18e" - It is a docker ID of "hello-world" docker image.

How to Save or Backup Docker Images?

Use "docker save" command along with Docker Image ID to save or backup a Docker Images in archive format. It can be used to restore the Docker Images in any Docker Host, when it is required.

[root@docker-host ~]# docker save e38bc07ac18e > backup_hello-world.tar

List the folder to confirm the docker Image ID is saved or not.

[root@docker-host ~]# ll backup_hello-world.tar
-rw-r--r-- 1 root root 11776 Jun 19 17:52 backup_hello-world.tar
[root@docker-host ~]#

How to Restore Docker Images from a Backup?

Use "docker load" command along with Docker Image ID to restore from backup.

[root@docker-host ~]# docker load -i backup_hello-world.tar
Loaded image ID: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
[root@docker-host ~]#

use "docker images" to confirm the backup is loaded the Docker Image or not.

How to remove or delete Docker Images?

use "docker images" to find the Docker Image ID or Image Name and use "docker rmi" command  to remove or delete docker images.

[root@docker-host ~]# docker rmi 49f7960eb7e4

To remove multiple docker images, mention the docker images ID separated by spaces as below.

[root@docker-host ~]# docker rmi 49f7960eb7e4 47fjay738290 987tr78rt5g5

To remove all docker images in a single command,

[root@docker-host ~]# docker rmi $(docker images -a -q)

Going forward, we will play more with docker tool.
Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.