DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published at yuscode.hashnode.dev

Chapter 2 - Docker Images

Table of Contents

Introduction

In this chapter, we will dive into the concept of Docker images. Understanding Docker images is fundamental to working with Docker, as images serve as the templates or blueprints for creating containers. We'll cover what Docker images are, how they work, and how to manage them using various Docker commands.

What is a Docker Image?

A Docker image is a lightweight, standalone, and executable package that includes everything an application needs to run, such as code, libraries, dependencies, and settings. It's a template for containers, allowing you to create multiple containers from a single image. Let's breakdown this definition of a Docker image by explaining the meaning of some key terms:

  • Stand-alone: A Docker image is a self-contained unit that doesn't rely on external dependencies or libraries to function. It has everything it needs to run, making it a standalone package.

  • Executable: A Docker image is executable because it can be run directly, without the need for additional compilation or interpretation.

  • Code: This refers to the application code, such as Go, Python, Java, or Node.js, that is packaged inside the Docker image.

  • Libraries: Libraries are pre-written code that provides common functionality, such as data encryption or networking. Docker images often include libraries that the application code relies on.

  • Dependencies: Dependencies are external libraries or services that the application code requires to function. Docker images can include these dependencies, making it easy to manage and deploy applications.

  • Settings: Settings refer to the configuration options, environment variables, and other parameters that are used to customize the application or service running inside the Docker image. These settings can be defined during the image build process or when creating a container from the image.

Understanding Images as Templates for Containers

Think of a Docker image as a blueprint or a template for creating containers. When you create a container from an image, Docker uses the image as a starting point and adds a read-write layer on top, allowing you to make changes to the container without modifying the original image. This process is called containerization.

In the explanation above, we mentioned that Docker adds a read-write layer to images. Let's breakdown what that means:

  • When you create a container from an image, Docker uses the image as a starting point. Docker then adds a thin, writable layer on top of the image. This layer is called the "container layer" or "read-write layer".

  • This read-write layer is where you can make changes to the container's filesystem, such as:

    • Creating new files
    • Modifying existing files
    • Deleting files
    • Installing new packages

The read-write layer is stored separately from the original image, which remains unchanged. By using a read-write layer, Docker allows you to make changes to the container without modifying the original image. This provides several benefits, such as:

  • You can create multiple containers from the same image and customize each
    one independently.

  • You can make changes to a container without affecting other containers
    created from the same image.

  • You can easily revert to the original image if needed.

Another important point to note about Docker Images is that they are made up of layers, like a stack of building blocks. Each layer represents a set of changes to the filesystem. This means that multiple images can share common layers, making storage more efficient and reducing duplication.

Working with Images

Pulling Images from Docker Hub: docker pull

To use a Docker image, you need to pull it from Docker Hub, a cloud-based registry service that allows you to store and share images. It is the default repository for Docker images.

To pull an image from Docker Hub, use the docker pull command:

docker pull <image_name>:<tag>
Enter fullscreen mode Exit fullscreen mode

For example, to pull the latest Ubuntu image, you would use:

docker pull ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

NB: If you don't specify a tag, Docker will pull the latest tag by default.

Listing Images: docker images

To list all the Docker images on your local machine, use the docker images command:

docker images
Enter fullscreen mode Exit fullscreen mode

This command provides a table of available images, showing details such as the repository name, tag, image ID, creation date, and size.

Viewing Detailed Information About an Image: docker image inspect

To view detailed information about a Docker image, use the docker image inspect command followed by the image name or ID.

docker image inspect <image_name_or_id>
Enter fullscreen mode Exit fullscreen mode

For example, to inspect the Ubuntu image you pulled earlier, use:

docker image inspect ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

This command will display detailed information about the ubuntu:latest image, including its architecture, operating system, and layers.

Viewing the History of an Image: docker image history

To view the history of a Docker image, use the docker image history command followed by the image name or ID.

docker image history <image_name_or_id>
Enter fullscreen mode Exit fullscreen mode

For example, to view the image history of the Ubuntu image you pulled earlier, use:

docker image history ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

This command will display a list of layers that make up the ubuntu:latest image, including the commands that were run to create each layer.

Removing Images: docker rmi

Be careful when removing images, as this will also remove any containers created from that image. To remove an image from your local machine, use the docker rmi command followed by the image ID or repository name and tag:

docker rmi <image_name>:<tag>
Enter fullscreen mode Exit fullscreen mode

For example, to remove the Ubuntu image you pulled earlier, use:

docker rmi ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

NB: If the image is being used by any containers, you will need to stop and remove those containers first.

Conclusion

Docker images are the foundation of containerized applications, providing a reproducible and portable environment for running software. In this chapter, we covered what Docker images are, how they function as templates for containers, and essential commands for managing images. Mastering these basics will enable you to efficiently use Docker for your DevOps needs.

Feel free to leave comments and share this article. Follow my blog for more insights on Docker and DevOps!

Top comments (0)