How to create, save, and load Docker images

How to create, save, and load Docker images

There are three ways to create an image: creating a container based on an existing image, importing it from a local template, and creating it based on a Dockerfile. This blog post explains the first two.

Creating a container based on an existing image

The method is to use the docker commit command, the command format is:

 docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

The main parameter options include:

  • -a, --author="" Author information
  • -m, --message="" Commit message
  • -p, --pause=true Submitting pauses the container

For example, first create an ubuntu container running bash:

docker run –it ubuntu /bin/bash

root@d8990fec2141:/# touch test

root@d8990fec2141:/# exit

Then submit a new image based on the created container, and the container ID is required for submission.

 docker commit –m “test” –a “zmc” d8990fec2141 testimage

If successful, the long ID number of the new image will be returned, and then you can check the existing images locally:

docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

testimage latest baea98d5a437 About a minute ago 188.3 MB

…

The third line is the image just created.

PS: The image ID created using this container is different from the image ID of this container, so it can be seen that they are not the same image.

Import based on local template

You can also import an image from an operating system template file, such as using the template provided by OpenVZ. The OPENVZ download template is at: http://openvz.org/Download/template/precreated.

I tried using the template for Ubuntu 14.04:

wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz

After downloading, you can import it:

sudo cat ubuntu–14.04–x86_64–minimal.tar.gz | docker import – ubuntu:14.04

There are only two commands, but they are so obvious that I won't explain them. If successful, it will return the long ID of the image created based on the template

sudo cat ubuntu–14.04–x86_64–minimal.tar.gz | docker import – ubuntu:14.04

ab80404d13d580965b9919b640169ccb585ea7884e6aa9de1ec043075c65fe35

Then you can view the local image:

docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

ubuntu 14.04 ab80404d13d5 56 seconds ago 215.4 MB

testimage latest baea98d5a437 29 minutes ago 188.3 MB

….

In fact, it can be seen that although the template is only 75M, the created image is not small.

Image saving and loading

You can use the docker save and docker commands to save and load images.

Save the image

If you want to save the image to a local file, you can use the docker save command. For example, save the local testimage:lastest file you just created as the image file testimage.tar:

docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

testimage latest baea98d5a437 25 minutes ago 188.3 MB

ubuntu latest fa81ed084842 3 days ago 188.3 MB

….

docker save –o /data/testimage.tar testimage:latest

The sixth line above is to save the code. At this time, there is a testimage.tar file under /data. At this time, we rmi the local image and try to load it.

Loading the image

Status after deleting the image:

ubuntu@VM–223–238–ubuntu:/data$ docker rmi baea98d5a437

Untagged: testimage:latest

Deleted: baea98d5a4371a6abf9efc8c53a54a6fc5befd167bf91ce9fd4a28a6d1b7dc5b

ubuntu@VM–223–238–ubuntu:/data$ docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

ubuntu 14.04 ab80404d13d5 5 minutes ago 215.4 MB

Then load the image:

docker load --input testimage.tar

docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

ubuntu 14.04 ab80404d13d5 6 minutes ago 215.4 MB

testimage latest baea98d5a437 35 minutes ago 188.3 MB

The first line is to load the image, which can be simplified as follows:

docker load --input testimage.tar

docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

ubuntu 14.04 ab80404d13d5 6 minutes ago 215.4 MB

testimage latest baea98d5a437 35 minutes ago 188.3 MB

The loading operation will import the image and related metadata information (including tags, etc.).

Image upload

Finally, let's talk about uploading images. The image management method is very similar to git. You can use the docker push command to upload your local image to the warehouse. By default, it is uploaded to the DockerHub official warehouse (login required). The command format is:

 docker push NAME[:TAG]

Before uploading, you usually add a tag with your name (author information) to your image:

docker tag testimage:lastest zmc/testimage:lastest

docker pushzmc/testimage:lastest

It is helpful to distinguish after uploading.

I think whether it is an operation and maintenance team, a development team or a laboratory, it is necessary to have its own Docker repository to store the environment or system images that meet your needs and achieve rapid deployment.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A detailed introduction to Docker image creation, how to modify and upload images, etc.
  • Detailed explanation of two methods of creating Docker images
  • Detailed explanation of nodejs to create a minimal docker image
  • Detailed explanation of Docker learning to create an image using the commit command
  • Creating a Docker image for a web project
  • Docker uses Dockerfile to create images
  • Detailed explanation of Dockerfile instructions in Docker to create images

<<:  Detailed explanation of unique constraints and NULL in MySQL

>>:  Native Js implementation of calendar widget

Recommend

Analysis of the locking mechanism of MySQL database

In the case of concurrent access, non-repeatable ...

Vue song progress bar sample code

Note that this is not a project created by vue-cl...

MySQL 8.0.20 winx64 installation and configuration method graphic tutorial

This article shares with you the installation and...

More than 100 lines of code to implement react drag hooks

Preface The source code is only more than 100 lin...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

Use of Linux network configuration tools

This article introduces RHEL8 network services an...

Detailed installation and configuration tutorial of PostgreSQL 11 under CentOS7

1. Official website address The official website ...

SQL injection vulnerability process example and solution

Code example: public class JDBCDemo3 { public sta...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

JavaScript design pattern learning adapter pattern

Table of contents Overview Code Implementation Su...

How to check the version of Kali Linux system

1. Check the kali linux system version Command: c...

Axios secondary encapsulation example Demo in the project

1. Why do packaging? Facilitates overall code cal...

Beginners learn some HTML tags (3)

Related articles: Beginners learn some HTML tags ...