专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Docker实战Docker常用命令详解之镜像篇

镜像命令

docker images

列出所有的镜像信息

  • 帮助
[root@t32 ~]# docker images --help

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

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --help            Print usage
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

  • 常用参数介绍

1、 -- filter:过滤器,目前支持以下过滤条件

 *  dangling悬空:为布尔类型,dangling镜像就是images命令中出现的,REPOSITORY 和 TAG 都显示为 的镜像。
 *  label (label= or label==)
 *  before (\[:\], ![80\_1.png][80_1.png] or [image@digest][image_digest]) 在给出的镜像之前创建的镜像
 *  since (\[:\], ![80\_2.png][80_2.png] or [image@digest][image_digest]) 在给出的镜像之后创建的镜像
 *  reference (pattern of an image reference) 正则表达式匹配
  • 例子
### 批量删除dangling镜像
[root@t32 ~]# docker rmi $(docker images -q --filter "dangling=true" )

docker rmi

删除镜像

  • 帮助
[root@t32 ~]# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --help       Print usage
      --no-prune   Do not delete untagged parents

  • 常用参数详解
    • --force 强制删除

docker search

从docker hub中查找镜像

  • 帮助
[root@t32 ~]# docker search --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --help            Print usage
      --limit int       Max number of search results (default 25)
      --no-index        Don't truncate output
      --no-trunc        Don't truncate output

  • 常用参数详解

1、 --filter:当前支持的过滤器有:

 *  stars (int):镜像在docker hub中的星数
 *  is-automated (boolean - true or false):是否为自动化
 *  is-official (boolean - true or false) :是否为官方
  • 例子
[root@t32 ~]# docker search centos --filter stars=1000
INDEX       NAME               DESCRIPTION                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos   The official build of CentOS.   5855      [OK] 

docker tag

给现有的镜像重新打tag

  • 帮助
[root@t32 ~]# docker tag --help

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

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Options:
      --help   Print usage

  • 例子
[root@t32 ~]# docker tag docker.io/centos:latest docker.io/centos:test
[root@t32 ~]# docker images
REPOSITORY                             TAG                   IMAGE ID            CREATED             SIZE
docker.io/centos                       latest                470671670cac        6 weeks ago         237 MB
docker.io/centos                       test                  470671670cac        6 weeks ago         237 MB

docker history

显示镜像的历史,显示镜像每层的变更内容,可用于查看dockerfile的大体内容

  • 帮助
[root@t32 ~]# docker history --help

Usage:  docker history [OPTIONS] IMAGE

Show the history of an image

Options:
      --help       Print usage
  -H, --human      Print sizes and dates in human readable format (default true)
      --no-trunc   Don't truncate output
  -q, --quiet      Only show numeric IDs

  • 例子
[root@t32 ~]# docker history 470671670cac
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
470671670cac        6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
<missing>           6 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema....   0 B                 
<missing>           7 weeks ago         /bin/sh -c #(nop) ADD file:aa54047c80ba300...   237 MB              
You have mail in /var/spool/mail/root
[root@t32 ~]# docker history --no-trunc 470671670cac
IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                                                                                                                                                                                                                  SIZE                COMMENT
sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee   6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]                                                                                                                                                                                                                                                                                                                                                                        0 B                 
<missing>                                                                 6 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20200114 org.opencontainers.image.title=CentOS Base Image org.opencontainers.image.vendor=CentOS org.opencontainers.image.licenses=GPL-2.0-only org.opencontainers.image.created=2020-01-14 00:00:00-08:00   0 B                 
<missing>                                                                 7 weeks ago         /bin/sh -c #(nop) ADD file:aa54047c80ba30064fe59adf4c978a929f38480be77af9ac644074bd5288ef18 in /

docker build

从Dockerfile中创建镜像。Dockerfile的具体编写可参考我的另外一篇文章:Docker专题—Dockerfile编写规范

  • 帮助
[root@t32 ~]#  docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --build-arg list             Set build-time variables (default [])
      --cache-from stringSlice     Images to consider as cache sources
      --cgroup-parent string       Optional parent cgroup for the container
      --compress                   Compress the build context using gzip
      --cpu-period int             Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int              Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int             CPU shares (relative weight)
      --cpuset-cpus string         CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string         MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust      Skip image verification (default true)
  -f, --file string                Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                   Always remove intermediate containers
      --help                       Print usage
      --isolation string           Container isolation technology
      --label list                 Set metadata for an image (default [])
  -m, --memory string              Memory limit
      --memory-swap string         Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string             Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                   Do not use cache when building the image
      --pull                       Always attempt to pull a newer version of the image
  -q, --quiet                      Suppress the build output and print image ID on success
      --rm                         Remove intermediate containers after a successful build (default true)
      --security-opt stringSlice   Security options
      --shm-size string            Size of /dev/shm, default value is 64MB
  -t, --tag list                   Name and optionally a tag in the 'name:tag' format (default [])
      --ulimit ulimit              Ulimit options (default [])
  -v, --volume list                Set build-time bind mounts (default [])

  • 常用参数详解
    • -t,–tag: 给打包的镜像命名
    • --label: 设置镜像的标签,方便后期查找
    • --no-cache:不使用cache,每一次创建都是从新开始
  • 例子
[root@t32 docker]# docker build -t test:v1.0 --label test=v1.0 --no-cache .
## 省略中间构建过程
[root@t32 docker]# docker images --filter label=test=v1.0
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                v1.0                29dc1b36db54        22 seconds ago      641 MB

docker login/docker push /docker logout/docker pull

  • docker login:登陆镜像仓库
  • docker logout:退出奖项仓库
  • docker push:上传本地镜像到仓库
  • docker pull:从仓库中下载镜像 具体帮助文档可以使用--help参数进行查看,参数相对简单,不再赘述。
  • 例子

将以上build完成的镜像上传到私有镜像仓库(Harbor),具体Harbor的使用和安装情况可参考我的另外一篇文章:Kubernetes专栏—Kubernetes与Harbor集成详解

[root@t32 docker]# docker tag test:v1.0 hub.xxx.io/zhangjx/test:v1.0 
[root@t32 docker]# docker login hub.xxx.io
Username (admin): admin
Password: 
Login Succeeded
[root@t32 docker]# docker push hub.xxx.io/zhangjx/test:v1.0 
The push refers to a repository [hub.xxx.io/zhangjx/test]
46516138b103: Pushed 
84ed16ce580f: Pushed 
fc41e9edd657: Pushed 
7c2460bb5175: Pushed 
50137a667658: Pushed 
bbb2ed441221: Pushed 
5f240e131c4d: Pushed 
528a545aaea3: Pushed 
c25c5f41b31e: Pushed 
ecfc116e3bdb: Pushed 
de6df59c2c39: Pushed 
475a4e226ad7: Pushed 
e542bb3ae2b4: Pushed 
e17e7b9510a0: Pushed 
9ba309a8bd34: Pushed 
cf47f2cd7a31: Pushed 
e080cd3255f0: Pushed 
5c4c0b386f1b: Pushed 
ba9ec208ce9b: Pushed 
v1.0: digest: sha256:1fa8f871ca5a5561780fc7d12add328bedce56936261e801bd97824ef036e89e size: 5141

80_3.png

[root@t32 docker]# docker rmi hub.xxx.io/zhangjx/test:v1.0 
Untagged: hub.xxx.io/zhangjx/test:v1.0
Untagged: hub.xxx.io/zhangjx/test@sha256:1fa8f871ca5a5561780fc7d12add328bedce56936261e801bd97824ef036e89e
[root@t32 docker]# docker pull hub.xxx.io/zhangjx/test:v1.0 
Trying to pull repository hub.xxx.io/zhangjx/test ... 
sha256:1fa8f871ca5a5561780fc7d12add328bedce56936261e801bd97824ef036e89e: Pulling from hub.xxx.io/zhangjx/test
Digest: sha256:1fa8f871ca5a5561780fc7d12add328bedce56936261e801bd97824ef036e89e
Status: Downloaded newer image for hub.xxx.io/zhangjx/test:v1.0
[root@t32 docker]# docker logout
Not logged in to https://index.docker.io/v1/
[root@t32 docker]# docker logout hub.xxx.io
Removing login credentials for hub.xxx.io
[root@t32 docker]# 

docker save/docker load

  • docker save:将镜像保存在磁盘上
  • docker load:将磁盘上的镜像文件导入到docker中 具体帮助文档可以使用--help参数进行查看,参数相对简单,不再赘述。
  • 例子
[root@t32 docker]# docker save test:v1.0 -o test#v1.0
[root@t32 docker]# ls -al test#v1.0
-rw-------. 1 root root 652762624 Mar  4 15:44 test#v1.0
[root@t32 docker]# docker rmi 29dc1b36db54 -f 
Untagged: hub.xxx.io/zhangjx/test:v1.0
Untagged: test:v1.0
Deleted: sha256:29dc1b36db5422493bb9a5b88d76f67284c3c124366e8989a28593477ecce962
Deleted: sha256:aab1d54bc8cb5b4fbfa6824da33feae368414a0830cef140d2797578732baaca
Deleted: sha256:8a681fbd9074944ce6feb1f3af38ca1ff5232f53b53a3e94b22507bec59398ca
Deleted: sha256:ccb1b32a2925bb963f532fd8483f8406683cbfc99eb032bebd39f4a3b484c8d5
Deleted: sha256:246b59c0e12940986eec6488acb5c901b7e5467a3f84c2e3321a1d57af859e67
Deleted: sha256:ae85dc57914f8b71618ea5970cdad270ffae7550eed39b4bad9995fa1072fe98
Deleted: sha256:18f9e329b9b2101644709db72e30447786baa9b3c3860a0ace06a47b9e74d80e
Deleted: sha256:0924e9810301021428b91508699f77565f2d56ebf19e3529922641b5575b0479
Deleted: sha256:804f07b388f4cd1f3d74c9f07de1229e0b2e7dc5e8d5ba7c40dcfffe53dd3f23
Deleted: sha256:dc915ec6c3f75aea8a6b9afde023d473944c480658727a7cbd58539b5b8d78eb
Deleted: sha256:be09a9a1444af130fb77b004408cc98d50ceba4ac2c08df637b0bd977393f69b
Deleted: sha256:7ebcdcd05b4e701f5d7236987c948fffd19df962b270aa5f852f2e536e11efbf
Deleted: sha256:172e5f925adb622fb234b6c643f2d227f4d69a5ea21d33c43a80cf123e433cf6
Deleted: sha256:665ea71a411b4adcef48e49c386fb6b52e3a3c576209afdd4a190ad59b0f865c
Deleted: sha256:e0b6fbea45c0dbee67e337b66c7b485d8264d31f10c0cf6108335e1e3788e112
[root@t32 docker]# docker load -i test#v1.0 
bbb2ed441221: Loading layer [==================================================>]  5.12 kB/5.12 kB
50137a667658: Loading layer [==================================================>]  2.56 kB/2.56 kB
7c2460bb5175: Loading layer [==================================================>] 368.9 MB/368.9 MB
fc41e9edd657: Loading layer [==================================================>] 289.3 kB/289.3 kB
84ed16ce580f: Loading layer [==================================================>]  2.92 MB/2.92 MB
46516138b103: Loading layer [==================================================>] 177.2 kB/177.2 kB
Loaded image: test:v1.0

docker commit

从运行的容器中创建一个新的镜像

  • 帮助
[root@t32 ~]# docker commit --help

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

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image (default [])
      --help             Print usage
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

  • 例子
[root@t32 ~]# docker run -itd  --label test=one  his_image_sharpen:latest bash 
c4bf11b54dbfaa0b13528dcec7275970decf99f17f71584535cb989a99832111
[root@t32 ~]# docker commit -a zhangjx -m "commit new image" test:v2.0
Error response from daemon: No such container: test:v2.0
[root@t32 ~]# docker commit -a zhangjx -m "commit new image" c4bf11b54dbfaa test:v2.0
sha256:4796d9d2b9bd66578a77f71dd96cd5ae727436f11a6307767ee2cb604dc8674a
[root@t32 ~]# docker images 
REPOSITORY                                TAG                   IMAGE ID            CREATED             SIZE
test                                      v2.0                  4796d9d2b9bd        14 seconds ago      641 MB

文章永久链接:https://tech.souyunku.com/44235

未经允许不得转载:搜云库技术团队 » Docker实战Docker常用命令详解之镜像篇

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们