So far you have seen Docker image is built from Dockerfile. An image is comprised of multiple layers and it contains the code that gets executed in a Docker container. You can build a custom image, tailor it to the best fit for your need or you can just use docker pull image_name to download an image from docker hub to your host and then run it using docker run as you did in hello-world example. Now we will look into various aspect of Docker image in depth.

List of handy docker image commands

Use a Docker image

Docker needs to have the corresponding image locally before running it. If the image is not present, docker will try to download it from the image repository (docker registry).

1. Get the image from Docker registry

Use docker pull command to download the image from docker hub.

Copy

As I have not specified any registry url, it will pull from Docker Hub. But you can always specify the custom registry if you are using one as docker pull myregistry.local:5000/testing/test-image

Previously I explained, that an image is composed of multi-tiered storage. You can that also see that in the above console log that docker is downloading layer-by-layer, not as a single image file. This optimizes the docker storage and enables to reuse already available layers locally.

2. Run docker image

Technically, you run a container that is created from an image using docker run . Execute the below command to run this in an interactive mode inside a bash shell.

Copy

Now you are running a Nginx:latest container and bash session has started for you in the interactive mode.

  • -it : This is just like writting -i & -t  together.
    -i : Enables/runs the container in interactive mode
    -t : Starts a terminal session in bash
  • --rm : This makes sure the container is deleted after it exits. By default, a container is not deleted immediately. We are only enabling this for learning purpose, use cautiously in production.
  • nginx:latest : This is the image to be used for stating the container
  • bash : Interactive bash shell to use with the running container.

Additionally, you can add -p 80:80 to bind the Nginx to port 80.

cat /etc/os-release prints out the information about OS used in nginx image. Now, type exit in the terminal to get out of the container and as you rightly guessed, the container will be deleted once you type exit.

Manage docker image (List & delete image )

When you do not want to use a specific image any more or free the disk space, docker allows you to delete the unnecessary images.

1. To list docker image

To list already pulled images.

Copy

REPOSITORY – is the image name.

TAG – Is a label, usually a version or build number

IMAGE ID – Uniquely identify the downloaded image (mirrored image)

SIZE – Size of the image on your machine. This could be different from the actual size shown on the docker hub, as it is published in compressed form on docker-hub.

2. List dangling image

When you type docker image ls , you may find some images without a name and a tag like below

To list out the dangling image specifically

Copy

To specifically delete dangling image docker image prune

3. To list all image  – including dangling and the middle layers

Use the below command to list all the images in your local docker repository

Copy

4. List similar images

There are situations when you may have multiple images of Nginx with different versions or built on different os. Below you can see alpine linux with different tags.

Copy

Delete docker image

To delete a docker image from local machine/host, use the below command.

docker image rm [OPTIONS] IMAGE [IMAGE...]

Alternatively, I prefer using the below one

docker rmi [OPTIONS] IMAGE [IMAGE...]

You can delete image using name (repository), image-id and digest. The image-id could become long to type, so you can pass the first few characters as long as it is enough to distinguish the image from others.

Similarly, you can also use the name:tag to delete the image.

Copy

Untagged images vs deleted images

Observe the output of delete image commands docker rmi or docker image rm, you will notice deletion message is basically 2 types, untagged and deleted. Remember, a unique identifier of an image is its ID. But, it can have multiple labels or tags.

When we run the delete command, we ask to delete the image of a tag. Docker checks if there are other images as well that are tagged to this image or if there is an image that uses it as a base image then it will only perform untagged and it will not delete.

If docker finds no dependent image pointing to the same, then it will perform delete and indicate as deleted.

How multilayered implementation works?

In docker, each image is made up of multiple layers. Docker uses Union FS to combine these layers into one final image.

Union Fs has 2 purposes, (i) multiple disks can be linked to the same directory, (ii) Unionfs allows any mix of read-only and read-write branches, as well as insertion and deletion of branches anywhere in the fan-out.

By |Last Updated: July 16th, 2019|