docker镜像 - 下载、创建镜像和导入导出镜像

时间:2022-05-24 17:20:14

实验环境

CentOS 7.5

安装并启动docker

yum install -y docker
systemctl start docker

镜像

安装镜像

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

注:对于Docker镜像来说,如果不显式地指定TAG,则默认会选择 latest标签,即下载仓库中最新版本的镜像。

  • 从Docker Hub的CentOS仓库下载一个最新的CentOS操作系统的镜像。
[root@kvm ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for docker.io/centos:latest docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for docker.io/centos:latest docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for docker.io/centos:latest

注:该命令实际上下载的就是centos:latest镜像,目前最新的7.6.1810版本的镜像。

  • 从Docker Hub的CentOS仓库下载一个指定版本的CentOS操作系统的镜像。
[root@kvm ~]# docker pull centos:7.3.1611
Trying to pull repository docker.io/library/centos ...
7.3.1611: Pulling from docker.io/library/centos
b8e0383d5f94: Pull complete
Digest: sha256:6fe8d484f2897e2ea75f496bfb6792b539baf3e56880fce3b9b6505d76e266dd
Status: Downloaded newer image for docker.io/centos:7.3.1611

查看镜像信息

列出本地主机上已有的镜像

[root@kvm ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 1e1148e4cc2c 7 weeks ago 202 MB
docker.io/centos 7.3.1611 1e20acd39495 3 months ago 192 MB
  • 各字段释义
    • REPOSITORY:来自于哪个仓库,比如centos仓库;
    • TAG:镜像的标签信息,比如7.3.1611;
    • IMAGE ID:镜像的ID号(唯一);
    • CREATED:创建时间;
    • SIZE:镜像大小。

创建镜像

创建镜像的方法主要有三种:基于已有镜像的容器创建、基于本地模板导入、基于Dockerfile创建;这里着重介绍前两种。

基于已有镜像的容器创建

  • 该方法实现创建的命令行格式
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

[root@kvm ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string 作者信息
-c, --change list 提交的时候执行Dockerfile指令 (default [])
--help 帮助信息
-m, --message string 提交信息
-p, --pause 提交期间暂停容器运行
  • 实际操作创建一个新镜像
    • 启动一个镜像,然后做修改后退出
[root@kvm ~]# docker run -t -i docker.io/centos:7.3.1611 /bin/bash
[root@90d3b762f848 /]# touch test.txt
[root@90d3b762f848 /]# exit

注:记住容器 ID为90d3b762f848。

* 此时的该容器与原centos:7.3.1611镜像相比,已经发生改变,可执行docker commit命令来提交一个新的镜像;提交时可以使用ID或名称来指定容器。

[root@kvm ~]# docker commit -m "add a file named test.txt" -a "docker new" 90d3b762f848 www.wholj.com:7.3
sha256:3a51b6661c9f9c31ec8239405535cc57f782972f6eedf8452e2d8f4bce0bc2d6
[root@kvm ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
www.wholj.com 7.3 3a51b6661c9f 7 seconds ago 192 MB
docker.io/centos latest 1e1148e4cc2c 7 weeks ago 202 MB
docker.io/centos 7.3.1611 1e20acd39495 3 months ago 192 MB

镜像ID为3a51b6661c9f的镜像即为刚新增的。

基于本地模板导入

  • 该方法实现创建的命令行格式
[root@kvm /home/tools/ISO]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help 帮助信息
-m, --message string 为此导入镜像设置提交信息
  • 实际操作创建一个新镜像
[root@kvm /home/tools/ISO]# cat centos-7-x86_64-minimal.tar.gz | docker import - www.wholj.com:7.5
sha256:4950a54ede5a5c0da704c6f74e6bcc43d440e83260b0752a926325035435a7dc
[root@kvm /home/tools/ISO]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
www.wholj.com 7.5 4950a54ede5a 10 seconds ago 435 MB
www.wholj.com 7.3 3a51b6661c9f 2 days ago 192 MB
docker.io/centos latest 1e1148e4cc2c 7 weeks ago 202 MB
docker.io/centos 7.3.1611 1e20acd39495 3 months ago 192 MB

镜像ID为4950a54ede5a的镜像即为刚新增的。

导出(save)和载入(load)镜像

导出镜像

将本地已有的镜像文件导出后可分享给其他人使用。

  • 导出镜像的命令行格式用法
docker save [OPTIONS] IMAGE [IMAGE...]

[root@kvm ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
--help 帮助信息
-o, --output string 从标准输出导出镜像到指定的文件中
  • 导出本地的docker.io/centos:latest镜像为文件centos_7.6.tar
[root@kvm ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
www.wholj.com 7.3 3a51b6661c9f About an hour ago 192 MB
docker.io/centos latest 1e1148e4cc2c 7 weeks ago 202 MB
docker.io/centos 7.3.1611 1e20acd39495 3 months ago 192 MB
[root@kvm ~]# ll
total 8
-rw-------. 1 root root 2090 Nov 30 11:33 anaconda-ks.cfg
-rw-------. 1 root root 1955 Nov 30 11:33 original-ks.cfg
[root@kvm ~]# docker save -o centos_7.6.tar docker.io/centos:latest
[root@kvm ~]# ll
total 205272
-rw-------. 1 root root 2090 Nov 30 11:33 anaconda-ks.cfg
-rw------- 1 root root 210186752 Jan 25 17:17 centos_7.6.tar
-rw-------. 1 root root 1955 Nov 30 11:33 original-ks.cfg
[root@kvm ~]#

载入镜像

将别人分享给自己的镜像导入到本地镜像库。

  • 载入镜像的命令行格式用法
docker load [OPTIONS]

[root@kvm ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
--help 帮助信息
-i, --input string 从指定tar文件读入镜像内容
-q, --quiet Suppress the load output
  • 从指定文件导入镜像文件到本地镜像库
[root@kvm ~]# docker load -i ./centos_7.6.tar
Loaded image: docker.io/centos:latest

或者:

[root@kvm ~]# docker load < ./centos_7.6.tar
Loaded image: docker.io/centos:latest

注:上述命令将导入镜像及其相关的元数据信息(包括标签等)。导入成功后,可以使用docker images命令进行查看,与原镜像一致。