How to Export a Docker Image?

Photo of author

By admin

Have you just created a Docker container image and now you want to export it to run your freshly-developed application on another system? If yes, then you’ve just clicked the right link!

Here we’re going to provide you with all the details regarding how to export Docker image in 6 easy steps. But before dividing into that, let’s first build a brief understanding of a Docker image.

What is a Docker Image?

A Docker image includes a set of instructions used to create a container for a Docker platform. It is a standalone, lightweight, and executable software package, which includes all attributes (i.e., code, system tools, libraries, runtime, etc.) required to run an application.

Docker image helps in the convenient packaging of applications as well as pre-configured servers, which can be used privately or even publicly with other users. Docker image is a read-only-template created by using the open-source software platform, Docker, designed to run on Windows, macOS, and Linux.

In simple words, Docker facilitates smooth application development and deployment.

Docker creates remote virtualized environments for building, testing, and deploying various applications. A Docker image is a series of processes set in a Docker file. These are templates created by Docker file and arranged in layers, where each layer is dependent on the other.

Moving software from host machines to host machines sometimes lead to different issues due to the software dependencies. A container that shifts between different Docker environments using the same OS will work without any change as the image has all the dependencies needed for executing the code.

Also, containers are different from virtual machines, which cover an entire OS with the code on top of the abstraction layer from various hardware resources. A Docker host can either be a physical or a virtual machine running the OS of the host. Therefore, the execution and building of containers are performed by a Docker daemon.

Sometimes, confusion between Docker images and containers could arise due to disc space. Well, size and virtual size differ in meanings. The disc space for the writable layer and container is called the virtual size, whereas the size is the amount of disc space used by the writable layer of a Docker container.

The Components of a Docker Image

A Docker image is a collection of files that brings together dependencies, installations, and application codes essential to configure a container environment.

A Docker image can be created in either of these 2 ways:

  1. Interactive method: This is done by running a container from a Docker image and changing the container environment through a series of steps, thereby saving the result as a new image.
  2. Dockerfile method: This is done by creating a plain-text file, which gives specifications for developing a Docker image. The plain-text file is called the Dockerfile.

Let’s take a look at the main components of a Docker image and see how they work.

Image Layers

Every file that contributes to a Docker image is called a layer. These layers form a set of images, built and placed on top of each other, where each layer depends on the layer below it.

The efficient lifecycle management of a Docker image depends on the hierarchy of its layers. Therefore, it is important to organize the layers which frequently change at the top of the stack.

So, if you make changes in the layer of your image, Docker will build that layer along with all the other layers built from that particular layer. This means that if you change a layer located at the top of a stack, then it would require the least amount of computational efforts to rebuild the image again.

Container Layer

Whenever Docker launches a container through an image, a thin writable layer is added, called the container layer. It stores all changes that occur in the Docker container during its run time.

Parent Image

The parent image is the first layer of the Docker image. It is a basic building block and the foundation on which all the subsequent layers are built for a container environment.

You can find different types of parent images on the public container registry of the Docker Hub. You can also get them on various third-party services like Google Container Registry. Besides, one of the existing images can also be used for constructing new ones.

Base Image

As the name suggests, a base image is the first empty layer, which helps in building a Docker image from the very beginning. Base images provide total control over the content of any image but are mostly used by advanced Docker users.

Docker Manifest

Docker Manifest can be summed up as an additional file with a description of an image in JSON format, which comprises a digital signature, image tags, and information on how to configure the container for various kinds of the host platform.

Uses of a Docker Image

There are two important uses of a Docker image:

  1. To intermediate between various Docker functions.
  2. To be used again and deployed on any host.

What is Docker Export?

Docker Export (docker export) refers to the command which is used to export a Docker container’s file as an archive, which can be later imported as a Docker image.

It includes any files and folders created in the container, though it doesn’t export the data that is placed on the container. The Docker command is used for creating a single layer or a Docker image in order to boost the performance or share the same without using the Docker registry.

Syntax – docker export [OPTIONS] container

where [OPTIONS] is a string type input the below command because it will help in writing the output to a file.

Commanddocker export –help

How to Export Docker Image?

After running the docker export –help command, the container’s file system is saved as an archive, and a flat Docker image is created. This flat Docker image decreases the size of the Docker image.

However, it loses the metadata as well as history, which signifies that you cannot go back to the previous layer if a Docker image is imported with any exported tar file.

The main purpose of Docker is to develop a consistent environment among all Docker enabled devices as well as create templates or images that can run on any server enabled by Docker. It simplifies exporting a container and then re-importing it on another Docker enabled server.

Following are the steps to export a Docker image:

  • Step 1: Select the ID of the Docker container you would like to move.
  • Step 2: Make changes and then save the container to a new image as ‘mynewimage’.
  • Step 3: Now save this ‘mynewimage’ to a tar file. You can use an NFS share to move the tar file.

For example – $ docker save mynewimage > /tmp/mynewimage.tar

  • Step 4: Next, copy the ‘mynewimage.tar’ to the new Docker instance using any method that suits your environment, like FTP, SCP, etc.
  • Step 5: On the new Docker instance, you’ve to run the Docker load command along with stating the exact location of the image tar file.

For example – $ docker load < /tmp/mynewimage.tar

  • Step 6: Lastly, run the Docker image command and check whether the image is available or not.

Benefits of Docker Export

  1. Better Image Sharing – While sharing the Docker image, we must move it to a Registry. However, with the help of Docker export, we can export the Docker image as an archive, and thus, it can be shared with others too.
  2. Faster Speed – The speed of the Docker export is faster than re-creating the Docker image on another system.
  3. Performant – When the exported archive is imported using the Docker import command, a single layer Docker image is created, thereby boosting container performance.

Conclusion

Hence, Docker export is a powerful tool to export a container image as an archive. The archive contains all the data except data related to mounted volumes.

Also, Docker export and Docker import commands work together. Since you now know how to export Docker images, it’s time to run your application on a system of your choice.

Leave a Comment