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

Docker学习5 - 镜像

2015-08-02 15:44 841 查看

本地镜像

列出本地镜像

[root@centos7-docker ~]# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

docker.io/ubuntu latest d2a0ecffe6fa 3 weeks ago 188.3 MB

docker.io/docker/whalesay latest fb434121fc77 9 weeks ago 247 MB

docker.io/hello-world latest 91c95931e552 3 months ago 910 B

本地镜像都保存在/var/lib/docker下

[root@centos7-docker ~]# cd /var/lib/docker/



每个镜像都保存在docker所采用的存储驱动目录下,比如aufs或devicemapper

container中包含了所有的容器

远程镜像

远程镜像保存在远程仓库中,仓库位于Registry中

默认的Registry是由Docker公司运营的公共Registry服务,即Docker Hub

docker仓库类似于git仓库,包含镜像,层和镜像metadata

从仓库下载镜像

由于Docker Hub在国内没有服务器也没有CDN,所以导致下载非常缓慢,DaoCloud提供了免费mirror,请参考这篇文章进行设置
http://dockone.io/article/160
[root@centos7-docker ~]# docker pull ubuntu

https://registry.hub.docker.com/ 中查找 ubuntu, 得到信息如下



如果打不开docker hub, 进入
https://dashboard.daocloud.io/
选择 镜像仓库 -> DockerHub镜像 -> 搜索 ubuntu, 得到信息如下



可以看到ubuntu 14.04有如下信息

14.04.2, 14.04, trusty-20150630, trusty, latest (trusty/Dockerfile)

下载所有

[root@centos7-docker ~]# docker pull ubuntu:14.04.2

[root@centos7-docker ~]# docker pull ubuntu:14.04

[root@centos7-docker ~]# docker pull ubuntu:trusty-20150630

[root@centos7-docker ~]# docker pull ubuntu:trusty

[root@centos7-docker ~]# docker pull ubuntu:latest

[root@centos7-docker ~]# docker pull ubuntu

这个时候默认是ubuntu:latest

[root@centos7-docker ~]# docker images



从这里可以看到,名字为ubunbtu的仓库中包含了许许多多的镜像。docker pull ubuntu拉取了ubuntu仓库中所有的内容

对于同一个镜像,可以用不同的TAG来标识,但是他们的IMAGE ID是一样的,比如ubuntu 12.04和ubuntu precise是一样的,只是叫法不一样而已

可以使用

Registry/仓库:TAG 来标识一个image,例如

ubuntu:precise 标识Docker Hub 这个Registry(默认) 下的ubuntu precise镜像

[root@centos7-docker ~]# docker run -i -t --name ubuntu_precise ubuntu:12.04 /bin/bash

查找镜像



创建镜像

有2种方式创建镜像

1. 使用docker commit命令

2. 使用docker build命令和Dockerfile文件

第2种方式我们将会在下一节专门介绍

第1种方式不推荐,我们做简单介绍,步骤如下:

1. 创建docker hub账号

hub.docker.com上创建账号

登陆docker hub

[root@centos7-docker ~]# docker login

个人信息会保存到 $HOME/.dockercfg 文件中

2. 创建新容器

[root@centos7-docker ~]# docker run -i -t ubuntu /bin/bash

进入新容器

root@6ec6eba31ed6:/# apt-get -y update

root@6ec6eba31ed6:/# apt-get -y install vim

root@6ec6eba31ed6:/# exit

[root@centos7-docker ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6ec6eba31ed6 ubuntu:latest "/bin/bash" 18 minutes ago Exited (0) 25 seconds ago jovial_jang

[root@centos7-docker ~]# docker commit 6ec6eba31ed6 youzhibicheng/ubuntu_vim:test

d7c6d72f57f1cfe9a238add73a3e98450adc1bc3fb4cde658551e0bb9ad3fd6f

这里test是TAG

提交时可以使用更多的信息

-m="messages"

--author="author"

[root@centos7-docker ~]# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

youzhibicheng/ubuntu_vim test d7c6d72f57f1 50 seconds ago 252.5 MB

使用docker inspect 查看更多容器信息

[root@centos7-docker ~]# docker inspect youzhibicheng/ubuntu_vim:test

使用刚才创建的镜像

[root@centos7-docker ~]# docker run -i -t --name ubuntu_vim_test youzhibicheng/ubuntu_vim:test /bin/bash
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: