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

Basic ideas and codes for implementing video players in browsers

Table of contents Preface Summary of audio and vi...

Docker installation of MySQL (8 and 5.7)

This article will introduce how to use Docker to ...

What is TypeScript?

Table of contents 1. JavaScript issues 2. Advanta...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...

Detailed explanation of the difference between Vue life cycle

Life cycle classification Each component of vue i...

Complete steps to achieve high availability with nginx combined with keepalived

Preface In order to meet the high availability of...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

Analyzing the four transaction isolation levels in MySQL through examples

Preface In database operations, in order to effec...

JavaScript flow control (branching)

Table of contents 1. Process Control 2. Sequentia...

VMware Workstation installation Linux (Ubuntu) system

For those who don't know how to install the s...

Detailed explanation of JavaScript Promise and Async/Await

Table of contents Overview Four examples Example ...