Docker Images

Learn via video courses
Topics Covered

This article introduces the essentials of Docker images, focusing on creating custom images on your journey in developing containerized applications, tools, and services. Ideal for newcomers to Docker, we will examine the key components of a Docker image.

We aim to clarify the process of building a personalized Docker image, an executable software package containing all the necessary elements for running an application. It dictates the setup of a container, detailing the software components and their operation. A Docker container is a virtual environment that combines application code with its dependencies, ensuring consistent functionality across different computing systems.

Let's get started.

Usage

A Docker image contains everything required to run a containerized application - this includes code, config files, environment variables, libraries, and runtimes.

When an image is deployed in a Docker environment, it can be run as a container. The docker run command will create a container from a specified image.

Not only are docker images reusable assets that can be deployed on any host, but developers can also take the static image layers from one project and use them in another. This saves time as there is no need to recreate an image from scratch.

How to Create a Docker Image?

Docker images can be created in two ways :

  • through an interactive method or
  • by using a Dockerfile.

Interactive Method

The interactive method for creating docker images is simple and easy to follow.

To get started, all you need is to launch Docker and open a terminal session. Then, use the Docker run command image_name:tag_name to start a shell session with the container that was launched from the image.

With this method, users can run a container from an existing Docker image and make any needed changes to the environment before saving the image - making it a convenient option for those looking to create docker images without much hassle.

Dockerfile

This method to create the docker image is more difficult and time-consuming but does better in continuous delivery environments.

This approach requires making a plain text Dockerfile with the instructions for creating the image. The Dockerfile is created, and the commands needed to build the image are executed.

Child Commands

Let's take a look at the commands used in Dockerfile.

CommandUse case
FROMUsed to specify the base image.
WORKDIRUsed to set the present working directory.
RUNUsed to run a command for installing applications and packages.
COPYUsed to copy files or directories from the specified location.
ADDSame as COPY to some extent. Used when remote URLs and unpack compressed files.
ENTRYPOINTUsed to specify the command that has to be run after the container starts.
CMDUsed to specify the arguments passed to the entry point.
EXPOSEUsed to specify the port through which the application can be accessed.
LABELUsed to add metadata to the docker image.

Image Layers

Each file that makes up a Docker image is called an image layer. In a docker image, layers build on top of each other in stages to create images. The bottom layer is the base image, and each successive layer is dependent on the one below it.

Container Layer

The container layer is a thin writable layer that docker adds each time a container is launched from an image. It stores all changes to the container throughout its runtime.

As this layer is the only difference between a live operational container and the source Docker image itself, any number of like-for-like containers can potentially share access to the same underlying image while maintaining their state.

Parent Image

The first layer of a Docker image is called the parent image. It's the foundation that all other layers are built on and provides the basic blocks for your container environments.

Base Image

A base image is an empty first layer that allows you to build your Docker images from scratch. This gives you more control over the contents of the images, but it's generally intended for more advanced Docker users.

Docker Manifest

A Docker image comes with a manifest file that contains information about the image in JSON format. This includes image tags, a digital signature, and instructions on how to configure the container for different types of host platforms.

Container Registries

It is a storage and distribution system for Docker images which are tagged and have a name.

Container Repositories

It is a collection of related container images used to provide different versions of an application.

Example Dockerfile

Let's take a look at a sample Dockerfile. As you can see, it utilizes commands explained in this article.

The Docker Build Context

The docker build commands build Docker images from a Dockerfile and a “context”. The "context" here refers to the set of files located at the PATH or URL specified as the positional argument to the build command.

The below example shows a build command that uses the current directory (.) as a build context :

Conclusion

Although Docker has a lot of objects that work together to run a containerized application, the docker image is arguably the most important one.

That is why knowing about the concepts and finer details about docker images is important. In this article, we have covered docker images in great depth.