1. 首页
  2. 技术知识

docker镜像的拉取登陆上传及保存等相关使用命令

目录

    docker 中的三大基本概念

      镜像容器仓库

    docker 镜像相关命令

      常用镜像仓库搜索镜像拉取镜像查看当前系统上的有哪些镜像获取镜像的详细信息登录镜像仓库为镜像标签镜像上传镜像的删除清空镜像查看镜像历史(镜像的构建历史)保存镜像(commit)保存镜像(import/export)保存镜像(save/load)保存镜像三种方式的区别

docker 中的三大基本概念


镜像

镜像就是启动一个容器的模板。

容器

容器就是对外提供服务的进程。或者容器就是镜像启动起来的一个实例。

仓库

仓库是用来存放镜像的地方。

docker 镜像相关命令


常用镜像仓库

官方仓库:hub.docker.com

自己的私有仓库:Harbor

阿里云私有仓库:registry.cn-hangzhou.aliyuncs.com


搜索镜像

  1. #格式
  2.         docker search [镜像名称]
  3. # 实例

复制代码
拉取镜像

  1. # 格式
  2.         docker pull [镜像名称]
  3. # 实例
  4. [root@Centos7 ~]# docker pull redis
  5. Using default tag: latest
  6. latest: Pulling from library/redis
  7. # 镜像层
  8. a076a628af6f: Already exists
  9. f40dd07fe7be: Pull complete
  10. ce21c8a3dbee: Pull complete
  11. ee99c35818f8: Pull complete
  12. 56b9a72e68ff: Pull complete
  13. 3f703e7f380f: Pull complete
  14. # 镜像ID号(镜像ID号是全球唯一)
  15. Digest: sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494
  16. # 镜像下载状态
  17. Status: Downloaded newer image for redis:latest
  18. # 镜像的全称(镜像的tag)
  19. docker.io/library/redis:latest

复制代码
查看当前系统上的有哪些镜像

  1. # 格式
  2.         docker images 或者 docker image ls
  3. # 参数
  4. -q : 只显示镜像ID
  5. [root@Centos7 ~]# docker images -q
  6. 621ceef7494a
  7. f6d0b4767a6c

复制代码
获取镜像的详细信息

  1. # 格式
  2.         docker inspect [镜像名称或镜像ID]
  3. # 参数
  4. -f : 格式化输出
  5. [root@Centos7 ~]# docker inspect -f ‘{{.Id}}’ 621ceef7494a
  6. sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb
  7. [root@Centos7 ~]# docker inspect -f ‘{{.ContainerConfig.Hostname}}’ redis
  8. 16535cfaf84a

复制代码
登录镜像仓库

  1. # 格式
  2.         docker login
  3.         注: 默认情况下,docker login登录的是官方仓库,如果登录其他镜像仓库则需要指定镜像仓库的URL连接。
  4. # 实例
  5.         [root@Centos7 ~]# docker login registry.cn-hangzhou.aliyuncs.com
  6.         Username: yangyang091022
  7.         Password:
  8.         WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  9.         Configure a credential helper to remove this warning. See
  10.         https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  11.         Login Succeeded
  12.         [root@Centos7 ~]# cat ~/.docker/config.json
  13.     {
  14.         “auths”: {
  15.             “registry.cn-hangzhou.aliyuncs.com”: {
  16.                 “auth”: “eWFuZ3lhbmcwOTEwMjI6Y2hlbjE4NzkwMDcwODMw”
  17.             }
  18.         }
  19.     }
  20. # 参数
  21. –username|-u : 指定用户名
  22. –password|-p : 指定密码

复制代码
为镜像标签

  1. # 镜像标签的构成
  2. docker.io/library/redis:latest
  3. docker.io  : 镜像仓库的URL
  4. library    :镜像仓库命名空间
  5. redis           : 镜像名称
  6. latest           : 镜像版本号
  7. # 打标签
  8.         # 格式
  9.                 docker tag [镜像ID]  镜像标签
  10. [root@Centos7 ~]# docker images
  11. REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
  12. redis        latest    621ceef7494a   2 months ago   104MB
  13. nginx        latest    f6d0b4767a6c   2 months ago   133MB
  14. [root@Centos7 ~]# docker tag 621ceef7494a registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
  15. [root@Centos7 ~]# docker images
  16. REPOSITORY                                        TAG       IMAGE ID       CREATED        SIZE
  17. redis                                             latest    621ceef7494a   2 months ago   104MB
  18. registry.cn-hangzhou.aliyuncs.com/alvinos/redis   v2        621ceef7494a   2 months ago   104MB
  19. nginx                                             latest    f6d0b4767a6c   2 months ago   133MB

复制代码
镜像上传

  1. # 格式
  2.         docker push [镜像标签]
  3. # 注:要想上传镜像,首先得登录镜像仓库,其次设置对应镜像仓库的tag
  4. [root@Centos7 ~]# docker push registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
  5. The push refers to repository [registry.cn-hangzhou.aliyuncs.com/alvinos/redis]
  6. 3480f9cdd491: Pushed
  7. a24a292d0184: Pushed
  8. f927192cc30c: Pushed
  9. 1450b8f0019c: Pushed
  10. 8e14cb7841fa: Pushed
  11. cb42413394c4: Pushed
  12. v2: digest: sha256:7ef832c720228ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892 size: 1572

复制代码
镜像的删除

  1. # 格式
  2.         docker rmi [镜像名称或者镜像ID]
  3. # 实例
  4.         [root@Centos7 ~]# docker rmi nginx
  5. # 参数
  6.         -f  : 强制删除
  7.         [root@Centos7 ~]# docker rmi -f nginx
  8.     Untagged: nginx:latest
  9.     Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
  10. # 注:当有容器正在使用镜像时,强制删除镜像,只能删除镜像的所有tag, 不会删除镜像。

复制代码
清空镜像

  1. # 格式
  2.         docker image prune
  3. # 实例
  4.         [root@Centos7 ~]# docker image prune
  5.     WARNING! This will remove all dangling images.
  6.     Are you sure you want to continue? [y/N] y
  7.     Total reclaimed space: 0B
  8. # 参数
  9. -a : 删除所有镜像
  10. [root@Centos7 ~]# docker image prune -a
  11. WARNING! This will remove all images without at least one container associated to them.
  12. Are you sure you want to continue? [y/N] y
  13. Deleted Images:
  14. untagged: redis:latest
  15. untagged: redis@sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494
  16. untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
  17. untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis@sha256:7ef832c720228ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892
  18. deleted: sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb
  19. deleted: sha256:de66cfbf4712b8ba9ef292e08ef7487be26d9d21b350548e400ae351405d820e
  20. deleted: sha256:79b2381e35429e8fc04d31b3445f069c22d288bf5c4cba7b7c10004ff78ae201
  21. deleted: sha256:1d047d19be363b00139990d4d7f392dabdb0809dbc9d0fbe67c1f15b8caed27a
  22. deleted: sha256:8c41f4e708c37059df28ae1cabc200a6db2fee45bd3a2cadcf70f2765bb68730
  23. deleted: sha256:b51317bef36fe1900be48402c8a41fcd9cdb6b8950c10209f764473cb8323371
  24. Total reclaimed space: 35.04MB
  25. [root@Centos7 ~]#

复制代码
查看镜像历史(镜像的构建历史)

  1. # 格式
  2.         docker history [镜像ID或镜像名称]
  3. # 实例
  4. [root@Centos7 ~]# docker history alpine
  5. IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
  6. 7731472c3f2a   2 months ago   /bin/sh -c #(nop)  CMD [“/bin/sh”]              0B        
  7. <missing>      2 months ago   /bin/sh -c #(nop) ADD file:edbe213ae0c825a5b…   5.61MB   

复制代码
保存镜像(commit)

  1. # 保存正在运行的容器直接为镜像
  2. # 格式:
  3.         docker commit [容器ID|容器名称]
  4. # 实例
  5. [root@Centos7 ~]# docker commit -a “Alvin” -m “这是一个docker镜像” -p be3b92e2886b  test:v1
  6. sha256:4a06cd2af42877b5e2908073061f7ae1bf9e308a470bdfc0c6f906ef368aaed8
  7. [root@Centos7 ~]# docker images
  8. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
  9. test         v1        4a06cd2af428   5 seconds ago   104MB

复制代码
保存镜像(import/export)

  1. # 保存正在运行的容器为镜像压缩包
  2. ## 保存容器为镜像
  3.         docker export [容器的ID] > [包名称]
  4.         # 实例
  5.                 [root@Centos7 ~]# docker export be3b92e2886b > redis.tar
  6.         [root@Centos7 ~]# ll | grep redis
  7.         -rw-r–r–. 1 root root 104178688 Mar 18 17:30 redis.tar
  8. ## docker import [包名称] [自定义镜像名称]
  9.         # 实例
  10.         [root@Centos7 ~]# docker import redis.tar test:v3
  11.     sha256:7776db3402fb8d59f6121a3b1977b5e7016f4064cf59218fd1b06637cb0fca87
  12.     [root@Centos7 ~]# docker images
  13.     REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
  14.     test         v3        7776db3402fb   6 seconds ago   101MB

复制代码
保存镜像(save/load)

  1. # 保存镜像为压缩包
  2. # 保存镜像的格式:
  3.         docker save [镜像名称|镜像ID] > [包名称]
  4.     [root@Centos7 ~]# docker save 7731472c3f2a > apline.tar
  5.     [root@Centos7 ~]# ll       
  6.     -rw-r–r–. 1 root root   5888000 Mar 18 17:36 apline.tar
  7.     [root@Centos7 ~]# docker save -o apline-two.tar 7731472c3f2a
  8.     [root@Centos7 ~]# ll
  9.     total 148692
  10.     -rw-r–r–. 1 root root   5888000 Mar 18 17:36 apline.tar
  11.     -rw——-. 1 root root   5888000 Mar 18 17:37 apline-two.tar
  12. # 导入镜像的格式:
  13.         docker load < [包名称]
  14.         [root@Centos7 ~]# docker load < apline.tar
  15.     c04d1437198b: Loading layer [========================================>]   5.88MB/5.88MB
  16.     Loaded image ID: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839
  17.     [root@Centos7 ~]# docker images
  18.     REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
  19.     <none>       <none>    7731472c3f2a   2 months ago     5.61MB
  20. # 注:save/load保存镜像无法自定义镜像名称,save保存镜像时如果使用ID保存则load导入镜像无名称,使用名称导入时才有名称。
  21. [root@Centos7 ~]# docker images
  22. REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
  23. busybox      latest    b97242f89c8a   2 months ago     1.23MB
  24. [root@Centos7 ~]# docker save busybox:latest > busybox.tar
  25. [root@Centos7 ~]# ll
  26. total 150120
  27. -rw-r–r–. 1 root root   1459200 Mar 18 17:43 busybox.tar
  28. [root@Centos7 ~]# docker rmi b97242f89c8a
  29. Untagged: busybox:latest
  30. Untagged: busybox@sha256:c5439d7db88ab5423999530349d327b04279ad3161d7596d2126dfb5b02bfd1f
  31. Deleted: sha256:b97242f89c8a29d13aea12843a08441a4bbfc33528f55b60366c1d8f6923d0d4
  32. Deleted: sha256:0064d0478d0060343cb2888ff3e91e718f0bffe9994162e8a4b310adb2a5ff74
  33. [root@Centos7 ~]# docker images
  34. REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
  35. [root@Centos7 ~]# docker load < busybox.tar
  36. 0064d0478d00: Loading layer [==================================================>]   1.45MB/1.45MB
  37. Loaded image: busybox:latest
  38. [root@Centos7 ~]# docker images
  39. REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
  40. busybox      latest    b97242f89c8a   2 months ago     1.23MB

复制代码
保存镜像三种方式的区别

1、export保存的镜像体积要小于save(save保存更完全,export保存会丢掉一些不必要的数据)

2、export可以重命名镜像名称而save则不行

3、save可以同时保存多个镜像而export则不行

以上就是docker镜像的拉取登陆上传及保存等相关使用命令的详细内容,更多关于docker镜像拉取登陆上传保存等使用命令的资料请关注共生网络其它相关文章!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/108172.html

联系我们