Day03 使用Docker镜像

Day03 使用Docker镜像

docker 镜像是 docker 中三大概念之一,其主要作用是作为启动容器的模板。

Docker镜像常用命令

docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search 搜索镜像
docker pull 下载镜像 docker image pull
docker rmi 删除镜像 docker image rm

2.1 获取镜像

​ 镜像是运行容器的模板,官方 Docker Hub 仓库已经提供了许多镜像共开发者使用。如果我们需要获取某 个镜像则可以去 docker 仓库下载所需的镜像。

​ 下载镜像的格式:docker pull [image_name][:image_version]

docker pull nginx:1.17

2.2 查看镜像信息

镜像主要包括镜像文件、镜像 tag 以及镜像详细信息等等。

docker images  /or   docker image ls

lNxMfEdrjc8oAOi

列举信息中,可以看到几个字段信息:

镜像来源(REPOSITORY):来自哪个仓库,默认来自:hub.docker.com

镜像标签(TAG):比喻 1.17、1000-teach-2020-10-28-13-40-27

镜像 ID(IMAGE ID):例如 22fdec3d9a6d

镜像创建时间(CREATED):例如:3 weeks ago

镜像大小(SIZE):127MB

其中镜像的 ID 信息十分重要,它唯一标识了镜像(全球唯一)。在使用镜像 ID 的时候,一般可以使用该 ID 的前若干个 字符组成的可区分串来替代完整的 ID。

2.2.1dokcer images -a

列出所有(包括临时文件)镜像文件

2.2.2docker images --digests=false|true

列出镜像的数字摘要值

2.2.3docker images -q

仅显示 ID 信息

2.3 为镜像添加 tag

为了方便后续工作中使用特定的镜像,还可以使用 docker tag 命令来为本地的镜像添加标签。

docker tag [名称]:版本号 [自定义名称]:[版本号]

docker tag hello-world:latest new_hello-world:new
docker images
------------------------------------------------------------------------
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB
new_hello-world     new                 bf756fb1ae65        11 months ago       13.3kB

2.4 使用 inspect 命令查看详细信息

docker inspect hello-world
------------------------------------------------------------------------
[
    {
    
    
        "Id": "sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b",
        "RepoTags": [
            "hello-world:latest",
            "new_hello-world:new"
        ],
        "RepoDigests": [
            "hello-world@sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323"
        ],
        "Parent": "",

2.5 使用 history 命令查看镜像历史

history hello-world
------------------------------------------------------------------------
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
bf756fb1ae65        11 months ago       /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
<missing>           11 months ago       /bin/sh -c #(nop) COPY file:7bf12aab75c3867a…   13.3kB  

2.6 搜索镜像

在docker中搜索镜像主要使用Search子命令,默认只搜索Docker Hub官方镜像仓库中的镜像。其语法为docker search [option] keyword。支持的命令选项主要包括:

2.6.1 -f : 过滤输出内容

# 搜索官方提供的带有 Redis 关键字的镜像
docker search -f is-official=true redis
------------------------------------------------------------------------
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   8792                [OK]   

# 搜索被收藏超过 8000 个的并且关键词包括 Redis 的镜像
docker search -f stars=8000 redis
------------------------------------------------------------------------
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   8792                [OK]   

2.6.2 --limit: 限制输出结果

# 搜索星星书大于3的redis 并且只显示3条
docker search -f stars=3 --limit 3  redis
------------------------------------------------------------------------
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that…   8792                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                      168                                     [OK]
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   47                                      [OK]

2.6.3 --no-trunc: 不截断输出结果

docker search -f stars=3 --limit 3 --no-trunc redis
------------------------------------------------------------------------
NAME                             DESCRIPTION                                                                          STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that functions as a data structure server.   8792                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                                                           168                                     [OK]
rediscommander/redis-commander   Alpine image for redis-commander - Redis

2.6.4 输出参数释义

  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
  • AUTOMATED: 自动构建。

2.7 删除和清理镜像

在 docker 中,删除镜像主要使用 rmi 子命令,清理镜像主要使用 prune 子命令。

rmi = rm image

2.7.1使用标签删除镜像

使用 docker rmi 或 docker image rm 命令可以删除镜像,命令格式为 docker rmi image

-f : 强制删除镜像。

docker images
------------------------------------------------------------------------
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 ae0658fdbad5        9 days ago          449MB
redis               latest              74d107221092        11 days ago         104MB
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB
new_hello-world     new                 bf756fb1ae65        11 months ago       13.3kB
nginx               1.12                4037a5562b03        2 years ago         108MB

docker rmi new_hello-world:new
------------------------------------------------------------------------
Untagged: new_hello-world:new

2.7.2 清理镜像

使用一段时间之后,docker 会产生很多临时文件,以及一些没有被使用的镜像,我们可以通过 docker image prune 命令来进行清理。

参数: -a :删除所有无用的镜像,不光是临时镜像。

​ -f :强制删除镜像,而不进行提示。

好家伙,直呼好家伙!

docker images
------------------------------------------------------------------------
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 ae0658fdbad5        9 days ago          449MB
redis               latest              74d107221092        12 days ago         104MB
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB
nginx               1.12                4037a5562b03        2 years ago         108MB
docker image prune -a
------------------------------------------------------------------------
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:1.12
untagged: nginx@sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
deleted: sha256:4037a5562b030fd80ec889bb885405587a52cfef898ffb7402649005dfda75ff
deleted: sha256:8b8d76e24d92203bae8c1184f5c94f607c92e87ff5f836780f0d1301e4073111
deleted: sha256:19f98a1ae65cc06a1f35e86ff4a7894c7f8fab2313a35503166233d470ff3752
deleted: sha256:d626a8ad97a1f9c1f2c4db3814751ada64f60aed927764a3f994fcd88363b659
untagged: mysql:5.7
untagged: mysql@sha256:8e2004f9fe43df06c3030090f593021a5f283d028b5ed5765cc24236c2c4d88e
deleted: sha256:ae0658fdbad5fb1c9413c998d8a573eeb5d16713463992005029c591e6400d02
deleted: sha256:a2cf831f4221764f4484ff0df961b54f1f949ed78220de1b24046843c55ac40f
deleted: sha256:0a7adcc95a91b1ec2beab283e0bfce5ccd6df590bd5a5e894954fcf27571e7f5
deleted: sha256:0fae465cbacf7c99aa90bc286689bc88a35d086f37fd931e03966d312d5dfb10
deleted: sha256:23af125b9e54a94c064bdfacc2414b1c8fba288aff48308e8117beb08b38cb19
deleted: sha256:c37f414ac8d2b5e5d39f159a6dffd30b279c1268f30186cee5da721e451726ea
deleted: sha256:955b3c214bccf3ee2a7930768137fd7ed6a72677334be67a07c78a622abd318a
deleted: sha256:a2e35a0fdb20100365e2fb26c65357fcf926ac7990bf9074a51cbac5a8358d7e
deleted: sha256:8c3a028fc66f360ce6ce6c206786df68fac4c24257474cbe4f67eda0ac21efd6
deleted: sha256:0a6d37fabaceb4faa555e729a7d97cb6ee193cb97789a213907a3d3c156d7e35
deleted: sha256:579519c51de1afe1e29d284b1741af239a307975197cf6ce213a70068d923231
untagged: redis:latest
untagged: redis@sha256:5b98e32b58cdbf9f6b6f77072c4915d5ebec43912114031f37fa5fa25b032489
deleted: sha256:74d107221092875724ddb06821416295773bee553bbaf8d888ababe9be7b947f
deleted: sha256:d951b383737320b4e1ac7f9bb63f3919bcf25363ccb59fbb52a41e45ba70ffdd
deleted: sha256:d3b2581a1c92973ee9a41fc00e5628047ce7e644a66240fb859b38831bd525b4
deleted: sha256:a447231da503a58432b4d7409980139206fdf398fbde189d8a7229dd0663f472
deleted: sha256:f786204ca260bcaef3d47ecad10821878028239072c65ceb2a1f212f275f9367
deleted: sha256:b68afce5f52461f79be59806be00e43ea95152a0358b8dc5de9ac3f486a70d7e
deleted: sha256:f5600c6330da7bb112776ba067a32a9c20842d6ecc8ee3289f1a713b644092f8

Total reclaimed space: 591.9MB

docker images
------------------------------------------------------------------------
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB

2.8 docker pull 下载镜像

下载镜像 docker pull 镜像名[:tag]

# 下载镜像 docker pull 镜像名[:tag]
➜ ~ docker pull tomcat:8
8: Pulling from library/tomcat #如果不写tag,默认就是latest
90fe46dd8199: Already exists #分层下载: docker image 的核心 联合文件系统
35a4f1977689: Already exists
bbc37f14aded: Already exists
74e27dc593d4: Already exists
93a01fbfad7f: Already exists
1478df405869: Pull complete
64f0dd11682b: Pull complete
68ff4e050d11: Pull complete
f576086003cf: Pull complete
3b72593ce10e: Pull complete
Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df640a027
# 签名 防伪
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8 #真实地址
#等价于
docker pull tomcat:8
docker pull docker.io/library/tomcat:8  # 直接从官方拉下来 相当于把前面的简化了,也可以自己加上地址

猜你喜欢

转载自blog.csdn.net/A1L__/article/details/110438826
今日推荐