docker | 打包容器\提交镜像\拉取镜像(阿里云)
这里做个人示例演示
1.查看本地镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
2.查看当前运行镜像容器
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.查看之前运行的容器,并运行装有vim的容器
# 查看之前运行的容器
[root@echohye ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70a649ed185e centos "/bin/bash" 16 hours ago Exited (0) 7 minutes ago linux
# 运行该容器
[root@echohye ~]# docker start 70a649ed185e
70a649ed185e
# 查看当前运行的容器
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70a649ed185e centos "/bin/bash" 16 hours ago Up About a minute linux
# 进入该容器
[root@echohye ~]# docker exec -it 70a649ed185e /bin/bash
[root@70a649ed185e /]# ls
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
# 因为之前安装了vim,所以这里演示创建文本文件并读取
[root@70a649ed185e /]# vim /home/test.txt
[root@70a649ed185e /]# cat /home/test.txt
hello world!
4.将容器打包成镜像
# 停止运行该容器
[root@echohye ~]# docker stop 70a649ed185e
70a649ed185e
[root@echohye ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 打包成镜像
[root@echohye ~]# docker commit -m="vim cmd add ok" -a='echohye' 70a649ed185e echogye/mycentos:1.0
sha256:8ff2df30dfccb1913396835f0968b9c298d0126d0b5d489af3ec26883104743c
# 查看当前容器镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
echogye/mycentos 1.0 8ff2df30dfcc 45 seconds ago 559MB <<< -- 这个就是了
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
5.登录阿里云Docker Registry
密码需要设置好,以及创建好命名空间和镜像仓库
[root@echohye ~]# docker login --username=echohye registry.cn-hangzhou.aliyuncs.com
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
6.将镜像推送到Registry
请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数
[root@echohye ~]# docker tag 8ff2df30dfcc registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
[root@echohye ~]# docker push registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/echohye/test]
f72b1f0683be: Pushed
74ddd0ec08fa: Pushed
1.0: digest: sha256:1536b6402409534284dc7b46c2fd5624bfe22fd800afd782cd1f05bba7d84625 size: 742
7.从Registry中拉取镜像
# 查看本地镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
echogye/mycentos 1.0 8ff2df30dfcc 12 minutes ago 559MB
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 12 minutes ago 559MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
# 删除刚刚上传的镜像
[root@echohye ~]# docker rmi -f 8ff2df30dfcc
Untagged: echogye/mycentos:1.0
Untagged: registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
Untagged: registry.cn-hangzhou.aliyuncs.com/echohye/test@sha256:1536b6402409534284dc7b46c2fd5624bfe22fd800afd782cd1f05bba7d84625
Deleted: sha256:8ff2df30dfccb1913396835f0968b9c298d0126d0b5d489af3ec26883104743c
Deleted: sha256:d68ad411ae67d03be3e95a10f49ccf7006f1c516cf8de82a7c86cb73aa44b748
# 再次查看本地镜像
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
# 拉取前面上传的镜像
[root@echohye ~]# docker pull registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
1.0: Pulling from echohye/test
a1d0c7532777: Already exists
b7f9a39f2c4f: Pull complete
Digest: sha256:1536b6402409534284dc7b46c2fd5624bfe22fd800afd782cd1f05bba7d84625
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
registry.cn-hangzhou.aliyuncs.com/echohye/test:1.0
# 再次查看本地镜像,发现已经拉取下来了
[root@echohye ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/echohye/test 1.0 8ff2df30dfcc 19 minutes ago 559MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 11 months ago 231MB
大家发现了没有,a1d0c7532777这个id所映射的镜像,就是本地的centos镜像,因为本地有了,所以显示Already exists
,拉取的时候不会再下载,而是直接复用本地的镜像,这就是docker的分层镜像的巧妙之处其一。
8.运行拉取下来的镜像,看看之前的test.txt文件是否还存在
# 运行并进入容器
[root@echohye ~]# docker run -it 8ff2df30dfcc /bin/bash
# 查看test.txt文本文件
[root@8c3cb9be3405 /]# cat /home/test.txt
hello world!
输出了hello world!
,说明了文本文件是存在的,也就是这个镜像是之前的完全复制品
一次上传,处处运行!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!