Docker Getting Started - Notes -1

Docker entry

Docker is written Golang, since its launch in 2013, more and more attention of developers. If you are concerned about the latest technological developments, then you must have heard Docker. Whether cloud service or micro-services (Microservices), more and more manufacturers are beginning to Docker as on infrastructure automation tools. So what is the Docker? Docker with traditional virtual machine What is the difference? Why adopt Docker? How to use the Docker?

In this paper, it is for the above mentioned problems, a brief introduction to Docker.

What is the Docker

Docker is an open source application container engine.

Docker lets you since all application software and packaging it into a standardized unit of software development.

All documents Docker container will run the installation software, and it required (the code is running, system tools, system libraries) packaged together, which ensures that no matter what kind of operating environment, always run in the same way . If the Java virtual machine as "write once (Write once, run anywhere)", while Docker was "build once, run everywhere (Build once, run anywhere)".

img

Docker is a "container as a Service" (Docker Containers as a Service, referred CaaS), enables developers and IT operations teams can use to build, distribute, run more agile and controllable.

In a nutshell: Docker is for developers and system administrators to build, publish, and run an open platform for distributed applications. The platform consists of Docker engine (a portable, lightweight runtime and packaging tool) and Docker Hub (a shared application and cloud service workflow automation) and other components. Docker can quickly assemble applications from components and eliminates the development problem of friction between quality assurance and production environments. As a result, IT departments can quickly publish, no matter which application is running in a virtual machine notebook computers, data center, or any of the cloud, its operation and the results are the same.

Let us look at Docker's Logo. Obviously, this is a whale, it is holding a number of containers. We can host may as whale, the container can be seen as isolated containers, each container contains its own applications. This Logo is simply a wonderful metaphor!

img

Docker's advantage

  • Lightweight: All containers share the same operating system kernel on a machine, so they start immediately, and more efficient use of memory. Image is built from a hierarchical file system so they can share a common file that downloads and Image disk usage more efficient.
  • Open: Docker container is a standards-based development, allowing the vessel to run in mainstream Linux distributions and Microsoft operating systems as all the infrastructure.
  • Security: container enables applications isolated from each other, and infrastructure while providing an extra layer of protection for your application.

Docker difference between the virtual machine

Container virtual machine has similar advantages isolation and resource allocation, but different architectural approaches the container to be more portable and efficient.

Virtual machine architecture

img

Each virtual machine including applications, the necessary binaries and libraries as well as a complete client operating system (Guest OS), although they are separated, they share and use of hardware resources of the host, need nearly the size of a dozen GB.

Container architecture

img

Container including the application and all of its dependencies, but shared kernel and other containers. They run on the host operating system to form a separate user space process. They are not dependent on any particular infrastructure, Docker containers can be run on any computer, on any infrastructure and any cloud.

Docker containers use the LXC, management utilizes namespaces do permission control and isolation, cgroups to allocation of resources, and also by aufs to further improve the resource utilization of the file system, and these technologies are not Docker original.

LXC

The difference LXC virtual machine is that it is an operating system-level virtualization environment, rather than hardware virtualization environment. They all do the same thing, but LXC is operating system-level virtualization environment, virtual environment has its own process and network space, instead of creating a complete mature virtual machine. Therefore, a virtual operating system LXC have minimal resource requirements, and takes only a few seconds.

正如你可以在下图中看到的,左侧是 LXC 虚拟的 Ubuntu ,默认安装使用 11 MB 大小。

img

Docker 与 Microservices 的关系

Microservices(微服务) 依赖于“基础设施自动化”,而 Docker 正是“基础设施自动化”的利器。可以说 Docker 的火爆,一定程度上也带动了微服务架构的兴起,而微服务的广泛应用也促进了 Docker 繁荣。可以说两者相辅相成。

有关微服务的介绍,可以移步至《简述 Microservices(微服务)》。

http://www.importnew.com/24651.html

为什么要用 Docker

开发更加敏捷:Docker 让开发人员可以自由定义环境,创建和部署的应用程序更快、更容易,IT 运维人员快速应对变化也更加灵活性。

  • 更加可控:Docker 使得开发人员保存从基础设施到应用的代码,帮助 IT 运维人管理拥有标准的、安全的、可扩展的操作环境。
  • 高可移植性:Docker 允许自由选择,可以是从笔记本电脑到一个团队,从私人基础设施到公共云提供商。

这样,你可以专注于开发应用,其他的繁琐事交给 Docker 去做吧。

如何使用 Docker

这可真是一个大话题,如果完整阐述足够写一本书了。好在我们的目的是做入门普及,因此我们就简单讲一下 Docker 的安装、基本使用和常用命令。

Docker安装

建议在linux环境下安装Docker,window环境搭建比较复杂且容易出错,使用Centos7+yum来安装Docker环境很方便。

Docker 软件包已经包括在默认的 CentOS-Extras 软件源里。因此想要安装 docker,只需要运行下面的 yum 命令:

yum install docker

安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:

service docker start
chkconfig docker on

LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:

service docker start
chkconfig docker on

测试

docker version

输入上述命令,返回docker的版本相关信息,证明docker安装成功。

Hello World镜像初试

下面,我们通过最简单的 image 文件"hello world",感受一下 Docker。

因为国内连接 Docker 的官方仓库很慢,因此我们在日常使用中会使用Docker 中国加速器。通过 Docker 官方镜像加速,中国区用户能够快速访问最流行的 Docker 镜像。该镜像托管于中国大陆,本地用户现在将会享受到更快的下载速度和更强的稳定性,从而能够更敏捷地开发和交付 Docker 化应用。

Docker 中国官方镜像加速可通过 registry.docker-cn.com访问。该镜像库只包含流行的公有镜像,私有镜像仍需要从美国镜像库中拉取。

修改系统中docker对应的配置文件即可,如下:

vi /etc/docker/daemon.json
#添加后
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true
}

运行下面的命令,将 image 文件从仓库抓取到本地。

docker pull library/hello-world

上面代码中,docker image pull是抓取 image 文件的命令。library/hello-world是 image 文件在仓库里面的位置,其中library是 image 文件所在的组,hello-world是 image 文件的名字。
抓取成功以后,就可以在本机看到这个 image 文件了。

docker images

#显示结果
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest f2a91732366c 3 months ago 1.848 kB

现在,运行这个 image 文件。

docker run hello-world
#显示结果
Hello from Docker!
This message shows that your installation appears to be working correctly.

输出这段提示以后,hello world就会停止运行,容器自动终止。有些容器不会自动终止,因为提供的是服务,比如Mysql镜像等。

常用命令

除过以上我们使用的Docker命令外,Docker还有一些其它常用的命令。

拉取docker镜像

docker pull image_name

查看宿主机上的镜像,Docker镜像保存在/var/lib/docker目录下:

docker images

删除镜像

docker rmi docker.io/tomcat:7.0.77-jre7 或者 docker rmi b39c68b7af30

查看当前有哪些容器正在运行

docker ps

查看所有容器

docker ps -a

启动、停止、重启容器命令:

docker start container_name/container_id
docker stop container_name/container_id
docker restart container_name/container_id

后台启动一个容器后,如果想进入到这个容器,可以使用attach命令:

docker attach container_name/container_id

删除容器的命令

docker rm container_name/container_id

查看当前系统Docker信息

docker info

从Docker hub上下载某个镜像:

docker pull centos:latest

Docker pull centos will perform this warehouse Centos following all the mirrors downloaded to the local repository.

Guess you like

Origin www.cnblogs.com/passzhang/p/12059217.html