Docker

How to save and load docker image?

Save and Load docker image

Docker is a very versatile software as well as a ground breaking concept. It gives you flexibility in over all the aspects. Today, we are going to understand and discuss “save and load docker image“.

Question: Why there is a need to do so? Why at any point of time it is required to save a docker image?

Answer: Imagine, you are working on a project and during that there is a need to share the compiled piece of code along with its dependencies, then in that situation you can simply create a container and then add your code to it, install required libraries or other software utilities as well. After that you can simply commit the changes to the running container, which in turn as a result will create “your custom image” in your local repositories. In addition, that image can be further save to your file-system (in the form of tar file) and further can be delivered to the other project members.

Current System State

Firstly, we need to check is there any running or stopped containers present in the system or not.

list all the stopped containers

sudo docker container ls -a

Secondly, we also need to check what images and how many images we are currently having in our system. In our case, we can clearly see by the output of the command below that there are none.

list all the images

sudo docker image ls -a

Now, we need to do the following:

  • Create a container with name “container1
  • Install “elinks” in it
  • Check elinks by accessing “google.com

All of the things mentioned above, are performed in the picture below, do look closely.

run a docker container

sudo docker container run -i -t –name container1 alpine ash

apk add –no-cache elinks

elinks google.com

After doing the above steps, “Go to new terminal” and now running the following in order to save the current state of the container as an image, which we are going to use later.

commit a running container

sudo docker container commit container1 alpine-elinks-installed

From the picture below, we can see that there are two images available in our system:

  1. alpine = this is our base image
  2. alpine-elinks-installed = this our custom image created previously
list docker images

sudo docker image ls

Save docker image

Now, after doing all the things discussed above, it is time to learn about saving docker images.

From the image below, we can see that only a command is required to save a docker image in our system and make it portable for further use.

save docker image as tar file

sudo docker image save -o alpine-elinks.tar alpine-elinks-installed

  • sudo = run the command as root user
  • docker = which command
  • save = context to the docker command
  • save = flag, which tells docker to save some file
  • -o = write to a file, instead of STDOUT.
  • alpine-elinks.tar = name of the output file
  • alpine-elinks-installed = name of the docker image present in the local repository.

We can check the created portable tar file, see the output of the command below, it is showing “alpine-elinks.tar” a file which is being created in the previous step, it is our portable docker image which can be further used for transportation and any other application.

long listing in linux with human readable form

ls -lh

  • ls = list the contents of directory specified
  • -lh = long listing with showing the size of file in human readable form

Question: How to cross verify that our portable image is perfectly working?

Answer: In order to do that, we need to first delete the already present image/images in our system and to do so, follow the command given below.

forcefully remove a docker image

sudo docker image rm -f alpine-elinks-installed

  • sudo = run the command as root user
  • docker = command’s name
  • image = giving the context to command
  • rm = remove the image
  • -f = forcefully remove the image
  • alpine-elinks-installed = name of the image

Now, we can see that there is only the base image present in our local repository and our “alpine-elinks-installed” is no more present.

list docker images

sudo docker image ls

Load docker image

Now, it is time to run our portable image and create a container from it, and check whether “elinks” which is previously installed on it, is working in it or not.

To do so, run the following command to load the portable docker tar file.

load a docker image which is saved as tar file

sudo docker image load –i alpine-elinks.tar

  • sudo = run the command as root user
  • docker = command’s name
  • image = give context to the command
  • load = specifying that we are about to load some image
  • -i = read from tar archive file, instead of STDIN.

Now check the output of the following command, it is showing that the image is now present in our local repository.

sudo docker image ls

Testing time, now run the command used in the picture below in order to create a container from our custom image which is successfully loaded in the previous step. Moreover, also visible in the image below that the “elinks google.com” is also able to access the website which means everything till now is perfectly done.

run docker container with name specified

sudo docker run -i -t –name elinks-container alpine-elinks-installed ash

elinks google.com

Comment here