docker镜像下载及docker镜像管理

2021年6月17日 14点热度 0条评论 来源: /*守护她的笑容

文章目录

docker镜像下载

镜像说明

  • docker容器万事基于镜像,没有镜像,啥也干不了。
  • 我们通过主机上拉取到的镜像和我们在hu.docker.com官网看到的镜像内容是一样的。

通外网主机上获取镜像

  • 前提是该主机上配置好相关源才可以【可以参考我博客中docker安装,里面有docker相关源的获取方式】
  • 命令:docker search 服务名
    如:docker search nginx
 [root@ccx ~]# docker search nginx
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        15016     [OK]       
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   2035                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   814                  [OK]
docker.io   docker.io/jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   200                  
docker.io   docker.io/linuxserver/nginx                  An Nginx container, brought to you by Linu...   147                  
docker.io   docker.io/tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   130                  [OK]
docker.io   docker.io/jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        119                  [OK]
docker.io   docker.io/alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from s...   99                   [OK]
docker.io   docker.io/bitnami/nginx                      Bitnami nginx Docker Image                      98                   [OK]
docker.io   docker.io/jasonrivers/nginx-rtmp             Docker images to host RTMP streams using N...   90                   [OK]
docker.io   docker.io/nginxdemos/hello                   NGINX webserver that serves a simple page ...   70                   [OK]
docker.io   docker.io/nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers ...   55                   
docker.io   docker.io/privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & ...   55                   [OK]
docker.io   docker.io/nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  38                   
docker.io   docker.io/staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs ...   23                   [OK]
docker.io   docker.io/schmunk42/nginx-redirect           A very simple container to redirect HTTP t...   19                   [OK]
docker.io   docker.io/nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter for NGINX and NG...   18                   
docker.io   docker.io/centos/nginx-112-centos7           Platform for running nginx 1.12 or buildin...   15                   
docker.io   docker.io/centos/nginx-18-centos7            Platform for running nginx 1.8 or building...   13                   
docker.io   docker.io/raulr/nginx-wordpress              Nginx front-end for the official wordpress...   13                   [OK]
docker.io   docker.io/bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Con...   9                    [OK]
docker.io   docker.io/mailu/nginx                        Mailu nginx frontend                            8                    [OK]
docker.io   docker.io/sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats...   7                    [OK]
docker.io   docker.io/ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          2                    [OK]
docker.io   docker.io/wodby/nginx                        Generic nginx                                   1                    [OK]
[root@ccx ~]# 

官网上查看镜像的详细说明

  • 其实上述方法获取源的地址就是下面这个网址。
  • 网址:https://hub.docker.com/
    • 进入网站后直接搜索想要的镜像名称即可
    • 到这个地方来是为了看这个镜像的详细说明【如默认参数这些】
  • 搜索出结果一般默认点第一个进去【这就是直接在主机上下载的镜像了】
  • 这也说了下载方式

镜像的命名方式

  • 镜像的命名规范:主机名:端口/分类/镜像名:tag 【主机名也可以是IP哦】【tag是一个标签而已,可以自定义】
  • 如:192.168.159.129:5000//ckacentos:v1
    • 主机名不写,默认是docker.io
    • tag的默认值是latest
  • 端口默认80

镜像的重命名

  • 镜像的重命名,我们仅仅是修改的REPOSITORY和TAG的名称
  • 格式是:docker tag 原名称:TAG 新主机名:端口/自定义名称:TAG名称
  • 如,我修改默认mysql名称为效果如下【修改后会多一个镜像,而非修改原来的行信息】
[root@ccx ~]# docker images | grep mysql
hub.c.163.com/library/mysql         latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# 
[root@ccx ~]# docker tag hub.c.163.com/library/mysql:latest 192.168.159.129:5000/docker/mysql:v1
[root@ccx ~]# docker images | grep mysql
192.168.159.129:5000/docker/mysql   v1                  9e64176cd8a2        4 years ago         407 MB
hub.c.163.com/library/mysql         latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# 
  • 修改后可以看到他们的ID是一样的,所以,重命名就相当于linux中的硬链接。

镜像的删除

删除单个

  • 镜像的删除有两种情况:

  • 1、如果删除的仅仅有一个镜像,那么删了后就没了

  • 2、如果有多个IMAGE ID的镜像,删除其中一个,实际上只是删除了这个名称的镜像而已

  • 格式是:docker rmi REPOSITORY名称:TAG名称 【rm是删除,i是镜像的意思】【如果不加TAG名称,则默认是latest】
    如,我现在删除一个我重命名的msq镜像【删了后myq这个镜像还是在的】【就不删镜像了测试了,我这下载没那么快】

[root@ccx ~]# docker images | grep mysql
192.168.159.129:5000/docker/mysql   v1                  9e64176cd8a2        4 years ago         407 MB
hub.c.163.com/library/mysql         latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# docker rmi 192.168.159.129:5000/docker/mysql #因为我这tag是v1所以不加就会报错了
Error response from daemon: No such image: 192.168.159.129:5000/docker/mysql:latest
[root@ccx ~]# docker rmi 192.168.159.129:5000/docker/mysql:v1
Untagged: 192.168.159.129:5000/docker/mysql:v1
[root@ccx ~]# docker images | grep mysql
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# 

删除全部镜像

  • 方式1,写个脚本,批量删除名称,方式和上面删除单个一样,脚本不难, 但麻烦,可以直接用下面方式2。
  • 方式2,通过删除文件的方式【该方法仅仅适用于删除全部】
    • 首先停止服务,systemctl stop docker 【如果停止报错,stop这个服务systemctl stop docker.socket
    • 删除文件中的内容:rm -rf /var/lib/docker/* 【注,这不仅会删除全部镜像,还会删除全部默认券,不要乱执行该命令】
    • 启动服务:systemctl start docker
[root@ccx ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/wordpress   latest              dccaeccfba36        3 years ago         406 MB
hub.c.163.com/library/centos      latest              328edcd84f1b        3 years ago         193 MB
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# systemctl stop docker
[root@ccx ~]# rm -rf /var/lib/docker/*
[root@ccx ~]# ls /var/lib/docker/
[root@ccx ~]# systemctl start docker
[root@ccx ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@ccx ~]# 

镜像的打包与解压包

默认存放路径说明

  • 镜像的默认存储路径是:/var/lib/docker/overlay2/
    ls查看的时候可以看到有很多这种ID,一行一行的
[root@ccx overlay2]# ls
046d16560f6e302cce9f312e0a92fe2fa5af64dcccd878e6c6fd3abae4ae66ff
05e36729cab1d107a5101bcc4afa6badc98472df0dccc67b8d270041462f862b
0d96eeb37078360984f52e1275a49b17ca69ebd7e5ab69f5158f934ad5929c0f
19a25ffd816ca1ceb673c57123131b0525d343347802937d767489955c79d592
1bf657e6b015252670a9222210a7f0c88a6daa40575a1b2b02069271760cc1d8
1d632e47898c8b4311b071af56f2fac88daf09c69157f303bb601cb4bdda9278
30d3a89075e63e2bb82a0c4ee5255c7361ea7f5e1d68a65723a29521ebe96e58
5399d869c74c4dcff565f36b9d9082d42b7d3592e74763cbd8730fdeaaa3e79b
5fc041b0163d777695fe6481b429c0c0e28933e493654668f3bd9a1bce389d37
6992dcd44ab5dc2088fb2e289d20d8d2a54b6531075ed53ae04be3861c805e63
70b5d10a7ea02c7771d35db14f9c24894aec36bab5e887d8b782899a35aa1fdf
73e9be947f30c404756c705d03729857605ae003366a5d44810b2b4c1fe9cee8
865fe7284725347b83d360fed6285a7f086dc5aa0e0e23fcf06a4e1afb2629bc
8bcd2b971a603db62f77da48b998a544818b360be1229d239ff4f378ae35dfa0
9183f962601c61f541fee4f679e39ad2c19d02ae8cb18c10f97337f249707517
a1601bbb19f321ea8b9ec1f9a45e477124a06ffd8eedd78766d513d38489583b
a29df21d8e873187d4164d70e9f2612599ee2c7a6662d6d1fe655e04ff97ebf1
b203d73b8587baddedfb0c55440ba7b445ae1c9131f22abd1ba37a26d36ec699
  • 是因为我们docker pull 的时候,有很多这种Download,一个Download占一行信息,如果有多个images的话,根本不能知道一个images对应的哪些行信息
69692152171a: Downloading 27.06 MB/27.15 MB
30afc0b18f67: Download complete 
596b1d696923: Download complete 
febe5bd23e98: Download complete 
8283eee92e2f: Download complete 
351ad75a6cfa: Download complete 

总大小和我们用docker images看到的是差不多的

[root@ccx overlay2]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/wordpress   latest              dccaeccfba36        3 years ago         406 MB
hub.c.163.com/library/centos      latest              328edcd84f1b        3 years ago         193 MB
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx overlay2]# pwd
/var/lib/docker/overlay2
[root@ccx overlay2]# du -sh
1.1G    .
[root@ccx overlay2]# 
[root@ccx overlay2]# ls
046d16560f6e302cce9f312e0a92fe2fa5af64dcccd878e6c6fd3abae4ae66ff
05e36729cab1d107a5101bcc4afa6badc98472df0dccc67b8d270041462f862b
0d96eeb37078360984f52e1275a49b17ca69ebd7e5ab69f5158f934ad5929c0f
19a25ffd816ca1ceb673c57123131b0525d343347802937d767489955c79d592
1bf657e6b015252670a9222210a7f0c88a6daa40575a1b2b02069271760cc1d8
1d632e47898c8b4311b071af56f2fac88daf09c69157f303bb601cb4bdda9278
30d3a89075e63e2bb82a0c4ee5255c7361ea7f5e1d68a65723a29521ebe96e58
5399d869c74c4dcff565f36b9d9082d42b7d3592e74763cbd8730fdeaaa3e79b
5fc041b0163d777695fe6481b429c0c0e28933e493654668f3bd9a1bce389d37
6992dcd44ab5dc2088fb2e289d20d8d2a54b6531075ed53ae04be3861c805e63
70b5d10a7ea02c7771d35db14f9c24894aec36bab5e887d8b782899a35aa1fdf
73e9be947f30c404756c705d03729857605ae003366a5d44810b2b4c1fe9cee8
865fe7284725347b83d360fed6285a7f086dc5aa0e0e23fcf06a4e1afb2629bc
8bcd2b971a603db62f77da48b998a544818b360be1229d239ff4f378ae35dfa0
9183f962601c61f541fee4f679e39ad2c19d02ae8cb18c10f97337f249707517
a1601bbb19f321ea8b9ec1f9a45e477124a06ffd8eedd78766d513d38489583b
a29df21d8e873187d4164d70e9f2612599ee2c7a6662d6d1fe655e04ff97ebf1
b203d73b8587baddedfb0c55440ba7b445ae1c9131f22abd1ba37a26d36ec699
backingFsBlockDev
bd53a5149549fd796937aece3ed1528132a130e3dd969094ecc13b8b754a89db
bef4811b4a55e717088657a616af5afdd2a7a4a010848f00addba4579342bd32
c2abe01b549aec02fae7cef9e073c09799776f78e425a4bd74fe192c3eca6f91
c53c61061c6a5f6cbfe06ab78d1775d4540734fb5ad88e4a8d691c8c3616cab0
c9b7be64bdad354a71a80f48e280e2f86ef5d9bfae12e3828cf493e6dd9f1337
c9b7dec653e74886057f222e8ef728ed73f0204966a376ede38512ee613c6c20
cd56cf41219ce51abfbac20529492b93d2fa03477e179b28e4fb634e1456023a
d247723d779a68fb06d19c5c902045cddbd73560b18027b7b4503d72295805be
dd8dd138ac25e6cd247501b8ecb353650681a71573c20ee89c0503e312dffe28
ef3cfa499cd010c8c0a1c15f2458d6e58300c09b19de48b3ea320481f7a94376
efdac498c27497f1accf7e5cc5157510a2ac675f6073df7368ceddecfb3e34be
fdfec033c82b487ec9211096664651b4bdaef4e6a4fbdfe62cc6c09c37185e5a
l
[root@ccx overlay2]# 

镜像打包【单个】

  • 直接用命令即可,格式:docker save REPOSITORY名称 > 自定义名称.tag
  • 如,我现在打包mysql,代码如下
[root@ccx ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/wordpress   latest              dccaeccfba36        3 years ago         406 MB
hub.c.163.com/library/centos      latest              328edcd84f1b        3 years ago         193 MB
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# 
[root@ccx ~]# docker save hub.c.163.com/library/mysql > mysql.tar 

打包完成的这个镜像tar包就可以拷贝到其他服务器上,再解压使用了哦,解压方式看下面。

镜像打包【多个】

  • 直接用命令即可,格式:docker save REPOSITORY名称1 REPOSITORY名称2 REPOSITORY名称3 > 自定义名称.tag【名称之间用一个空格隔开】
    注:这个必须是一个包一个镜像,不能>>追加不同镜像到一个包里啊
  • 如我现在同时打包现在的3个镜像到all.tar里面
[root@ccx ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/wordpress   latest              dccaeccfba36        3 years ago         406 MB
hub.c.163.com/library/centos      latest              328edcd84f1b        3 years ago         193 MB
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# docker save hub.c.163.com/library/wordpress hub.c.163.com/library/centos hub.c.163.com/library/mysql > all.tar

镜像解压

  • 也是通过命令解压,格式为:docker load -i tar包名称
  • 如,我解压mysql.tar这个镜像包
    拷贝到其他服务器麻烦了,所以我直接删除现在的这个mysql镜像,再解压出来测试
[root@ccx ~]# docker images | grep mysql #存在
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# docker rmi hub.c.163.com/library/mysql #删除
Untagged: hub.c.163.com/library/mysql:latest
Untagged: hub.c.163.com/library/mysql@sha256:b2bce1a792237ac5df78877d583f34b09ab023a77130921a6bcce67ce2d24ff0
Deleted: sha256:9e64176cd8a206f88336506fe52cd8f87423147dc197d0250175dddc39465e90
Deleted: sha256:2d79a736de4c447bbad1801e9e4568d6b5f2ba4cc2d5e31a908af9bf51e9b3b3
Deleted: sha256:6e4bd70bb55bcb5ad9850d51c13fd6d40c66aae50bca7741fd940561b2a148e2
Deleted: sha256:75c1f169d72e1475a4427f528343669d2973701589a26791aacfda1dd5df4f66
Deleted: sha256:b934b913f0c1280d85ee196c29a5b246087d74a2a6747b479b4a677f05d11626
Deleted: sha256:47be8aca05b42f1e1a4c9e7058f1eaf59a68119f7e7cbec56b9a68aeb8ed8fc5
Deleted: sha256:ba5061936a764f53b2d1118db1f491f06f4afb0a1a19f94aa0d07ee3ac1a5949
Deleted: sha256:0bfdd0113d6e2adeef68baa97ab6da9900698f656e9ca193b274cb25e67a12ac
Deleted: sha256:edcf46029534ffee6cbf4b621473ad513b66b60ab0e9f8c15107b841c2986755
Deleted: sha256:bd0eace476f07ae4c41014e81823062d43f3c848aa5337061f59833590ff15f9
Deleted: sha256:4a0ae64be2238aa31b864ee1e396b24f672025bcf98656a10e4ee3158b2c8399
Deleted: sha256:295d6a056bfd381abf9c462629066b0458373045a1dca7d13ef6128cb38c977e
[root@ccx ~]# docker images | grep mysql #没了
[root@ccx ~]# docker load -i mysql.tar #解压
295d6a056bfd: Loading layer 129.2 MB/129.2 MB
3b7c5f5acc82: Loading layer 344.6 kB/344.6 kB
da4155a7d640: Loading layer 4.657 MB/4.657 MB
daf31ec3573d: Loading layer 1.536 kB/1.536 kB
904af8e2b2d5: Loading layer 39.68 MB/39.68 MB
2bee3420217b: Loading layer 25.09 kB/25.09 kB
e5de8ba20fae: Loading layer 3.584 kB/3.584 kB
9e615ff77b4f: Loading layer 240.5 MB/240.5 MB
fa9efdcb088a: Loading layer 6.144 kB/6.144 kB
3c376267ac82: Loading layer 9.216 kB/9.216 kB
8129a85b4056: Loading layer 1.536 kB/1.536 kB
Loaded image: hub.c.163.com/library/mysql:latest
[root@ccx ~]# docker images | grep mysql #有了
hub.c.163.com/library/mysql       latest              9e64176cd8a2        4 years ago         407 MB
[root@ccx ~]# 

容器的导出和导入

把容器导出为镜像

格式为:docker export 容器名 > 自定义名称.tar

镜像导入为容器

格式为:cat 导出的自定义名称.tar | docker import - 镜像名

查看镜像内容中的cmd进程

简单来说,在镜像里,都会指定,使用我这个镜像所生成的容器里运行的是什么进程【这个进程就是cmd进程】

查看镜像中的cmd进程内容

  • 命令: docker history REPOSITORY名称
  • 如,查看mysq的进程【一般为第一行】
    可以看到mysq的cmd进程为mysqld进程
[root@ccx ~]# docker history hub.c.163.com/library/mysql | head -n 2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9e64176cd8a2        4 years ago         /bin/sh -c #(nop) CMD ["mysqld"] 0 B 
[root@ccx ~]# 

查看镜像的完整内容【不压缩】

  • 这个主要是用于 某个 服务的 cmd进程内容太长显示不全,才用这个命令让其不压缩显示,平常的话直接 docker history查看即可
  • 命令: docker history REPOSITORY名称 --no-trunc
  • 如,nginx直接history查看就显示不全,所以需要加上这个参数,让其显示完整。
[root@ccx ~]# docker history docker.io/nginx | head -n 2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d1a364dc548d        3 weeks ago         /bin/sh -c #(nop) CMD ["nginx" "-g" "daem... 0 B 
[root@ccx ~]# docker history docker.io/nginx --no-trunc| head -n 2
IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  SIZE                COMMENT
sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee   3 weeks ago         /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0 B 
[root@ccx ~]# 
    原文作者:/*守护她的笑容
    原文地址: https://blog.csdn.net/cuichongxin/article/details/117987194
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。