Docker 镜像管理

一、Docker 镜像管理

docker images    # 查看本地已下载的镜像,详细用法
docker pull      # 拉取镜像,用法如:docker pull centos,详细用法
docker search    # 搜索镜像,用法如:docker search centos,详细用法
docker rmi       # 删除镜像,用法如:docker rmi cbc05be1559f,详细用法
docker tag       # 给镜像打标签,用法如:docker tag centos centos:v1,详细用法
docker run       # 把镜像启动为容器,用法如:docker run -itd centos,详细用法


二、Docker 获取镜像的方式

1. 直接从 Docker Hub 拉取镜像

[root@localhost ~]$ docker pull centos

2. 通过容器创建镜像,假设我现在有一个容器,容器里安装了 LNMP,我想把这个容器制作成一个镜像,以便其他人使用,命令如下

[root@localhost ~]$ docker commit -m "install lnmp" -a "laowang" abe82363da86 centos_with_lnmp
# -m "install lnmp" 表示描述信息,描述改动了哪些配置
# -a "laowang" 表示作者相关的信息,可选
# abe82363da86 表示容器ID
# centos_with_lnmp 表示新的镜像名

3. 通过容器快照来创建镜像

[root@localhost ~]$ docker export 8190868b730e > centos6.tar     # 通过容器导出一个快照文件,8190868b730e是容器ID
[root@localhost ~]$ cat centos6.tar | docker import - centos6    # 通过快照文件导入一个镜像,centos6是镜像名   

4. 通过镜像备份文件来创建镜像

[root@localhost ~]$ docker save -o centos6.tar centos6    # 备份镜像
[root@localhost ~]$ docker load < centos6.tar             # 恢复镜像(写法一)
[root@localhost ~]$ docker load --input centos6.tar       # 恢复镜像(写法二)

 
三、Docker 镜像发布

我们可以通过 docker pull centos 从 Docker Hub 拉取镜像,那么我们也可以把我们的镜像发布( 或者说上传 )到 Docker Hub

1. 登录 https://hub.docker.com/ 注册一个用户

2. 需要把要上传的镜像的仓库名修改成自己的用户ID,如下

[root@localhost ~]$ docker tag centos southfly/centos
[root@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
southfly/centos     latest              1e1148e4cc2c        3 weeks ago         202MB

3. 登录 Docker Hub 并上传镜像:

[root@localhost ~]$ docker login
Username: southfly
Password: 
Login Succeeded
[root@localhost ~]$ docker push southfly/centos:latest

 

 

 

 

 

 

 

     

posted @ 2018-12-26 16:57  孔雀东南飞  阅读(518)  评论(0编辑  收藏  举报