Docker Images! WTH is that?
By: Meghal Chhabria
Tags: Docker Images, Containers, Docker, Microservices, Basics

Ep:02 Hands-on Docker Images commands and usage

I hope you guys are doing well.

Source : Dzone

Docker Images are the template that contains set of instructions for creating a container.

Fair enough? I don’t think so.

Docker Images is the combination of all the scripts that is required for your application to run.

Docker Files are used to create docker images using set of available docker commands, lets dive into those commands to create an image and upload to a registry and also try to pull it and run a container. :D Excited?

*** Let’s Start ***

List the available images on the host

... $ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f643c72bc252 2 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

Get the detailed information of the image like image name, full id, container details, registry details etc.

Use the image name or the image id with the command exposed .

... $ docker inspect ubuntu

[

{

"Id": "sha256:f643c72bc25212974c16f3348b3a898b1ec1eb13ec1539e10a103e6e217eb2f1",

"RepoTags": [

"ubuntu:latest"

],

"RepoDigests": [

"ubuntu@sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c"

],

"Parent": "",

"Comment": "",

"Created": "2020-11-25T22:25:29.546718343Z",

"Container": "0672cd8bc2236c666c647f97effe707c8f6ba9783da7e88763c8d46c69dc174a",

"ContainerConfig": {

"Hostname": "0672cd8bc223",

"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\"]"

],

"Image": "sha256:28e90b4e135b38b4dd5efd0045019a2c8bfb7e114383e3a4ae80b1ec0dcaaf79",

"Volumes": null,

"WorkingDir": "",

"Entrypoint": null,

"OnBuild": null,

"Labels": {}

},

"DockerVersion": "19.03.12",

"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"

],

"Image": "sha256:28e90b4e135b38b4dd5efd0045019a2c8bfb7e114383e3a4ae80b1ec0dcaaf79",

"Volumes": null,

"WorkingDir": "",

"Entrypoint": null,

"OnBuild": null,

"Labels": null

},

"Architecture": "amd64",

"Os": "linux",

"Size": 72898198,

"VirtualSize": 72898198,

"GraphDriver": {

"Data": {

"LowerDir": "/var/lib/docker/overlay2/25f72d93acb934405b2b479b3a74be42a6c38b903305534e20d3978c3a0f9d4b/diff:/var/lib/docker/overlay2/146989b9b288c76d53f3af5188ec829ffbbf98eb4df5fd604e6423c8d502b009/diff",

"MergedDir": "/var/lib/docker/overlay2/287d5fb4298c98c16cfbb149ae7706e08eb0544d4696961698500a7d9b45c3e8/merged",

"UpperDir": "/var/lib/docker/overlay2/287d5fb4298c98c16cfbb149ae7706e08eb0544d4696961698500a7d9b45c3e8/diff",

"WorkDir": "/var/lib/docker/overlay2/287d5fb4298c98c16cfbb149ae7706e08eb0544d4696961698500a7d9b45c3e8/work"

},

"Name": "overlay2"

},

"RootFS": {

"Type": "layers",

"Layers": [

"sha256:bacd3af13903e13a43fe87b6944acd1ff21024132aad6e74b4452d984fb1a99a",

"sha256:9069f84dbbe96d4c50a656a05bbe6b6892722b0d1116a8f7fd9d274f4e991bf6",

"sha256:f6253634dc78da2f2e3bee9c8063593f880dc35d701307f30f65553e0f50c18c"

]

},

"Metadata": {

"LastTagTime": "0001-01-01T00:00:00Z"

}

}

]

3. Pull images from a registry using this command,

... $ docker image pull centos:latest

latest: Pulling from library/centos

7a0437f04f83: Pull complete

Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1

Status: Downloaded newer image for centos:latest

docker.io/library/centos:latest ... $ docker image ls //verify it using this command

REPOSITORY TAG IMAGE ID CREATED SIZE

centos latest 300e315adb2f 5 days ago 209MB

ubuntu latest f643c72bc252 2 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

In the above example, we just pulled the CentOS from dockerhub.com.

Log into the image using the following steps,

... $ docker run -it centos bash //remember to use ctrl+p ctrl+q,

[root@26114b36d46f /]# ls // to keep the container running

bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

... $ docker ps //verify the container is running using this command

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

26114b36d46f centos "bash" About a minute ago Up About a minute gifted_jemison

Remove a image from our host , follow with me,

... $ docker rmi 300e315adb2f

Error response from daemon: conflict: unable to delete 300e315adb2f (cannot be forced) - image is being used by running container 26114b36d46f

Saw the above error? Reason the container is still running so image cannot be removed.

... $ docker ps //verify the container is running using this command

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

26114b36d46f centos "bash" About a minute ago Up About a minute gifted_jemison ... $ docker stop 26114b36d46f // name or container Id can be given.

26114b36d46f ... $ docker ps // verify that the container is stopped

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES //Try to delete now $ docker rmi 300e315adb2f

Error response from daemon: conflict: unable to delete 300e315adb2f (must be forced) - image is being used by stopped container 26114b36d46f

In the last log, if you can see after stopping the container , we tried to delete the image, notice now it’s asking us to remove it forcefully reason being that the docker was running and its context is still saved in host (be careful while performing this action). We can delete it with force using the below command,

... $ docker rmi -f 300e315adb2f //Name/container Id can be used

Untagged: centos:latest

Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1

Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 ... $ docker images //verify that the image is deleted now

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f643c72bc252 2 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

Also you can save the image in tar and can load it anytime, anywhere.

... $ docker image save -o hello-world.tar hello-world:latest //to verify do ls -lrt $ ls -lrt . -rwxrwxrwx 1 meghal meghal 24576 Dec 13 22:35 hello-world.tar



to load this tar as an image, follow these simple step,

... $ docker images // you can see, we don't have hello-world image

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f643c72bc252 2 weeks ago 72.9MB ... $ docker load -i hello-world.tar // give the tar name here

Loaded image: hello-world:latest ... $ docker images // verify that the image is present

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f643c72bc252 2 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

Rename the tag name of an image from latest to custom tag name

... $ docker tag hello-world:latest hello-world:version1 $ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f643c72bc252 2 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

hello-world version1 bf756fb1ae65 11 months ago 13.3kB



We can see, two images with different tag name are present, reason is we have tagged it locally, you can push this to the registry to replace it, using the following command.

... $ docker push hello-world:version1

The push refers to repository [docker.io/library/hello-world]

9c27e219663c: Layer already exists

Note I’m not allowed to push it since I’m using hello-world image. If it’s your custom image , you would be able to do this.

That’s all for today, I hope you learnt to play around with container, Next up we will learn about image commands in docker.

Please take a minute to appreciate your efforts. You made it. Learnt something new. :) Congratulations

Article 1 : Hello World Docker

Article 2 : Docker Terminologies

Article 3 : Docker Basic Commands. PS Cheat Sheet Ep-01

Follow me on linkedin,

Stay tuned , Keep Learning. Cheers :D

Thanks for reading 👍🏻