1、获取centos镜像(源:docker.com)
docker pull centos
2、查看本地都有哪些镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest bb3d629a7cbc 11 days ago 196.6 MB
3、修改镜像名(这里实际上是新建了镜像,但是image id相同)
# docker tag 原镜像名 自定义名
[root@localhost ~]# docker tag centos daixuan //为centos镜像设置标签为daixuan,再使用docker images查看会多出来一行,该行的image id和centos的一样
[root@localhost ~]# docker images//实际上IMAGE ID都一样
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest bb3d629a7cbc 11 days ago 196.6 MB
daixuan latest bb3d629a7cbc 11 days ago 196.6 MB
修改镜像名 + TAG
# docker tag 原镜像名 自义定名:自定义TAG名 //实际上是新建了镜像
# docker tag IMAGE_ID 自义定名:自定义TAG名 //实际上是新建了镜像
注:如果镜像已经有了名称和tag,那么这条命令就会新建一个相同IMAGE ID的镜像;
注:模板导入的镜像没有tag,所以使用docker tag添加名称和tag,参考模板导入,不会新建镜像;
4、从docker仓库搜索docker镜像,后面是关键词
#docker search (image -name)
5、用下载到的镜像开启容器
#docker run -t -i centos /bin/bash //-i表示让容器的标准输入打开,-t表示分配一个伪终端,要把-i -t 放到镜像名字前面 /bin/bash表示运行的一个命令
[root@localhost ~]# docker run -it centos
[root@c49153b5d5b6 /]# ls
anaconda-post.log dev home lib64 media opt root sbin sys usr
bin etc lib lost+found mnt proc run srv tmp var
[root@c49153b5d5b6 /]# exit //使用exit命令退出镜像,这个容器也随之关闭
exit
当该镜像发生修改后,我们可以把该镜像提交重新生成一个新版本进行在本地。
6、[root@localhost ~]# docker ps //查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c49153b5d5b6 centos "/bin/bash" 15 minutes ago Up 15 minutes furious_perlman
#docker ps -a //查看所有容器,包括已经退出的
7、用来删除指定镜像
#docker rmi 名称 //删除的是:daixuan:latest,lastest是默认tag
#docker rmi 名称:TAG //删除的是:daixuan:TAG
#docker rmi IMAGE ID //删除的是:所有的IMAGE ID,会提示输入-f选项,强制删除多个镜像
//其中后面的参数可以是tag,也可以是镜像ID,如果是tag时,实际上是删除该tag,只要该镜像还有其他tag,就不会删除该镜像。当后面的参数为镜像ID时,则会彻底删除整个镜像,连同所有标签一同删除
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
centos latest bb3d629a7cbc 2 weeks ago
daixuan latest bb3d629a7cbc 2 weeks ago
daixuan12 latest bb3d629a7cbc 2 weeks ago
daixuan123 latest bb3d629a7cbc 2 weeks ago
[root@localhost ~]# docker rmi daixuan123 //删除镜像daixuan123:latest
Untagged: daixuan123:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
centos latest bb3d629a7cbc 2 weeks ago
daixuan latest bb3d629a7cbc 2 weeks ago
daixuan12 latest bb3d629a7cbc 2 weeks ago
[root@localhost ~]# docker rmi daixuan12:latest //加上TAG:lastest 删除镜像daixuan12
Untagged: daixuan12:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
centos latest bb3d629a7cbc 2 weeks ago
daixuan latest bb3d629a7cbc 2 weeks ago
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
daixuan_new latest cbefc7d7e973 9 minutes ago
centos latest bb3d629a7cbc 2 weeks ago
daixuan latest bb3d629a7cbc 2 weeks ago
[root@localhost ~]# docker rmi cbefc7d7e973 //通过IMAGE ID删除镜像
Untagged: daixuan_new:latest
Deleted: cbefc7d7e973f26573bb64e5fa0deb0a64906c375ba8c3de335ee5db82d79827
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
[root@localhost ~]# docker tag centos centos:tag_daixuan//删除tag为tag_daixuan的centos镜像,因为容器库中,centos名称有多个,通过tag才能确定是哪一个
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
centos tag_daixuan bb3d629a7cbc 2 weeks ago 196.6 MB
[root@localhost ~]# docker rmi centos:tag_daixuan //通过名称+tag删除镜像
Untagged: centos:tag_daixuan
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
Docker基于已经有镜像的容器创建镜像
8、启动,关闭镜像,进入容器
[root@localhost ~]# docker stop c49153b5d5b6
c49153b5d5b6
[root@localhost ~]# docker start c49153b5d5b6
c49153b5d5b6
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c49153b5d5b6 centos "/bin/bash" 19 hours ago Up 10 seconds furious_perlman
[root@localhost ~]# docker exec -it c49153b5d5b6 /bin/bash //进入正在运行的容器exec命令
[root@c49153b5d5b6 /]#
ctrl + d 或者exit都是退出一个容器
8、创建镜像:基于已有镜像的容器创建自定义的镜像
运行docker run后,进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像,因为每个运行的镜像(容器)都是只读的,所以想保存,就必须啊保存到新的镜像
#docker commit -m "自定义的commit信息" -a "作者信息" image _id(通过docker images获取id) 新镜像名字
[root@localhost ~]# docker commit -m "centos_with_nettools_and_wget" -a daixuan c49153b5d5b6 centos_with_net_wget
b85b1f09773d34ff7faac672d1072c298c233c527720234b7a774ebe882d2918
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_with_net_wget latest b85b1f09773d About a minute ago 267.8 MB
centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
这个命令有点像svn的提交,-m 加一些改动信息,-a 指定作者相关信息 c49153这一串为容器id,再后面为新镜像的名字
9、创建镜像:基于本地模板(文件)导入
模块获取,可以直接在网上下载一个模块 http://openvz.org/Download/templates/precreated 可惜速度并不快,家人我们下载了一个centos的模板 centos-6-x86.tar.gz 那么导入该镜像的命令为:
cat centos-6-x86-minimal.tar.gz | docker import - centos-6-x86
[root@localhost ~]# cat centos-6-x86-minimal.tar.gz | docker import - centos-6-x86 77e6048b6e121e7b7517f4d0758564724232dda2518b41a78b14f694be6af1be [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos-6-x86 latest 77e6048b6e12 31 seconds ago 324 MB
10、将一个现有的镜像导出为一个文件 - 会保存在当前目录下
# docker save -o 自定义文件名.tar 镜像名
# docker save -o 自定义文件名.tar IMAGE_ID
例:把现有镜像,导出为一个文件,指定导出文件名centos_with_net_wget.tar,源centos_with_net_wget
[root@localhost ~]# docker save -o centos_with_net_wget.tar centos_with_net_wget
[root@localhost ~]# ls
centos_with_net_wget.tar install.log install.log.syslog lanmp.sh shdow.sh
11、将文件恢复到本地镜像 - 创建一个新的镜像
# docker load --input 镜像名.tar
# docker load < 镜像名.tar
例:用该文件恢复本地镜像:
[root@localhost ~]# docker rmi centos_with_net_wget
[root@localhost ~]# docker load < centos_with_net_wget.tar
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
centos latest bb3d629a7cbc 2 weeks ago
--input也可以替换成-i,有时候导入的镜像,docker images显示镜像名和TAG为none 需要用命令修改
#docker tag IMAGE_ID 自定义镜像名:自定义tag
模板导入的images没有tag,所以使用docker tag 添加
[root@localhost ~]# docker tag b85b1f09773d centos_with_net_wget:daixuanlinux
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_with_net_wget daixuanlinux b85b1f09773d About an hour ago 267.8 MB
centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
docker push image_name //可以把自己的镜像传到dockerhub官方网站上去,但前提是需要先注册一个用户,后续如果有需求再研究吧