1. 程式人生 > 其它 >Docker映象相關的命令

Docker映象相關的命令

官方文件:https://docs.docker.com/reference/

獲取映象

docker pull 映象名稱

示例:

$ docker pull redis:6

注:映象名稱後面可以指定 tag 標籤資訊,來拉取指定的版本, 如果不宣告 tag 映象標籤資訊則預設拉取 latest 版本。

檢視本地映象

方式1:

docker images

方式2:

docker image ls

示例:

$ docker images
REPOSITORY               TAG            IMAGE ID            CREATED           SIZE
redis                    6              bc8d70f9ef6c        4 days ago        105MB
redis                    latest         bc8d70f9ef6c        4 days ago        105MB
nginx                    latest         8a2fb25a19f5        4 days ago        16M
  • REPOSITORY: 映象在倉庫中的名稱
  • TAG: 映象標籤
  • IMAGE ID: 映象ID
  • CREATED: 映象的建立日期(不是獲取該映象的日期)
  • SIZE: 映象大小

使用 -q 引數列出所有映象的id

$ docker images -q
311b8e6ce7a
1311b8e6ce7a
13c0a7ac4e4c
7ef0d2051128
a86b7d59e091

搜尋映象

docker search 映象名稱

示例:

$ sudo docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   9448      [OK]       
bitnami/redis                    Bitnami Redis Docker Image                      180                  [OK]
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      78                   
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   58                   [OK]
redislabs/redisearch             Redis With the RedisSearch module pre-loaded…   34                   
redislabs/redis                  Clustered in-memory database engine compatib…   30                   
redislabs/redisinsight           RedisInsight - The GUI for Redis                30                   
oliver006/redis_exporter          Prometheus Exporter for Redis Metrics. Supp…   25       
  • NAME: 映象名稱
  • DESCRIPTION: 映象的描述
  • STARS: 點贊數目,反映一個映象的受歡迎程度
  • OFFICIAL: 是否為官方構建
  • AUTOMATED: 自動構建,表示該映象由 Docker Hub 自動構建流程建立的

刪除映象

可以使用映象ID,映象名(倉庫名:標籤)來刪除映象

docker rmi 映象ID
# docker rmi 倉庫名:標籤

或者:

docker image rm 映象ID
# docker image rm 倉庫名:標籤

引數:

-f: 強制刪除映象,即使有容器在使用該映象

--no-prune: 不清理未帶標籤的父映象

示例:

$ docker image rm mongo:latest
Untagged: mongo:latest
Untagged: mongo@sha256:8b35c0a75c2dbf23110ed2485feca567ec9ab743feee7a0d7a148f806daf5e86
Deleted: sha256:07630e791de3ceb87d39543799438e118753246d19dcfd6529bd4d27ff1b83bd
Deleted: sha256:4a66f19e9d76939e792729b76a29d272779492806307f81e7a000815206fdcec
Deleted: sha256:13dcbb5d78f44fd2197f2107de111604c96602f238fc6c9b106abc1e1586073d
Deleted: sha256:6a9ec91fc2a1e84877840a47a4a76e25be748cca6f828d0a4f33db1cd0b3058e
Deleted: sha256:f633e79fbfe93d8750cb22cfa13708ac74838113bff5b054ea8a788f1ef522cd
Deleted: sha256:f7113b27f3a2d69a87eee7cf778c813dfd229f8fcd3e9619728226fcf99301b9
Deleted: sha256:8ec2a7a02aaca5c75168922c58048474dbcb62bcba488d46272b8e6b9586f39b
Deleted: sha256:95558bd5c3398f4a0d176bb6e3f580158b7e1d29143951f0eb79582a59a0cfe1
Deleted: sha256:d99356a78f13510683e69464045da3e6a2d9577c57b9d9889aa2e30f81d6c8b6
Deleted: sha256:03f5eb78fae16679a5641a35ead4819f2f719a7f08498ed2906515dda760be15
Deleted: sha256:5683b8d625d1dfa13fd9bf36bd007876a10ba7b9d0159e5b751c2e555056b1a4
Deleted: sha256:e40721a5f7d83ef5475788aa771a9cc75312998fb206ba8c2cf4294f9594f46e
Deleted: sha256:5153e1acaabce0d87adbf4a717bdc5e26cf86d851330830589a6b2bf5ede30ee

結合 docker images 命令刪除指定的映象

docker image rm $(docker images -q redis) 

新增映象標籤

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

示例:

$ docker images
REPOSITORY                                                           TAG             IMAGE ID       CREATED         SIZE
redis                                                                6               bc8d70f9ef6c   6 weeks ago     105MB
redis                                                                latest          bc8d70f9ef6c   6 weeks ago     105MB
minio/minio                                                          latest          305cf2390a4e   2 months ago    184MB
$ docker tag redis:latest redis:v1 
$ docker images
REPOSITORY                                                           TAG             IMAGE ID       CREATED         SIZE
redis                                                                6               bc8d70f9ef6c   6 weeks ago     105MB
redis                                                                latest          bc8d70f9ef6c   6 weeks ago     105MB
redis                                                                v1              bc8d70f9ef6c   6 weeks ago     105MB
minio/minio                                                          latest          305cf2390a4e   2 months ago    184MB
  • 可以看到成功新增了一條 TAG 記錄,但是 IMAGE ID 沒有變化,可以看出他們是同一個映象。
  • 如果沒有指定tag,則預設是latest標籤。

映象的匯入匯出

映象匯出:將映象儲存為tar檔案

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

OPTIONS:

  • -o: 輸出到的檔案

示例:

docker save -o redis-latest.tar redis:latest
docker save -o redis-v1.tar redis:v1

檢視儲存後的tar檔案:

$ ls -al
-rw-------  1 root root 108803072 6月  25 11:02 redis-latest.tar
-rw-------  1 root root 108803072 6月  25 11:05 redis-v1.tar

映象匯入:匯入使用docker save命令匯出的映象

docker load [OPTIONS]

OPTIONS:

  • -i: 指定要匯入的檔案
  • -q: 精簡輸出

示例:

$ docker load -i ./redis-v1.tar 
Loaded image: redis:v1

檢視映象的歷史

$ docker image history [OPTIONS] IMAGE

OPTIONS:

  • --human, -H: 以可讀的格式列印,預設為 true
  • --no-trunc: 顯示完整的提交記錄
  • --quiet, -q: 僅列出提交記錄 ID

示例:

$ docker image history redis
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
bc8d70f9ef6c   2 months ago   /bin/sh -c #(nop)  CMD ["redis-server"]         0B        
<missing>      2 months ago   /bin/sh -c #(nop)  EXPOSE 6379                  0B        
<missing>      2 months ago   /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B        
<missing>      2 months ago   /bin/sh -c #(nop) COPY file:df205a0ef6e6df89…   374B      
<missing>      2 months ago   /bin/sh -c #(nop) WORKDIR /data                 0B        
<missing>      2 months ago   /bin/sh -c #(nop)  VOLUME [/data]               0B        
<missing>      2 months ago   /bin/sh -c mkdir /data && chown redis:redis …   0B        
<missing>      2 months ago   /bin/sh -c set -eux;   savedAptMark="$(apt-m…   31.6MB    
<missing>      2 months ago   /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_SHA=98…   0B        
<missing>      2 months ago   /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_URL=ht…   0B        
<missing>      2 months ago   /bin/sh -c #(nop)  ENV REDIS_VERSION=6.2.3      0B        
<missing>      2 months ago   /bin/sh -c set -eux;  savedAptMark="$(apt-ma…   4.15MB    
<missing>      2 months ago   /bin/sh -c #(nop)  ENV GOSU_VERSION=1.12        0B        
<missing>      2 months ago   /bin/sh -c groupadd -r -g 999 redis && usera…   329kB     
<missing>      2 months ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      2 months ago   /bin/sh -c #(nop) ADD file:7362e0e50f30ff454…   69.3MB