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!
,说明了文本文件是存在的,也就是这个镜像是之前的完全复制品
一次上传,处处运行!