2. docker管理
2.1 docker拉取、推送镜像
从仓库pull镜像或者从仓库拉取镜像,都是用标准的命令格式
ip:5000/分类/镜像名:tag
其中:
- ip是仓库的ip地址或者主机名称也可(主机名称需要能够正常解析),默认为docker.io
- 5000是端口号,默认为80
- tag标签,默认为latest
举例:182.168.57.49:5000/cka/mysql:v1,如下所示
hub.c.163.com/library/wordpress:latest
[root@vms41 yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 0627ec6901db 13 days ago 556MB
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
2.2 docker重命名镜像
命令:docker tag 原标签名 ip:5000/分类/镜像名:tag
示例:
[root@vms41 yum.repos.d]# docker tag mysql:latest 192.168.57.49:5000/cka/mysql:v1
[root@vms41 yum.repos.d]#
[root@vms41 yum.repos.d]#
[root@vms41 yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 0627ec6901db 13 days ago 556MB
192.168.57.49:5000/cka/mysql v1 0627ec6901db 13 days ago 556MB
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
可以看到镜像:mysql和192.168.57.49:5000/cka/mysql的IMAGE ID是一样的,这种情况类似于linux的硬链接,系统中的同一个image,有不同的命名,
只有有一个命名tag在,则该镜像就会存在,如果要删除该镜像,则需要删除该镜像所有的tag
2.3 docker删除镜像
命令:docker rmi 镜像名:tag,不添加tag的时候,tag默认为latest
示例:
[root@vms41 yum.repos.d]# docker rmi mysql
##没有添加tag的时候,默认使用latest作为tag名称,因为这时候系统里面存在mysql:latest这个镜像,所以成功删除,但是因为该镜像有多个命名,所以只是Untagged的
Untagged: mysql:latest
Untagged: mysql@sha256:04ee7141256e83797ea4a84a4d31b1f1bc10111c8d1bc1879d52729ccd19e20a
[root@vms41 yum.repos.d]# docker rmi 192.168.57.49:5000/cka/mysql ##没有添加tag,且系统中已经没有mysql:latest镜像名,所以删除失败
Error: No such image: 192.168.57.49:5000/cka/mysql
[root@vms41 yum.repos.d]# docker rmi 192.168.57.49:5000/cka/mysql:v1
Untagged: 192.168.57.49:5000/cka/mysql:v1 ##删除了镜像的最后一个命名所以删除成功
Deleted: sha256:0627ec6901db4b2aed6ca7ab35e43e19838ba079fffe8fe1be66b6feaad694de
Deleted: sha256:94d5db550d62032ddc8ad8d4cfef8bac06fe7d35757deb2f8638d0fdc1e89217
Deleted: sha256:5fbbb0e9bfb8f2a4d665cc60aaf4876191c057898db276d2d030a0d6123afc8e
Deleted: sha256:124a9d262ebecea284b6438b0a04766b076bd3f89cb0f29a8a475f26bf93911f
Deleted: sha256:5aa37b6c8e31197ab2f357c09755bcb83228ff0fb69c2009a12b6eddb087e884
Deleted: sha256:d0b1d5665c308823bbc5b8986d405e5202ade6994dfa00d8ff576eff372dd045
Deleted: sha256:cd35e2328f0670969657f1abae8beffbc1eb1fddbaf667e1e6e6286598500a35
Deleted: sha256:068b92efc0504adcd3c23f16fde80775a2f4dfe485e242206f638eae72c4fa1b
Deleted: sha256:7c8818a166d9666c68fcdbe421c30568d60d51a505e540f42901664113047a75
Deleted: sha256:5aa8f65565168fd7db2aa6b9f8fb1db746aa598fa3854dcbdbb49d5a29f6d8a5
Deleted: sha256:cca9d1bafa1ee67bb4d7178732c0955a40a5dea6e5b989f61248984f26f7306b
Deleted: sha256:34ca91e79c4027120ca740231d415c739cccad57d1ee68d6a6e67ca60bbaf3a4
[root@vms41 yum.repos.d]# docker images ##产看镜像,已经删除了mysql的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
2.4 保存镜像
2.4.1 镜像保存位置
[root@vms41 yum.repos.d]# du -sh /var/lib/docker/overlay2/ ##镜像保存位置,下载的镜像拆分后保存在该目录
1.3G /var/lib/docker/overlay2/
[root@vms41 yum.repos.d]# ls /var/lib/docker/overlay2/
034d378e2ab74a677565ef54ff5c9ca33faa7f427e8ef3b57ab29acbd9f3fda5 a8274667b95b3241cfcfad27d09d7e97ab708ec1b39a2de5a41a27fe29ac4501
213f5cecdfb90c49fc9d774d8b4175fb36bbeff9d9b0d2a354be3e06ec306897 a9091f419aad96043ea811181dc04ffb2515e10511c90a1f2a2c7b94140bbf8a
317ce51d4462c37f702a62066fef995ab685dd9409c59509a2a5603871fb2c5c b1647f6aadbecd474d5e10f4e36905bea3ad40e23bc7850e6c0a8c98869c9d59
325cb1ce2b38ac28848ea600dd9cc53c481b246cea643d7bafbb53b1de8756f2 b31d76ca16a02fed9529df3906eddb2ef69a7236b91365457aa36fcf51e33633
3621c486d8c2a2a78bf92f2d2aeed7c5a407c30ac013ef18fd970e171f8129f1 b461c2b0175ccb7a4e8f212fc8c5cdf02496f0cabde7bbcafc13740f5858991f
3b4322e12f7bf0194393e1adb96b94226af308d0cd234f0f00264acc2a06e0a4 backingFsBlockDev
3c10f045331049eca8ec5bf6b6c275b8a4b4a2cfd706e54b2959da18854f7364 c47db9c53b0e44ac0efe08cb77e2514df6172862926de59ec1a5cb3deeda92a9
3d8fa760bab15e610e22801fd7c3babcd19af96aa41a31f455a2a9837901212a c967ee67a6deb3f5aa6160ea9a1b15c0f8228bf0526b66b22a615409ab4e1421
48d830e3e9e28cab8b59c38181dda4cbbe1e9e71f96403bc2f0dd6bcbbaca1b6 cd511efc803d7ca8effc1cd7a350b06f866da30fd303171ecfb1fe9d2ba72c6a
48fb0d06539048812bb46980a2928fa8e3735066bfa7d30fec87ab527751112b d809666ca3562f1a9c7d5a2ba3d5b7d3db9c9f439d9ce1f87bdccf32b4777950
4bae4ee972881aef83696c9cefb03adad276508383471ad16b26fba0d5066328 d930f3e6cad635bd01d57856c3ff0c44640031a814bfb93fde65ab542e84ca77
4cae0e1fb79b90daed2ccdd5fe86b68ee7317b24224d70a0eb49497184a5a14d da31183fc161a6be7fe60eafd4fe36eba527592d37f40192a1f1d335531f3ea6
52e7ba3651a1400e32ec68b8c9222c07843f835f8b0c998e3c0699dfcfdd6204 da5b70e9123be5825e67001827a0deb3bfd53307358073059f37eb8a60182fa8
5bf35616c7ab2718f1c3e774d8468281b430b86cd325b0b8ccde5529e72fc1bc f384896da83ae73309c89722a4ae54d2bdb358af466886448c1697310ff6251f
6b6a184505d721dd1b5757c2057bd734feeb542428bec1661f5ba2895978a668 f47a5b02dc16131640e5f2664ba1971d194f0f3dde50ac685b75b56245bd0d4c
7135597ef94b4e4aaa653f8fd4f656cf00bf21deceb4858f58c34ac28e86d093 f65b594e6e728719600686413f7fa1347ba03fd235b83a7c2f39aa16aa1ef294
7bfe0faedd2786bbe705905292a4b020264dcb1f38d8883a22ff4bc8da208951 f85f35f9a75b028660fcb32c9da2431186ccc988ab43ba213ea9b95db134fe7b
7c13ffa53009308734a8232d7d9a9289abda2ba16787b2a7db0b0956284e2963 fa727ceda9a2956755a01f410e88beb767cff09acab6c6fdb979a454a5eee2d9
8b7ff826e1ddc02c89b9bfe96cee53675b4fe22ea14c4593436b8bab1ed2a344 fcb8bcce2f39980b11d75102d8e0f8676f9b6788d3932a7bc19c36f296201fec
9722e653f72ca1fe4a9e767dc016238755085bfee85f05bf81e6e9b7f5526810 ff65876d1418bf5820de83aad133af455a0dcfa18e70ce109f319cbd8e1a9b7a
97dd392fd564e153e3d1d7010a8687181ec0b3b8d53863d1c2421f2e27fe6b36 ff9615cf3b56b282818d1313819a2b5cb36c2d4abb55daffe3dd823ca31bae72
a0acd75ae6e493bd87296a35971517a94d55f60cabeda14185840b5778a945be l
2.4.2 导出已经安装的镜像
命令:
docker save 镜像名称(多个用空格分隔) > 导出文件名称
备注:
不可以使用>>来追加镜像到导出文件中
示例:
[root@vms41 yum.repos.d]# docker save nginx hub.c.163.com/library/wordpress hub.c.163.com/library/centos hub.c.163.com/library/redis hub.c.163.com/library/mysql > /root/all.tar
[root@vms41 yum.repos.d]# ll /root/all.tar
-rw-r--r-- 1 root root 1280875008 5月 3 23:30 /root/all.tar
[root@vms41 yum.repos.d]# ls -lh /root/all.tar
-rw-r--r-- 1 root root 1.2G 5月 3 23:30 /root/all.tar
2.4.3 导入已经导出的本地镜像
命令:
docker load -i 导出镜像名
示例:
[root@vms41 ~]# docker save nginx > nginx.tar #导出nginx镜像
[root@vms41 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
[root@vms41 ~]# docker rmi nginx ##删除nginx镜像
Untagged: nginx:latest
Untagged: nginx@sha256:75a55d33ecc73c2a242450a9f1cc858499d468f077ea942867e662c247b5e412
Deleted: sha256:62d49f9bab67f7c70ac3395855bf01389eb3175b374e621f6f191bf31b54cd5b
Deleted: sha256:3444fb58dc9e8338f6da71c1040e8ff532f25fab497312f95dcee0f756788a84
Deleted: sha256:f85cfdc7ca97d8856cd4fa916053084e2e31c7e53ed169577cef5cb1b8169ccb
Deleted: sha256:704bf100d7f16255a2bc92e925f7007eef0bd3947af4b860a38aaffc3f992eae
Deleted: sha256:d5955c2e658d1432abb023d7d6d1128b0aa12481b976de7cbde4c7a31310f29b
Deleted: sha256:11126fda59f7f4bf9bf08b9d24c9ea45a1194f3d61ae2a96af744c97eae71cbf
Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d
[root@vms41 ~]# docker images ##查看确认nginx镜像已经删除
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
[root@vms41 ~]# docker load -i nginx.tar ##导入已经导出的nginx.tar镜像
7e718b9c0c8c: Loading layer [==================================================>] 72.52MB/72.52MB
4dc529e519c4: Loading layer [==================================================>] 64.81MB/64.81MB
23c959acc3d0: Loading layer [==================================================>] 3.072kB/3.072kB
15aac1be5f02: Loading layer [==================================================>] 4.096kB/4.096kB
974e9faf62f1: Loading layer [==================================================>] 3.584kB/3.584kB
64ee8c6d0de0: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@vms41 ~]# docker images ##查看确认nginx镜像已经导入
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/redis latest d4f259423416 3 years ago 106MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
2.5 使用镜像生成容器
2.5.1 查看镜像生成时候的指令
命令:
docker history 镜像名:Tag
查看镜像生成时候的指令,不截取
docker history 镜像名:Tag --no-trunc
示例:
[root@vms41 ~]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
62d49f9bab67 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 2 weeks ago /bin/sh -c set -x && addgroup --system -… 63.9MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.5.3 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.19.10 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:c855b3c65f5ba94d5… 69.3MB
[root@vms41 ~]# docker history nginx:latest --no-trunc