• Post author:
  • Post category:docker
  • Post comments:0评论
1

1、获取镜像

docker pull [OPTIONS] NAME[:TAG]

选项 含义
-a , –all-tags 获取仓库中的所有镜像

如果不指定TAG,则默认会选择latest标签,这会下载仓库中最新版本的镜像。
例:

root@cp:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
da7391352a9b: Pull complete 
14428a6d4bcd: Pull complete 
2c2d948710f2: Pull complete 
Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
root@cp:~# docker images    # 列出镜像
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    f643c72bc252   6 weeks ago   72.9MB

  拉取指定指定版本的镜像,即指定tag。例如我要指定拉取 centos 7.6 镜像,先去官网查看 centos 镜像的tag(地址:https://hub.docker.com/_/centos),可以获取到tag为 “7.6.1810”,然后我们使用这个tag去拉取镜像即可。

root@cp:~# docker pull centos:7.6.1810
7.6.1810: Pulling from library/centos
ac9208207ada: Pull complete 
Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Status: Downloaded newer image for centos:7.6.1810
docker.io/library/centos:7.6.1810
root@cp:~# docker images
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
ubuntu       latest     f643c72bc252   6 weeks ago     72.9MB
centos       7.6.1810   f1cb7c7d58b7   22 months ago   202MB

2、列出镜像

docker images [OPTIONS] [REPOSITORY[:TAG]]

选项 含义
-a , –all 列出所有镜像文件(包括临时文件)
–digests 列出镜像的数字摘要值
-f, –filter filter 过滤列出的镜像。如:dangling=true列出无tag的镜像; before过滤出指定镜像之前创建的镜像; since过滤出指定镜像之后创建的镜像
–format string 使用Go模板打印出指定格式的输出信息
–no-trunc 列出镜像完整ID,不截断输出信息。
-q, –quiet 仅输出ID信息

例:
列出镜像

root@cp:~# docker images
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
busybox      latest     a77dce18d0ec   12 days ago     1.24MB
ubuntu       latest     f643c72bc252   6 weeks ago     72.9MB
centos       7.6.1810   f1cb7c7d58b7   22 months ago   202MB
root@cp:~# docker images centos
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
centos       7.6.1810   f1cb7c7d58b7   22 months ago   202MB
root@cp:~# docker images cen*
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
centos       7.6.1810   f1cb7c7d58b7   22 months ago   202MB

使用 -f 选项进行过滤。注:下列没有无TAG的镜像,镜像获取顺序:先centos,后ubuntu,最后busybox。

root@cp:~# docker images -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
root@cp:~# docker images -f dangling=false
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4c108a37151f        3 weeks ago         64.2MB
centos              latest              9f38484d220f        3 months ago        202MB

root@cp:~# docker images -f before=ubuntu:latest    # 过滤出在ubuntu镜像之前创建的镜像
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
centos       7.6.1810   f1cb7c7d58b7   22 months ago   202MB

root@cp:~# docker images -f since=ubuntu:latest     # 过滤出在ubuntu镜像之后创建的镜像
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
busybox      latest    a77dce18d0ec   12 days ago   1.24MB

使用 –format 控制输出指定格式。

root@cp:~# docker images --format "{{.ID}}: {{.Repository}}"
a77dce18d0ec: busybox
f643c72bc252: ubuntu
f1cb7c7d58b7: centos

可以使用 table 指令,显示列标题。

root@cp:~# docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID       REPOSITORY   TAG
a77dce18d0ec   busybox      latest
f643c72bc252   ubuntu       latest
f1cb7c7d58b7   centos       7.6.1810

使用 -q 选项仅输出ID信息。

root@cp:~# docker images -q
a77dce18d0ec
f643c72bc252
f1cb7c7d58b7

可以组合起来进行批量删除,也可以和 -f 选项一起组合使用。

root@cp:~# docker rmi `docker images -q`
Untagged: busybox:latest
Untagged: busybox@sha256:49dae530fd5fee674a6b0d3da89a380fc93746095e7eca0f1b70188a95fd5d71
Deleted: sha256:a77dce18d0ecb0c1f368e336528ab8054567a9055269c07c0169cba15aec0291
Deleted: sha256:1dad141bdb55cb5378a5cc3f4e81c10af14b74db9e861e505a3e4f81d99651bf
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Deleted: sha256:f643c72bc25212974c16f3348b3a898b1ec1eb13ec1539e10a103e6e217eb2f1
Deleted: sha256:9386795d450ce06c6819c8bc5eff8daa71d47ccb9f9fb8d49fe1ccfb5fb3edbe
Deleted: sha256:3779241fda7b1caf03964626c3503e930f2f19a5ffaba6f4b4ad21fd38df3b6b
Deleted: sha256:bacd3af13903e13a43fe87b6944acd1ff21024132aad6e74b4452d984fb1a99a
Untagged: centos:7.6.1810
Untagged: centos@sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Deleted: sha256:f1cb7c7d58b73eac859c395882eec49d50651244e342cd6c68a5c7809785f427
Deleted: sha256:89169d87dbe2b72ba42bfbb3579c957322baca28e03a1e558076542a1c1b2b4a
root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

3、添加镜像标签

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

docker tag 命令添加标签实际是起到类似链接的作用,镜像还是同一个镜像。
当要使用镜像id的时候,一般可以使用该id的前若干个字符组成的可区分串来代替完整的id。
例:

root@cp:~# docker tag ubuntu:latest ubuntu-test:v1.0
root@cp:~# docker tag 300e centos-test:v1.0
root@cp:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
centos-test   v1.0      300e315adb2f   4 weeks ago   209MB
centos        latest    300e315adb2f   4 weeks ago   209MB
ubuntu-test   v1.0      f643c72bc252   6 weeks ago   72.9MB
ubuntu        latest    f643c72bc252   6 weeks ago   72.9MB

4、查看镜像详细信息

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

选项 含义
-f, –format string 使用Go模板打印出指定格式的输出信息

例:

root@cp:~# docker inspect centos:latest
[
    {
        "Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "RepoTags": [
            "centos-test:v1.0",
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-12-08T00:22:53.076477777Z",
        "Container": "395e0bfa7301f73bc994efe15099ea56b8836c608dd32614ac5ae279976d33e4",
        "ContainerConfig": {
            "Hostname": "395e0bfa7301",
......

前面命令返回的是一个 JSON 格式的信息,如果只需要查看其中某一项信息时,可以使用 -f 选项来指定。

root@cp:~# docker inspect -f "{{.Created}}  {{.RepoTags}}" 300e
2020-12-08T00:22:53.076477777Z  [centos-test:v1.0 centos:latest]

注:格式:"{{.xx.xx}}"
    一级属性 {{.属性}}
    二级属性 {{.属性.属性}}
    三级属性 {{.属性.属性.属性}}

例如:           
root@cp:~# docker inspect -f "{{.ContainerConfig.Hostname}}  {{.RepoTags}}" 300e
395e0bfa7301  [centos-test:v1.0 centos:latest]

5、搜索镜像

docker search [OPTIONS] TERM

选项 含义
-f, –filter filter 过滤输出内容
–format string 使用Go模板打印出指定格式的输出信息
–limit int 限制输出结果数,默认为25个
–no-trunc 不截断输出结果,可以查看完整的镜像描述信息

例:
搜索ubuntu相关镜像。

root@cp:~# docker search ubuntu
NAME                              DESCRIPTION                                     STARS    OFFICIAL    AUTOMATED
ubuntu                            Ubuntu is a Debian-based Linux operating sys…   9716     [OK]        
dorowu/ubuntu-desktop-lxde-vnc    Docker image to provide HTML5 VNC interface …   320                  [OK]
rastasheep/ubuntu-sshd            Dockerized SSH service, built on top of offi…   224                  [OK]
consol/ubuntu-xfce-vnc            Ubuntu container with "headless" VNC session…   183                  [OK]
ubuntu-upstart                    Upstart is an event-based replacement for th…   99       [OK]        
ansible/ubuntu14.04-ansible       Ubuntu 14.04 LTS with ansible                   97                   [OK]
......

输出信息说明:
  NAME:镜像仓库。
  DESCRIPTION:镜像描述信息。
  STARS:镜像收藏数。
  OFFICIAL:是否为docker官方发布的镜像。
  AUTOMATED:是否为自动化构建的镜像。

  使用 -f 选项搜索官方提供的带ubuntu关键字的镜像,目前支持过滤选项有"is-automated="、"is-official="、"stars="三个。

root@cp:~# docker search -f "is-official=true" ubuntu
NAME                 DESCRIPTION                                     STARS    OFFICIAL    AUTOMATED
ubuntu               Ubuntu is a Debian-based Linux operating sys…   9716     [OK]                
ubuntu-upstart       Upstart is an event-based replacement for th…   99       [OK]                
neurodebian          NeuroDebian provides neuroscience research s…   57       [OK]                
ubuntu-debootstrap   debootstrap --variant=minbase --components=m…   40       [OK]                

搜索收藏数至少100的带centos关键字的镜像。

root@cp:~# docker search -f "stars=100" centos
NAME                      DESCRIPTION                                     STARS    OFFICIAL    AUTOMATED
centos                    The official build of CentOS.                   5443     [OK]                
ansible/centos7-ansible   Ansible on Centos7                              122      [OK]
jdeathe/centos-ssh        CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   110                  [OK]

使用 –format 选项打印出指定格式的搜索结果列表,同时使用 –limit 选项限制输出结果数为5个。

root@cp:~# docker search --format "table {{.Name}}\t{{.Description}}" --limit 5 centos
NAME                          DESCRIPTION
centos                        The official build of CentOS.
jdeathe/centos-ssh            OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …
pivotaldata/centos-gpdb-dev   CentOS image for GPDB development. Tag names…
pivotaldata/centos            Base centos, freshened up a little with a Do…
pivotaldata/centos-mingw      Using the mingw toolchain to cross-compile t…

6、查看镜像历史

docker history [OPTIONS] IMAGE

选项 含义
–format string 使用Go模板打印出指定格式的输出信息
-H, –human 以可读的格式打印镜像大小和日期,默认为true
–no-trunc 不截断输出信息,列出完整的提交记录。
-q, –quiet 仅列出提交记录ID。

镜像文件是由多个层组成的,history可以列出各层的构建信息。
例:

root@cp:~# docker history ubuntu:latest
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
f643c72bc252   6 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      6 weeks ago   /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B        
<missing>      6 weeks ago   /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B        
<missing>      6 weeks ago   /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B      
<missing>      6 weeks ago   /bin/sh -c #(nop) ADD file:4f15c4475fbafb3fe…   72.9MB   

7、删除镜像

docker rmi [OPTIONS] IMAGE [IMAGE...]

选项 含义
-f, –force 强制删除镜像

  当有用该镜像创建的容器存在时,镜像是无法被删除,除非添加-f选项,通常是先删除使用该镜像的容器,再删除镜像。
  例:

root@cp:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
centos        latest    300e315adb2f   4 weeks ago   209MB
ubuntu        latest    f643c72bc252   6 weeks ago   72.9MB
root@cp:~# docker rmi ubuntu    # 一般不带tag,默认都是latest
root@cp:~# docker rmi 300e      # 可以用"image id"作为删除条件
root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE

  以 imgae id 作为删除条件时:删除有 tag 的镜像,需要先删除 tag 才能删除镜像。当然 -f 选项强制删除也是可以,但是会将同属一个 IMAGE ID 的镜像全删除。

root@cp:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
centos-test   v1.0      300e315adb2f   4 weeks ago   209MB
centos        latest    300e315adb2f   4 weeks ago   209MB
root@cp:~# docker rmi 300e
Error response from daemon: conflict: unable to delete 300e315adb2f (must be forced) - image is referenced in multiple repositories
root@cp:~# docker rmi -f 300e
Untagged: centos-test:v1.0
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859

8、清理镜像

docker image prune [OPTIONS]

选项 含义
-a, –all 删除所有无用镜像,不仅仅是临时镜像
–filter filter 只清理符合过滤条件的镜像
-f, –force 强制删除进行,而不进行提示确认

  这个命令用来清理系统中遗留的一些临时镜像文件,像一些 REPOSITORY 和 TAG 为 none 的镜像就是临时镜像。加 -a 选项时注意,什么是无用就是没有被用到的镜像,命令执行会把所有没有用到的镜像全部删除。

例:
清理系统中遗留的一些临时镜像文件。

root@cp:~# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

使用 -a 和 -f 选项删除所有没有用到的镜像。

root@cp:~# docker image prune -af
Deleted Images:
untagged: busybox:latest
untagged: busybox@sha256:49dae530fd5fee674a6b0d3da89a380fc93746095e7eca0f1b70188a95fd5d71
deleted: sha256:a77dce18d0ecb0c1f368e336528ab8054567a9055269c07c0169cba15aec0291
deleted: sha256:1dad141bdb55cb5378a5cc3f4e81c10af14b74db9e861e505a3e4f81d99651bf
untagged: alpine:latest
untagged: alpine@sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436
deleted: sha256:389fef7118515c70fd6c0e0d50bb75669942ea722ccb976507d7b087e54d5a23
deleted: sha256:777b2c648970480f50f5b4d0af8f9a8ea798eea43dbcf40ce4a8c7118736bdcf

Total reclaimed space: 6.813MB

  使用 –filter 选项来进行过滤清理,目前支持过滤选项有:"label="、"label!=" 、"until="。了解即可表示没用过,这里以 "until=" 为例:

root@cp:~# docker image prune -f --filter "until=2021-01-11"
Total reclaimed space: 0B

root@cp:~# docker image prune -f --filter "until=2021-01-11T17:00:30"
Total reclaimed space: 0B

9、创建镜像

(1)基于已有容器创建

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

选项 含义
-a, –author string 作者信息
-c, –change list 应用Dockerfile指令到将要创建的镜像中
-m, –message string 提交时的说明文字
-p, –pause 提交时暂停容器运行,默认是会暂停,不想暂停可以改成false

例:
先创建一个容器,在容器中进行一些操作,再用commit命令创建提交一个新镜像。

root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
busybox      latest    a77dce18d0ec   12 days ago   1.24MB
root@cp:~# docker run -it a77
/ # touch test
/ # ls -l test
-rw-r--r--    1 root     root             0 Jan 11 09:11 test
/ # exit
root@cp:~# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                     PORTS     NAMES
215a212fc5c8   a77       "sh"      37 seconds ago   Exited (0) 8 seconds ago             happy_mccarthy

root@cp:~# docker commit -a "cp" -m "a new image" 215a test:0.1
sha256:111c90ca91d32b83c5769c6f8a69a682973105561732be5c4e11386377de60f7
root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test         0.1       111c90ca91d3   11 seconds ago   1.24MB
busybox      latest    a77dce18d0ec   12 days ago      1.24MB

此时用新创建生成的镜像去创建一个容器,就可以看到前面创建的 test 文件了。

root@cp:~# docker run -it test:0.1
/ # ls -l test
-rw-r--r--    1 root     root             0 Jan 11 09:11 test

(2)使用Dockerfile构建

这个比较重要内容较多,后面单独为一篇进行介绍。

10、导出镜像

docker save [OPTIONS] IMAGE [IMAGE...]

选项 含义
-o, –output string 输出到的文件

例:
将 test:0.1 导出保存为 test_0.1.tar 文 件 。

root@cp:~# docker save -o test_0.1.tar test:0.1
root@cp:~# ls
test_0.1.tar

同时也可以保存一个或多个镜像到压缩文件。

root@cp:~# docker save -o images.tar busybox:latest alpine:latest 
root@cp:~# ls
images.tar  test_0.1.tar

11、导入镜像

docker load [OPTIONS]

选项 含义
-i, –input string 从 tar 归档文件中读取,而不是 STDIN
-q, –quiet 精简输出信息

例:
将之前的镜像全部删除,用 load 将前面导出的 test:0.1 镜像导入。

root@cp:~# docker rmi `docker images -q`
root@cp:~# docker load -i test_0.1.tar
1dad141bdb55: Loading layer  1.455MB/1.455MB
905572700c21: Loading layer  3.072kB/3.072kB
Loaded image: test:0.1
root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test         0.1       111c90ca91d3   16 minutes ago   1.24MB

再将前面导出的 images.tar 导入,里面包含了 busybox:latest 和 alpine:latest 两个镜像。

root@cp:~# docker load -i images.tar 
777b2c648970: Loading layer  5.848MB/5.848MB
Loaded image: alpine:latest
Loaded image: busybox:latest
root@cp:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test         0.1       111c90ca91d3   16 minutes ago   1.24MB
busybox      latest    a77dce18d0ec   12 days ago      1.24MB
alpine       latest    389fef711851   3 weeks ago      5.58MB

  注:当然导入导出镜像也可以使用 export 和 import 命令,但是这两个指令通常是针对容器,具体有什么不同,大家自行尝试吧。save 导出的镜像文件,可以由 load 导入也可以由 import 导入,但是 export 导出的只能由 import 导入。

12.上传镜像

docker push [OPTIONS] NAME[:TAG]

上传自制镜像时,默认是上传到docker.io,即docker官方仓库。上传前需要先登录。

root@cp:~# docker push cp/test:0.1
The push refers to repository [docker.io/cp/test]
905572700c21: Preparing 
1dad141bdb55: Preparing 
denied: requested access to the resource is denied
root@cp:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: cuipengcp
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

一般按照:Username/Repository:Tag 格式给镜像打标签,再上传。
Username:你的用户名
Repository:仓库名。上传前建议先去 hub.docker 创建一个仓库,当然不创建仓库,上传时默认会自动创建(默认为公共)。

root@cp:~# docker tag test:0.1 cuipengcp/test:0.1
root@cp:~# docker push cuipengcp/test:0.1
The push refers to repository [docker.io/cuipengcp/test]
905572700c21: Pushed 
1dad141bdb55: Pushed 
0.1: digest: sha256:eb4e1d91ca9e7995bdff6b4d95da56b9cb1f1cd5c5813187ae4e837dc3c74ec7 size: 734

  除了docker官方,我们也可以使用阿里云镜像仓库,打开阿里云容器镜像服务,创建一个仓库,然后点击仓库管理,可以看到如何推送镜像到刚创建的仓库,这里就不演示了。
  我们还可以自建私有仓库,然后将镜像推送到自建私有仓库中。如何配置私有仓库不在本文范围内,这里就演示下推送。

root@cp:~# docker tag test:0.1 192.168.200.20:5000/cp/test:0.1
root@cp:~# docker push 192.168.200.20:5000/cp/test:0.1
The push refers to a repository [192.168.200.20:5000/cp/test]
9b728062fb6d: Pushed 
481c807467a1: Pushed 
a049b9c716b3: Pushed 
d57f828d06ea: Pushed 
011b303988d2: Pushed 
0.1: digest: sha256:2fdff97736e7dd785a91ccddb6c2df4ad6664f7032e3d8f28f56d94f699a58f9 size: 1363



参考书籍:《Docker技术入门与实战》 作者: 杨保华 / 戴王剑 / 曹亚仑

1

发表评论

验证码: − 5 = 5