How To Use Docker Save Image and Export for Sharing

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Docker is a containerization technology that allows you to run software in isolated, secure environments. Docker Image and Export allow you to save your work as an image of the entire platform for sharing across devices and more. This process can be done by command line or GUI

Docker is a popular open-source software container platform. The “docker load” command loads an image from a file and the “docker export” command saves it as an image.

How To Use Docker Save Image and Export for Sharing

You may not know how to share containers and images with other developers or computers if you’re new to Docker. Is it feasible, however, to share containers? Yes! Allow the docker save image and docker export image commands to do their work!

In this article, you’ll learn how to use Docker commands to save and export containers and images for sharing.

Ready? Let’s get going!

Prerequisites

This will be an interactive presentation. If you want to follow along, make sure you have these items:

  • This article makes use of Docker Desktop or Engine version 20.10.7.
  • This tutorial uses Ubuntu, although any Docker-compatible operating system will work.

Installation and Use of Docker on Ubuntu (In the Real World)

Creating a Docker Base Application

Before you begin exporting or storing Docker containers and images, you must first create a sharing application. You’ll use a command-line environment to create a fundamental arithmetic solution as a Docker application.

1. Create a Dockerfile file for your project and put the code below into it.

The code below performs a simple arithmetic operation, yielding a value of 10.

# busybox is a single executable file that contains Unix tools and allows you to conduct some math. CMD echo $(((20*5)/10)) FROM busybox # Performs a simple arithmetic operation that returns a value of 10.

2. Next, open your terminal and type the docker build command to create the (arithmetic) application in the working directory (.).

—tag arithmetic docker build

If you’re using Linux, you may need to prepend the sudo command, such as sudo docker build —tag arithmetic, to prevent receiving a “permission denied” error. The sudo command raises the command to the level of administrator.

Troubleshooting Docker Permission Denied Issues is related.

3. Run the Docker application you created using the command below (arithmetic). When you run the Docker app, it immediately builds a container for your application.

Application Execution (arithmetic) Application Execution (arithmetic)

Docker Containers for Export

It’s time to learn how to export a Docker container now that you have a basic Docker application. Building a large application takes a long time, and sharing the container with your team might be a pain. Is there any way to distribute the container more quickly? Yes!

Export the Docker container instead of creating the application straight from a Dockerfile.

To list all (ls —all) containers accessible on your workstation, use the docker command below. You’ll need the CONTAINER ID of the container you wish to share in order to export it in the following step.

Note that only containers, not pictures, may be exported.

ls —all docker container

All Docker Containers are listed here. All Docker Containers are listed here.

2. Then, to export the Docker container of your choosing, use the command below (container-id). In step one, replace container-id with the container ID you observed.

When exporting, you save the container’s filesystem as a.tar archive (arithmetic.tar).

docker export container-id > arithmetic.tar

3. Finally, open your file manager and look for a new.tar archive in your project folder (arithmetic.tar). You may now share the container with anybody since you’ve exported it to a.tar archive.

Docker Containers Import

Perhaps you need to import a Docker container that a colleague shared with you instead of exporting. If that’s the case, you’ll need to use the docker import command. The docker import command turns the exported filesystem into an image filesystem that can be run locally.

1. Import a container (arithmetic.tar) and convert it to an image using the docker command. You must attach a tag (latest) and name the picture (put any name here) when importing, as seen below.

put any name here: docker import arithmetic.tar latest

2. Next, use the command below to list all existing Docker images so you can see whether a new one has been created.

A new Docker image is available, as seen below.

All Docker Images are listed here. All Docker Images are listed here.

3. Now run the following docker run command to open the shell (sh) for the imported image. (-i and -t flags) The container runs interactively and is connected to your terminal.

put any name here:latest sh docker run -i -t

You’re now in the shell of the imported picture, as shown by the prompt below (/ #).

Accessing Imported Image's ShellShell Access for Imported Image

4. Finally, execute the command below to verify that the image works properly.

If the picture is correct, you’ll receive a 10 as seen below.

Checking whether the Docker Image is functional Checking whether the Docker Image is functional

Because the imported image is a filesystem image, you’ll receive an error if you attempt to execute it immediately, as seen below.

When running a Docker image directly, an error occurs. When running a Docker image directly, an error occurs.

Using Docker to save images Image to Save

Instead of a container, you could wish to store a Docker image. If that’s the case, instead use the docker save image command. The docker save command, unlike the docker export command, allows you to save one or more images to a tar archive and share them with anybody.

Only Docker images, not containers, may be saved and loaded.

  1. To save a Docker image (arithmetic) to your preferred.tar archive (arm image.tar), use the command below. You may now distribute the.tar archive to other developers for them to load the image.

docker save arithmetic > arm_image.tar

2. Now type the following command to create a directory with the name you choose (mkdir arm). However, for this demonstration, the directory is called arm. The image is then extracted from the.tar archive (-xf arm image.tar) and placed in the new directory (arm).

You may check whether the picture you stored in the.tar archive exists by extracting the contents of the.tar archive.

mkdir arm && tar -xf arm_image.tar -C arm

3. Finally, use the tree command to see the directory tree of the image you retrieved from the /arm directory. /arm/tree

The files and subdirectories within the directory where you extracted the Docker image are listed below.

Directory of Viewing The /arm Directory Tree Directory of Viewing The /arm Directory Tree

If you want to publish your Docker image on the Docker hub, the docker save command is the best approach. Why? You’re effectively storing the image precisely how it’s designed to run using the docker save command. Furthermore, you can only upload images to the Docker hub, thus the docker export command is not a viable alternative.

Docker Images Are Loading

How would you load a picture on a system if you have previously saved it for backup or sharing? Don’t worry, the docker load command can assist you. The docker load command restores both images and tags after loading an image or repository to your computer.

Delete all previous Docker images to better understand how the docker load command works.

To list all Docker images on your workstation, use the command below.

There are two photos available, as seen below. You’ll need to remember the IMAGE ID for each of the photographs since you’ll be deleting them in the following step.

Making a list of Docker image IDs to delete Making a list of Docker image IDs to delete

2. Use the command below to remove (rm) Docker images based on the image IDs you provide. Replace the picture IDs you specified in step one with imageID1 and imageID2.

imageID1 imageID2 docker image rm

3. Finally, use the docker load command to load an image from a.tar archive (arm image.tar).

docker load < arm_image.tar

4. To list all available images on your workstation, use the docker images command as you did in step one. images docker

The freshly loaded picture may be seen below.

All Docker Images are listed here. All Docker Images are listed here.

5. Finally, run the command below to see whether the image is working.

The command returned a result of 10, indicating that the loaded image is functional.

Using the Docker Loaded Image (arithmetic) Using the Docker Loaded Image (arithmetic)

Conclusion

You’ve learned how to export and import containers, as well as load and save photos, in this lesson. You should now have a basic understanding of how to convert your Docker save images and containers into a shared format like a TAR file.

You’ll need to share your Docker image or container at some point. Now, how do you intend to use your newly acquired expertise while dealing with Docker containers and images, possibly as part of a backup system?

The “docker save image as tar” is a command that allows users to save an image of their current Docker container and export it. This can be helpful if you want to share your project with others.

Related Tags

  • docker load saved image
  • docker save image to filesystem
  • docker save vs export
  • docker save command
  • docker load image