您的位置:首页 > 运维架构 > Docker

docker安装、镜像管理、创建镜像、导入导出镜像

2020-03-13 07:41 676 查看

文章目录

Centos7安装docker

下载阿里云提供的docker yum源:

[root@linux01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装docker社区版(免费版):

[root@linux01 ~]# yum -y install docker-ce

启动docker服务:

[root@linux01 ~]# systemctl start docker

查看docker版本信息:

docker version

配置docker加速器

配置加速器可以提升获取Docker官方镜像的速度,创建配置文件:

[root@linux01 ~]# vi /etc/docker/daemon.json

配置文件内容:

{
"registry-mirrors": ["https://jmnbijcd.mirror.aliyuncs.com"]
}

#该url为加速器地址,需要自行到阿里云—>容器镜像服务—>加速器获取

重启docker服务:

[root@linux01 ~]# systemctl restart docker

镜像管理

下载docker官方仓库的centos镜像:

[root@linux01 ~]# docker pull centos

查看本地的镜像:

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

#从左到右分别为镜像名、标签、镜像id、创建时间、大小

根据镜像名称搜索镜像:

[root@linux01 ~]# docker search ubuntu

更改镜像名:

[root@linux01 ~]# docker tag centos test
[root@linux01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        3 months ago        220MBtest                latest              0f3e07c0138f        3 months ago        220MB

#更改镜像名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

启动镜像:

[root@linux01 ~]# docker run -itd centos
af57114ec8a10c96b6301de65fb6d6e30a61fee19ef48676a15d736e83628264

#镜像启动后就变成了容器,-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

#docker ps -a可以查看所有容器(包括未运行的)

删除指定镜像:

[root@linux01 ~]# docker rmi test

当镜像标签不是latest时,需要指定镜像名以及标签才可删除:

[root@linux01 ~]# docker rmi abc:abc

#如果指定的是镜像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 /]#

在容器中安装任意服务后退出:

[root@af57114ec8a1 /]# yum -y install net-tools
[root@af57114ec8a1 /]# exit
[root@linux01 ~]#

将变更后的容器创建镜像:

[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

#新的镜像创建成功,这个命令类似git、svn的提交,-m参数添加描述,-a参数指定操作者或是作者 af57114ec8a1这一串为容器id(通过docker ps获取),再后面为新镜像的名字

导入导出镜像

导出镜像:

[root@linux01 ~]# docker save -o centos_net.tar centos_net

#将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

导入镜像:

[root@linux01 ~]# docker load < centos_net.tar
45a8968b6423: Loading layer [==================================================>]   41.4MB/41.4MB
Loaded image: centos_net:latest

#导入镜像的第二种方式:

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
  • 点赞 2
  • 收藏
  • 分享
  • 文章举报
Asnfy 发布了116 篇原创文章 · 获赞 925 · 访问量 12万+ 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: