温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker容器中镜像的操作方法

发布时间:2020-05-23 09:11:12 来源:亿速云 阅读:281 作者:Leah 栏目:大数据

今天小编就为大家带来一篇有关docker容器中镜像操作方法的文章。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。

文章目录
Centos7安装docker
配置docker加速器
镜像管理
通过容器创建镜像
导入导出镜像
Centos7安装docker
下载阿里云提供的docker yum源:

[root@linux01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1
安装docker社区版(免费版):

[root@linux01 ~]# yum -y install docker-ce
1
启动docker服务:

[root@linux01 ~]# systemctl start docker
1
查看docker版本信息:docker version

配置docker加速器外汇技术面www.gendan5.com/tech.html
配置加速器可以提升获取Docker官方镜像的速度,创建配置文件:

[root@linux01 ~]# vi /etc/docker/daemon.json
1
配置文件内容:

{
"registry-mirrors": ["https://jmnbijcd.mirror.aliyuncs.com"]
}
1
2
3
#该url为加速器地址,需要自行到阿里云—>容器镜像服务—>加速器获取

重启docker服务:

[root@linux01 ~]# systemctl restart docker
1
镜像管理
下载docker官方仓库的centos镜像:

[root@linux01 ~]# docker pull centos
1
查看本地的镜像:

[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        3 months ago        220MB
1
2
3
#从左到右分别为镜像名、标签、镜像id、创建时间、大小

根据镜像名称搜索镜像:

[root@linux01 ~]# docker search ubuntu
1
更改镜像名:

[root@linux01 ~]# docker tag centos test
[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        3 months ago        220MB
test                latest              0f3e07c0138f        3 months ago        220MB
1
2
3
4
5
#更改镜像名centos为test,会产生一个id相同但镜像名不相同的镜像

更改镜像名并更改标签:

[root@linux01 ~]# docker tag test abc:abc
[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              0f3e07c0138f        3 months ago        220MB
abc                 abc                 0f3e07c0138f        3 months ago        220MB
centos              latest              0f3e07c0138f        3 months ago        220MB
1
2
3
4
5
6
启动镜像:

[root@linux01 ~]# docker run -itd centos
af57114ec8a10c96b6301de65fb6d6e30a61fee19ef48676a15d736e83628264
1
2
#镜像启动后就变成了容器,-i表示让容器的标准输入打开,-t表示分配一个伪终端,-d表示后台启动,要把-i -t -d 放到镜像名字前面

查看正在运行的容器:

[root@linux01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
af57114ec8a1        centos              "/bin/bash"         4 minutes ago       Up 4 minutes                            charming_mclaren
1
2
3
#docker ps -a可以查看所有容器(包括未运行的)

删除指定镜像:

[root@linux01 ~]# docker rmi test
1
当镜像标签不是latest时,需要指定镜像名以及标签才可删除:

[root@linux01 ~]# docker rmi abc:abc
1
#如果指定的是镜像id而非镜像名时,所有该id的镜像都会被删除

通过容器创建镜像
使用docker run启动镜像后,即可指定容器id进入容器:

[root@linux01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
af57114ec8a1        centos              "/bin/bash"         12 minutes ago      Up 12 minutes                           charming_mclaren
[root@linux01 ~]# docker exec -it af57114ec8a1 bash
[root@af57114ec8a1 /]#
1
2
3
4
5
在容器中安装任意服务后退出:

[root@af57114ec8a1 /]# yum -y install net-tools
[root@af57114ec8a1 /]# exit
[root@linux01 ~]#
1
2
3
将变更后的容器创建镜像:

[root@linux01 ~]# docker commit -m "add net-tools" -a "root" af57114ec8a1 centos_net
sha256:f9dba795f88479c70133e18145ad5337c3199fe633523009a2bb84febf58d695
[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_net          latest              f9dba795f884        8 seconds ago       261MB
centos              latest              0f3e07c0138f        3 months ago        220MB
1
2
3
4
5
6
#新的镜像创建成功,这个命令类似git、svn的提交,-m参数添加描述,-a参数指定操作者或是作者 af57114ec8a1这一串为容器id(通过docker ps获取),再后面为新镜像的名字

导入导出镜像
导出镜像:

[root@linux01 ~]# docker save -o centos_net.tar centos_net
1
#将centos_net镜像导出为.tar文件

删除centos_net镜像:

[root@linux01 ~]# docker rmi centos_net
[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        3 months ago        220MB
1
2
3
4
导入镜像:

[root@linux01 ~]# docker load < centos_net.tar
45a8968b6423: Loading layer [==================================================>]   41.4MB/41.4MB
Loaded image: centos_net:latest
1
2
3
#导入镜像的第二种方式:docker load --input centos_net.tar

查看镜像是否导入成功:

[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_net          latest              f9dba795f884        31 minutes ago      261MB
centos              latest              0f3e07c0138f        3 months ago        220MB

关于docker容器中镜像的操作方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI