Day03 Use Docker image

Day03 Use Docker image

Docker image is one of the three major concepts in docker, and its main function is to serve as a template for starting containers.

Common commands for Docker images

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

2.1 Get mirror

​ An image is a template for running containers. The official Docker Hub repository has provided many images for developers to use. If we need to obtain a certain image, we can go to the docker warehouse to download the required image.

​ Download image format:docker pull [image_name][:image_version]

docker pull nginx:1.17

2.2 View image information

Mirroring mainly includes mirroring files, mirroring tags, mirroring details, etc.

docker images  /or   docker image ls

lNxMfEdrjc8oAOi

In the listing information, you can see several fields of information:

Mirror source (REPOSITORY) : Which warehouse comes from, the default comes from: hub.docker.com

Mirror tag (TAG) : metaphor 1.17, 1000-teach-2020-10-28-13-40-27

Image ID (IMAGE ID) : for example 22fdec3d9a6d

Mirror creation time (CREATED) : For example: 3 weeks ago

Image size (SIZE) : 127MB

The ID information of the mirror is very important, and it uniquely identifies the mirror (globally unique). When using a mirror ID, you can generally use a distinguishable string composed of the first several characters of the ID to replace the complete ID.

2.2.1dokcer images -a

List all (including temporary files) image files

2.2.2docker images --digests=false|true

List the numeric summary value of the mirror

2.2.3docker images -q

Only display ID information

2.3 Add tag to mirror

In order to facilitate the use of specific images in subsequent work, you can also use the docker tag command to add tags to local images.

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 Use inspect command to view detailed information

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

2.5 Use the history command to view the mirror 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 Search mirror

Searching for images in docker mainly uses the Search subcommand. By default, only the images in the official Docker Hub mirror repository are searched. The syntax is docker search [option] keyword. The supported command options mainly include:

2.6.1 -f: Filter output content

# 搜索官方提供的带有 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: Limit output results

# 搜索星星书大于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: Do not truncate the output result

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 Interpretation of output parameters

  • NAME: The name of the mirror repository source
  • DESCRIPTION: The description of the mirror
  • OFFICIAL: Is docker officially released
  • stars: Similar to the star in Github, which means like or like.
  • AUTOMATED: Automatically build.

2.7 Delete and clean up images

In docker, the rmi subcommand is mainly used to delete the image, and the prune subcommand is mainly used to clean the image.

rmi = rm image

2.7.1 Use tags to delete images

Use docker rmi or docker image rm command to delete the image, the command format is docker rmi image

-f: delete the mirror forcibly.

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 Clean up the image

After using for a period of time, docker will generate a lot of temporary files and some unused images. We can clean them up with the docker image prune command.

Parameters: -a: Delete all useless mirrors, not just temporary mirrors.

​ -f: Delete the image forcefully without prompting.

Good guy, call good guy!

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 download image

Download mirror 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  # 直接从官方拉下来 相当于把前面的简化了,也可以自己加上地址

Guess you like

Origin blog.csdn.net/A1L__/article/details/110438826