先分享一下构建好的image(源码部署的Nginx) https://hub.docker.com/r/xxbandy123/centos/
php
本地docker image http://pan.baidu.com/s/1dDtux6h java
为何要提起docker呢?在当前大数据盛行的环境下,数据的处理量越来越多,如何可以让应用快速的部署,消耗的资源更少,这些将是互联网企业须要考虑的问题。随而就兴起了相关云计算平台。
众所周知的是云计算平台无非分为三个方向,IAAS,PAAS,SAAs,。那么国内,在SAAS平台上能够说已经
作到了。好比QQ,微信,等等包括当前的一些企业应用均可以看作SAAS的应用。在IAAS,和PAAS一直没有太多的关注,而在中国也没有太大的发展。
近两年来,国内IAAS环境作的还算不错,毕竟做为基础设施即服务层,当前的阿里云,青云等等国内基础设施仍是作的比较到位。能够为小中型企业提供至关完备的相关基础设施服务。
而做为PAAS平台,最近一辆发展的也比较火,最为受欢迎的当数容器平台docker了。node
那么什么是docker呢?docker又能为咱们带来什么呢?mysql
Docker 是一个开源的应用容器引擎,让开发者能够打包他们的应用以及依赖包到一个可移植的容器中,而后发布到任何流行的 Linux 机器上,也能够实现虚拟化。容器是彻底使用沙箱机制,相互之间不会有任何接口(相似 iPhone 的 app)。几乎没有性能开销,能够很容易地在机器和数据中心中运行。最重要的是,他们不依赖于任何语言、框架包括系统。
linux
什么是容器?有哪些现有的容器技术?Docker与其余容器技术的不一样是什么?
从宏观角度来看,容器更像是轻量化的虚拟机。你能够在容器中安装任何你想要的软件,独立且不影响其余容器或者宿主环境。每个容器有它本身的网络 堆栈、进程空间、文件系统等。同时,他们的影响要远小于虚拟机:容器启动更快、占用更少的内存和硬盘空间。从底层角度来看,这都是由于容器只是宿主机上的 一个进程,利用内核特征如命名空间和组管理来提供这种隔离。启动一个容器只是启动了一个普通的UNIX进程;建立一个容器只是复制了一个copy-on- wirte文件系统的镜像。
nginx
Docker与其余容器技术不一样,由于它不仅是一个容器引擎。Docker是一个平台,整合了Docker引擎、Docker Hub以及一系列的生态工具,如Docker Compose、Docker Machine、Docker Swarm等。这些都采用开发API。git
Docker用到的技术:web
传统虚拟化技术和Docker技术:sql
Docker与其余hypervisor虚拟技术的不一样之处?
有人说Docker是是一个“容器的hypervisor”,可是许多人并不这么认为,这是由于hypervisor一般是管理虚拟机,而 Docker是管理容器。在深层技术细节也是不一样的。当一个hypervisor启动虚拟机,它建立虚拟硬件,并利用特定CPU基础结构和特性,如VT- x,AMD-x或者权限层级。当Docker建立一个容器,它利用的是内核的特性,如命名空间、群组管理,而不依赖硬件特性。
这意味着容器在某种意义上说更具备可移植性,由于它们能够同时在物理机或者虚拟机上运行;可是他们从另外某种意义上来讲,也更不容易移植,由于容 器必须使用宿主的内核。这意味着,你不能运行一个Windows容器在Linux内核上(除非linux内核能够执行Windows二进制程序)。docker
Docker的重要组成部分:
Docker image(镜像):相似于虚拟机镜像,存放一个虚拟机配置信息的只读模板文件(json格式),且包含文件系统!镜像是建立Docker容器的基础。经过版本管理和增量的文件系统,Docker提供了一套十分简单的机制来建立和更新现有的镜像,用户甚至能够从网上下载一个已经作好的应用镜像,并经过简单的命令就能够直接使用。
Docker container(容器):相似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用,所以每个容器里的应用都是相互隔离的,每个容器也都是一个独立的进程,不可见的,只有宿主机可以管理和查看相关镜像和容器。容器是从镜像建立的应用运行实例,能够将其启动、开始、中止、删除。镜像自身是只读的。容器从镜像启动的时候,Docker会在镜像的最上层建立一个可写层,镜像自己将保持不变。
Docker registry(注册中心):相似于代码仓库,是Docker集中存放镜像文件的场所,也就是dockerHub(其实docker在设计的时候就采用git的思路,好比commit,pull,push以及Hub等操做,所以,在使用过git后,很容易理解啦)。
根据所存储的镜像公开分享与否,Docker仓库能够分为公开仓库(Public)和私有仓库(Private)两种形式。
目前,最大的公开仓库是Docker Hub,存放了数量庞大的镜像供用户下载。国内的公开仓库包括Docker Pool等,能够提供稳定的国内访问。
固然,本身也能够在本地建立一个本身的私有仓库咯!
Docker实战:
1、Docker的安装部署:
$ cat >/etc/yum.repos.d/docker.repo <<-EOF
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
[root@xuxuebiao yum.repos.d]#yum install docker-engine libcgroup zx -y
[root@xuxuebiao yum.repos.d]# /etc/init.d/docker restart
Stopping docker: [FAILED]
Starting cgconfig service: [ OK ]
Starting docker: [ OK ]
测试docker:
[root@xuxuebiao yum.repos.d]# docker run hello-world 运行hello-world镜像,由于没有回自动pull最新镜像
。。。。。。。。。。
[root@xuxuebiao yum.repos.d]# docker images 查看到最新下载的镜像
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest af340544ed62 9 days ago 960 B
查看hello-world相关的镜像:(官方自带测试镜像)
[root@xuxuebiao yum.repos.d]# docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Docker... 17 [OK]
tutum/hello-world Image to test docker deployments. Has Apac... 11 [OK]
vegasbrianc/docker-hello-world 1 [OK]
sbasyal/java-hello-world 1 [OK]
joshuaconner/hello-world-docker-bottle 1 [OK]
bonomat/nodejs-hello-world 1 [OK]
alexwelch/hello-world 0 [OK]
rcarun/hello-world 0 [OK]
chunyunchen/ruby-hello-world 0 [OK]
keithchambers/docker-hello-world 0 [OK]
kocopepo/docker-hello-world 0 [OK]
lanaplexus/docker-hello-world 0 [OK]
sabya1979/java-hello-world 0 [OK]
adamkdean/hello-world 0 [OK]
dockerman99/hello-world 0 [OK]
vasia/docker-hello-world rhrthrth 0 [OK]
kisp/docker-hello-world 0 [OK]
gardleopard/docker-hello-world 0 [OK]
openshift/ruby-hello-world 0 [OK]
fermayo/hello-world 0 [OK]
nirmata/hello-world 0 [OK]
davelesser/hello-world 0 [OK]
marcells/aspnet-hello-world ASP.NET vNext - Hello World 0 [OK]
crccheck/hello-world Hello World web server in under 2.5 MB 0 [OK]
milkyway/java-hello-world Java Hello World Test Docker Image 0 [OK]
2、docker简单使用:
镜像的获取,能够从官方进行下载最新的版本,也能够在第三方docker的images共享上进行pull。
http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974865 (ali 的docker镜像资源。汗,是基于本身的云上的资源,外人不能下载。。。。)
国内dockerHub下载地址:http://dockerpool.com/downloads
查看centos镜像相关镜像
[root@xuxuebiao yum.repos.d]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 1201 [OK] (最新的centos镜像了,因此就使用这个咯!)
ansible/centos7-ansible Ansible on Centos7 48 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.6 x86_64 / Apache / PHP / PHP m... 11 [OK]
blalor/centos Bare-bones base CentOS 6.5 image 9 [OK]
openshift/wildfly-8-centos DEPRECATED - see openshift/wildfly-81-centos7 6 [OK]
torusware/speedus-centos Always updated official CentOS docker imag... 6 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 5 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 4 [OK]
jdeathe/centos-ssh CentOS-6 6.6 x86_64 / EPEL/IUS Repos / Ope... 4 [OK]
tcnksm/centos-node Dockerfile for CentOS packaging node 2 [OK]
jdeathe/centos-ssh-mysql CentOS-6 6.6 x86_64 / MySQL. 2 [OK]
lighthopper/orientdb-centos A Dockerfile for creating an OrientDB imag... 1 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 1 [OK]
layerworx/centos CentOS container with etcd, etcdctl, confd... 1 [OK]
nathonfowlie/centos-jira JIRA running on the latest version of CentOS 1 [OK]
yajo/centos-epel CentOS with EPEL and fully updated 1 [OK]
centos/mariadb55-centos7 1 [OK]
insaneworks/centos CentOS 6.5 x86_64 + @update 0 [OK]
lighthopper/openjdk-centos A Dockerfile for creating an OpenJDK image... 0 [OK]
jasonish/centos-suricata Suricata base image based on CentOS 7. 0 [OK]
pdericson/centos Docker image for CentOS 0 [OK]
akroh/centos Centos 6 container that has been updated w... 0 [OK]
dmglab/centos CentOS with superpowers! 0 [OK]
jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK]
blacklabelops/centos Blacklabelops Centos 7.1.503 base image wi... 0 [OK]
注意:以上有官方的信息以及相关image的名称,以及星级(受欢迎度)
获取centos最新镜像(不加后面的:latest默认下载最新镜像)
[root@xuxuebiao yum.repos.d]# docker pull centos:latest
latest: Pulling from centos
f1b10cd84249: Pull complete
c852f6d61e65: Pull complete
7322fbe74aa5: Pull complete
Digest: sha256:90305c9112250c7e3746425477f1c4ef112b03b4abe78c612e092037bfecc3b7
Status: Downloaded newer image for centos:latest
查看全部镜像
[root@xuxuebiao yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest af340544ed62 9 days ago 960 B
centos latest 7322fbe74aa5 8 weeks ago 172.2 MB
运行docker进入一个shell,其实如今就已经开启了一个shell的容器了。
[root@xuxuebiao yum.repos.d]# docker run -i -t centos /bin/bash 进入docker的shell(直接启动一个容器并进入)
[root@465d13a11ffa /]#
注意;本章使用的是docker1.7版本,所以能够直接切换到shell中
(在2015年8月14号好像已经升级到1.8了,不过须要内核的支持哦)
测试这个容器和外网联通,如今你就能够想在真正的linux里面来操做了,想装什么就装什么
[root@465d13a11ffa yum.repos.d]# ping baidu.com
PING baidu.com (220.181.57.217) 56(84) bytes of data.
64 bytes from 220.181.57.217: icmp_seq=1 ttl=48 time=42.1 ms
那么如今就可使用centos这个镜像启动的容器(465d13a11ffa)进行搭建一个nginx服务器了。(http://my.oschina.net/xxbAndy/blog/493184 )
查看当前运行的容器,以及对应运行的相关镜像和命令(须要注意的是查看container的ID)
[root@xuxuebiao ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
465d13a11ffa centos "/bin/bash" 5 minutes ago Up 5 minutes pensive_lovelace
5a2f72348903 hello-world "/hello" 32 minutes ago Exited (0) 32 minutes ago elated_davinci
表示如今有两个容器!分别由centos镜像和hello-world镜像生成
将刚才本身构建好的Nginx源码容器(465d13a11ffa)打包成新的images(注意了,必须得在容器没有退出的时候进行建立新的image,不然退出后以前的image仍是以前的)
[root@xuxuebiao ~]# docker commit 465d13a11ffa centos-nginx_by-xuxuebiao
0e76b29fa3b66db2f3d1354d123c7094122083b2289dc1b5cfe1dabdc89cefdf
也就是说如今300M构建了Nginx服务器!的镜像
[root@xuxuebiao ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos-nginx_by-xuxuebiao latest 0e76b29fa3b6 26 seconds ago 301.2 MB
hello-world latest af340544ed62 9 days ago 960 B
centos latest 7322fbe74aa5 8 weeks ago 172.2 MB
给本身的image打标签
[root@xuxuebiao ~]# docker tag centos:latest mytestlinux:base
[root@xuxuebiao ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos-nginx_by-xuxuebiao latest 0e76b29fa3b6 3 minutes ago 301.2 MB
hello-world latest af340544ed62 9 days ago 960 B
centos latest 7322fbe74aa5 8 weeks ago 172.2 MB(这两个实际上是一个软连接的过程哦!)
mytestlinux base 7322fbe74aa5 8 weeks ago 172.2 MB
查看镜像的详细信息,其实就是一个json文件(只读的系统模板)而已!(而使用dockefile其实应该就是用json写)
#docker inspect image-ID 查看镜像的详细信息
[root@xuxuebiao ~]# docker inspect 465d13a11ffa(镜像id) | less
[
{
"Id": "465d13a11ffa2d6a9d3a594b98c9903cce751fb9c9f58d6ee142b26aabdb772f",
"Created": "2015-08-16T06:20:36.95544219Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1971,
"ExitCode": 0,
"Error": "",
"StartedAt": "2015-08-16T06:20:37.781513999Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "7322fbe74aa5632b33a400959867c8ac4290e9c5112877a7754be70cfe5d66e9",
"NetworkSettings": {
"Bridge": "",
"EndpointID": "2ad4474d8540c045a3c14e2d2e2fe324d9ef51c512cee1ac671a39c19c6c43a6",
"Gateway": "10.0.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "10.0.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"MacAddress": "02:42:0a:00:00:02",
"NetworkID": "3a7feff16f069e0f1ced69b41b8cc12f3d9c4be55455f2680a5ce4a823a553e3",
"PortMapping": null,
"Ports": {},
:
[root@465d13a11ffa nginx]# exit
exit
退出由centos:latest建立的/bin/shell容器。
使用docker镜像建立容器和使用容器完毕!
3、Docker对容器和镜像进行管理
怎样对容器和镜像进行管理呢?
删除镜像:docker rmi
[root@xuxuebiao ~]# docker rmi -f 465d13a11ffa (xxbandy/latest) 最好不要建议使用docker rmi -f 强制删除
注意:有的镜像是打了tag的,若是删除的时候必定要留下一个最终的image,不然数据也就删除咯!
建立镜像的三种方法:
1.基于已有镜像的容器建立(commit)(以上实验中作的)
2.基于本地模板导入
3.基于dockerfile文件建立
1.基于已有镜像建立:#docker commit 选项 描述 镜像信息
[root@xuxuebiao ~]# docker commit -m "Nginx-test" -a "xuxuebiao" 6d0be722dcb1(容器ID) centos:nginx 基于已经运行的容器进行建立image
712d266e4b2ef6554d001a53d2b6969f4e6a67ed22816b10b422ffa2c0c80973
-m 提交信息 -a做者信息 -p 提交时暂停容器运行
[root@xuxuebiao ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos nginx 712d266e4b2e 56 seconds ago 301.2 MB
centos-nginx_by-xuxuebiao latest 0e76b29fa3b6 20 minutes ago 301.2 MB
hello-world latest af340544ed62 9 days ago 960 B
centos latest 7322fbe74aa5 8 weeks ago 172.2 MB
mytestlinux base 7322fbe74aa5 8 weeks ago 172.2 MB
(须要注意的是,上面五个镜像中,四个分别至关因而硬连接和软链接)
[root@xuxuebiao ~]# docker inspect 712d266e4b2e(镜像ID) 查看centos:nginx的详细信息(做者了等等)
[
{
"Id": "712d266e4b2ef6554d001a53d2b6969f4e6a67ed22816b10b422ffa2c0c80973",
"Parent": "0e76b29fa3b66db2f3d1354d123c7094122083b2289dc1b5cfe1dabdc89cefdf",
"Comment": "Nginx-test",
"Created": "2015-08-16T08:25:00.380756797Z",
"Container": "6d0be722dcb181bce33189f980e41d49e26ead26999cf32d9912aabce932eb3c",
"ContainerConfig": {
"Hostname": "6d0be722dcb1",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"PortSpecs": null,
"ExposedPorts": null,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": null,
"Cmd": [
"/bin/bash"
],
"Image": "0e76b29fa3b6",
"Volumes": null,
"VolumeDriver": "",
"WorkingDir": "",
"Entrypoint": null,
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.7.1",
"Author": "xuxuebiao",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"PortSpecs": null,
"ExposedPorts": null,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": null,
"VolumeDriver": "",
"WorkingDir": "",
"Entrypoint": null,
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 0,
"VirtualSize": 301236984
}
]
2.基于本地模板导入镜像:
#cat centos.minimal.tar.gz | docker import - centos:latest
存储和载入镜像;
存出镜像:将镜像文件保存到本地tar文件
[root@xuxuebiao ~]# docker save -o centos-nginx.tar centos:nginx
[root@xuxuebiao ~]# ls
centos-nginx.tar
载入镜像:将本地tar镜像文件导入本地镜像库
[root@xuxuebiao ~]# docker load < centos-nginx.tar
或者使用#docker load --input centos.tar
上传镜像:
默认上传到dockerhub网站上,(相似于git了啦),先进行tag打标签,而后使用push进行上传。
#docker tag centos:nginx xxbandy/centos:nginx (能够区分一下那个是标签,那个是真正的image)
#docker push xxbandy/centos:nginx(必须有dockhub的用户名和密码啊|则dockerhub的用户名是xxbandy,仓库是centos:nginx)
注意,为何要打tag标签呢,就是为了可以将刚才的镜像成功的上传到本身的dockerhub的repo里面,所以这个标签的规则和hub上的是一致的!
好比:
[root@xuxuebiao ~]# docker push xxbandy123/centos (个人用户名为xxbandy123 centos是个人repository)
The push refers to a repository [xxbandy123/centos] (len: 1)
712d266e4b2e: Image already exists
712d266e4b2e: Image already exists
0e76b29fa3b6: Image already exists
7322fbe74aa5: Image already exists
c852f6d61e65: Image already exists
Digest: sha256:0101b798bfddf10774132934dcd747002e73dfd325a85604299a89027d0d7a28
思考:在作了这么多试验,使用docker ps -a 会发现存在不少容器,那么容器又须要怎样管理呢?
感谢你们关注,干活下次继续!(Dockerfile建立镜像、网络的封装、逻辑卷的使用、注册中心的配置、容器后台启动以及调用!。。。。。 )
注意:转载请注明来源地址。欢迎交流!