Methods for creating saving and loading Docker images
- 2020-12-20 03:56:03
- OfStack
There are three ways to create an image: a container based on an existing image, a local template import, and an Dockerfile creation, the first two of which are covered in this post.
Create a container based on an existing image
The method uses the docker commit command, which has the format:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
The main parameter options include:
-a, �author= "" -m, message= "" To submit information -p, pause=true commit is to suspend the containerFor example, first create an ubuntu container running bash:
docker run � it ubuntu /bin/bash
root@d8990fec2141:/# touch test
root@d8990fec2141:/# exit
A new image is then submitted based on the container created, using the container ID.
docker commit � m " test " � a " zmc " d8990fec2141 testimage
If successful, the long ID number of the new image is returned, and you can then view the existing image locally:
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
testimage latest baea98d5a437 About a minute ago 188.3 MB
...
Line 3 is the image you just created.
PS: Mirror Id created with this container is different from the mirror id created with this container, so they are not the same 1 mirror.
Import based on local templates
Can also be a template file from the operating system into a mirror, such as using the template provided OpenVZ created, OPENVZ download templates in: http: / / openvz org/Download/template/precreated.
I tried to use the template of Ubuntu14.04:
wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz
After downloading, you can import:
sudo cat ubuntu � 14.04 � x86_64 � minimal.tar.gz | docker import � ubuntu:14.04
It's really just two commands, but it's so obvious that I'm not going to explain it. On success, the long ID of the image based on the template is returned
sudo cat ubuntu � 14.04 � x86_64 � minimal.tar.gz | docker import � ubuntu:14.04
ab80404d13d580965b9919b640169ccb585ea7884e6aa9de1ec043075c65fe35
You can then 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
... .
As you can see, even though the template is only 75M, the image created is not small.
Save and load the image
You can use the docker save and docker commands to save and load the 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 image to the local testimage:lastest 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
Line 6 above is where the code is saved. There is an ES88en.tar file under /data. Then we drop the local image rmi and try to load it.
Load the image
Status after removing 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 run � it ubuntu /bin/bash
root@d8990fec2141:/# touch test
root@d8990fec2141:/# exit
0
The first line is load image, which can also be simplified as:
docker run � it ubuntu /bin/bash
root@d8990fec2141:/# touch test
root@d8990fec2141:/# exit
1
The load operation will import the image and related metadata information (including labels, etc.).
Upload of image
Finally, I would like to talk about image uploading. The management mode of image is very similar to git. You can use the command docker push to upload your local image to the warehouse and upload it to the DockerHub official warehouse by default (login is required).
docker run � it ubuntu /bin/bash
root@d8990fec2141:/# touch test
root@d8990fec2141:/# exit
2
Before uploading, I usually add a tag with my name (author information) to my image:
docker tag testimage:lastest zmc/testimage:lastest
docker pushzmc/testimage:lastest
Good for post-upload differentiation.
I think it is necessary for the operation and maintenance team, development team and a laboratory to have their own Docker warehouse, which can store the environment or system image that meets their needs and realize rapid deployment.