Docker virtual containers

A, Docker

1, the concept of

  Docker is an application container engine open source, so developers can package their applications and dependencies into a portable container and then posted to any popular Linux machine or Windows machine, can be virtualized, the container is completely use the sandbox mechanism will not have any interface with each other.

2, docker application scenarios

  • Automating the packaging and deployment of applications (the application packaging and deployment automation)
  • Creation of lightweight, private PAAS environments (to create a lightweight, intimate environment PAAS)
  • Automated testing and continuous integration / deployment (automated testing and continuous integration / deployment)
  • Deploying and scaling web apps, databases and backend services (deployment and expansion webapp, databases and back-office services)
  Cloud computing, big data and rapid development, mobile technology, coupled with changing business needs, leading enterprise architecture to change at any time to suit the business needs to keep pace with newer technology. There is no doubt that these will bear the burden of all corporate developers who; how efficient coordination between the team, the rapid delivery of products, quickly deploy applications, and to meet business requirements, developers are needed to solve the problem. Docker technology just can help developers solve these problems.
  In order to solve the collaborative relationship between developers and the operation and maintenance personnel to accelerate the speed application delivery, more and more companies introduced the concept of DevOps. However, the traditional development process, development, testing, operation and maintenance of three independent teams, poor communication between the teams, there occurred a conflict between the development of operation and maintenance, resulting in low efficiency of collaboration, product delivery delays affecting the company's business operation. Docker technology will be applied in the manner of packing container delivery, the application sharing in different teams, by mirroring the way the application can be deployed in any environment. This avoids the problem of the emergence of collaboration between the team and become an important tool for enterprises to achieve the goal of DevOps. Docker containers delivered to the way technical support continue to develop iterations, greatly enhance the product development and delivery speed.
  Further, by the underlying device virtualization Hypervisor virtual machine different, Docker transplanted directly on top of the Linux kernel, the underlying process by running Linux virtual isolation device, so that the system performance loss is lower than the virtual machine, almost ignore. At the same time, start and stop Docker application container is very efficient and can support large-scale horizontal expansion of the distribution system, and really bring the Gospel to the enterprise development.

3, with the difference docker virtual machine

virtual machine:

  • Infrastructure (Infrastructure) . It could be your PC , the data center server , or cloud host .
  • 虚拟机管理系统(Hypervisor)。利用Hypervisor,可以在主操作系统之上运行多个不同的从操作系统。类型1的Hypervisor有支持MacOS的HyperKit,支持Windows的Hyper-V、Xen以及KVM。类型2的Hypervisor有VirtualBox和VMWare workstation。
  • 客户机操作系统(Guest Operating System)。假设你需要运行3个相互隔离的应用,则需要使用Hypervisor启动3个客户机操作系统,也就是3个虚拟机。这些虚拟机都非常大,也许有700MB,这就意味着它们将占用2.1GB的磁盘空间。更糟糕的是,它们还会消耗很多CPU和内存。
  • 各种依赖。每一个客户机操作系统都需要安装许多依赖。如果你的应用需要连接PostgreSQL的话,则需要安装libpq-dev;如果你使用Ruby的话,应该需要安装gems;如果使用其他编程语言,比如Python或者Node.js,都会需要安装对应的依赖库。
  • 应用。安装依赖之后,就可以在各个客户机操作系统分别运行应用了,这样各个应用就是相互隔离的。

docker:

  • 基础设施(Infrastructure)
  • 主操作系统(Host Operating System)。所有主流的Linux发行版都可以运行Docker。对于MacOS和Windows,也有一些办法”运行”Docker。
  • Docker守护进程(Docker Daemon)。Docker守护进程取代了Hypervisor,它是运行在操作系统之上的后台进程,负责管理Docker容器。
  • 各种依赖。对于Docker,应用的所有依赖都打包在Docker镜像中,Docker容器是基于Docker镜像创建的。
  • 应用。应用的源代码与它的依赖都打包在Docker镜像中,不同的应用需要不同的Docker镜像。不同的应用运行在不同的Docker容器中,它们是相互隔离的。

 区别:

  虚拟机多了一层guest OS,同时Hypervisor会对硬件资源进行虚拟化。docker直接使用硬件资源,适用的平台更广、启动速度快、资源利用率高。

二、Docker生命周期

  docker的整个生命周期有三部分组成:镜像(image)+容器(container)+仓库(repository)。容器是由镜像实例化而来,这和我们学习的面向对象的概念十分相似,我们可以把镜像看作类,把容器看作类实例化后的对象。镜像是文件, 容器是进程。 容器是基于镜像创建的, 即容器中的进程依赖于镜像中的文件, 这里的文件包括进程运行所需要的可执行文件, 依赖软件, 库文件, 配置文件等等。

1,docker镜像

   镜像(Image)就是一堆只读层(read-only layer)的统一视角,存在于/var/lib/docker/aufs目录下。

2,docker容器

   容器(container)的定义和镜像(image)几乎一模一样,也是一堆层的统一视角,唯一区别在于容器的最上面那一层是可读可写的。容器 = 镜像 + 读写层。并且容器的定义并没有提及是否要运行容器。 

三、基本命令

1,操作docker镜像

以下[image]含义皆为:镜像+版本号,即可写作[image:tag],不写tag则会使用默认tag:latest

docker pull [image]    从仓库拉取/更新镜像

docker push [image]    推送镜像到仓库

docker images    查看本地所有镜像

docker search [image]    从仓库搜索相关镜像

docker rmi [image]    删除镜像

docker save [image] > xxxx.tar    保存镜像到文件

docker load < [image]    加载文件镜像到环境

docker history [image]    镜像操作历史

2,操作docker容器

docker run -itd --name cent -p 80:80 [image] /bin/bash    通过镜像创建并启动一个容器

  -i:交互式操作

  -t:终端

  -d:以deamon守护进程的方式运行

  --name:容器名称,可以不使用id号,用名称对容器进行操作

  -p:端口映射,本机端口:容器端口

  /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash

docker start [container]    启动容器

docker stop [container]    关闭容器

docker restart [container]    重启容器

docker exec -it [container] /bin/bash    连接到正在运行的容器

docker rm [container]    删除容器

docker ps -a    查看所有容器,不加-a为查看正在运行的容器

docker rm `ps -a -q `    删除所有容器

docker inspect [container]    查看容器配置信息

docker commit -m [comment] [container] imageName:tag     将容器保存为本地镜像

docker export [container] > xxxx.tar    保存容器到文件

docker import < xxxx.tar    导入容器到环境

镜像导入和容器导入的区别:

1,容器导入 是将当前容器 变成一个新的镜像

2,镜像导入 是复制的过程

save 和 export区别:

1,save 保存镜像所有的信息-包含历史

2,export 只导出当前的信息

应用案例

脚本:init.sh

1 docker run -itd --name app -p 3306:3306 -p 8080:8080 app:v1.0 /bin/bash

脚本:exec.sh

1 docker exec -it app /bin/bash

脚本:start.sh

1 docker start app

脚本:stop.sh

1 docker stop app

 

Guess you like

Origin www.cnblogs.com/guanghe/p/12166675.html