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

docker 中的三大基本概念

镜像

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

容器

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

仓库

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

docker 镜像相关命令

常用镜像仓库

官方仓库:hub.docker.com

自己的私有仓库:Harbor

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

搜索镜像

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

拉取镜像

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

查看当前系统上的有哪些镜像

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

获取镜像的详细信息

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

登录镜像仓库

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

为镜像标签

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

镜像上传

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

镜像的删除

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

清空镜像

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

查看镜像历史(镜像的构建历史)

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

保存镜像(commit)

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

保存镜像(import/export)

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

保存镜像(save/load)

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

保存镜像三种方式的区别

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

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

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

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

原文链接:https://www.cnblogs.com/guyouyin123/p/15010838.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:docker从安装入门到应用部署及私有仓库搭建基础命令

2023-3-7 21:46:20

建站知识

小知识:基于Docker与Jenkins实现自动化部署的原理解析

2023-3-7 22:00:59

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索