Docker understand [from here]

First, the configuration of environmental problems


Software development is one of the biggest challenges of the environment configuration problem. Now users the chaotic environment, and due to further promote the open source community and many open source projects iteratively updated constantly, the project may be more and more dependent and rely more and more difficult to manage versions, how to ensure that the program can not write only in the "run on my computer" (It works on my machine) into a complex troublesome thing.

Then someone asked: can not solve the problem fundamentally, so that software can bring their own environment to install? In other words, when installed, the original environment exactly copied over.

virtual machine

VM (virtual machine) is a kind of solution that comes with the installation environment. It can run another operating system on which an operating system, such as Windows system running a Linux operating system.

Application of the virtual machine is no perception, because the virtual machine looks exactly the same with the real operating system, but also for the underlying system, the virtual machine is a regular file, does not need the deleted, there is no impact on other parts of the .

Although the user can restore the original software virtual machine environment. However, this solution has the following disadvantages:

1. resource consumption and more

Virtual chance to monopolize a part of this part of the operating system memory and hard disk space. When it is running, other programs will not be able to use those resources. Even if you want to use the application in a virtual machine inside it is actually only occupy 1MB of memory, virtual machines still need several hundred MB of memory to run.

2. redundant steps and more

A virtual machine is a complete operating system, some of the steps on the system level, often can not be adjusted too, such as user login.

3. Start Slow

Start the operating system how long, how long you need to start the virtual machine. You may have to wait several minutes to let the application up and running true.

Linux container

Because of these disadvantages of the above virtual machine, Linux virtual machine proposed another technique: Linux container (Linux Containers, abbreviated LXC).

Linux container is not a complete simulation of the operating system, but rather the process of isolation. Or, in the normal course it sets out a protective layer . For container inside the process, it is exposed to a variety of virtual resources, enabling the isolation of the underlying system.

Since the container is the process level, the virtual machine has many advantages in comparison.

1. Start fast

Inside the container application, directly underlying system is a process, rather than inside the virtual machine process. So, to start a process container equivalent to start the machine, instead of starting an operating system, speed is much faster.

2. The small footprint

Container only takes up resources needed, do not take those resources are not used; virtual machine because it is complete operating system, it is inevitable to take up all the resources. In addition, multiple containers can share resources, virtual machines are exclusive resources.

3. Small Volume

As long as the container contains components that can be used, while the virtual machine is package an entire operating system, so the container file is much smaller than the virtual machine files.

In short, a bit like a lightweight container virtual machine, can provide a virtualized environment, but cost much less overhead.

Second, what is the Docker?


Enterprise Container Platform for High-Velocity Innovation
Securely build, share and run any application, anywhere

Translation:
Enterprise high-speed container platform
in a safe place construction of any share, run your application

Docker is the official definition of a container platform. Briefly, Docker belongs Linux package container, the container provides an easy to use interface. It is the most popular Linux container solutions. Developers can take advantage of Docker collaboration to eliminate code "on my computer is working properly" problem.

Docker application and dependence of the program, which is packaged in a file. This file is run, it will generate a virtual container. Running in the virtual container, just like running on a real physical machine. With Docker, do not worry about environmental issues.

Overall, Docker fairly simple interface, users can easily create and use a container, put their applications into the container. The container may also be version management, copying, sharing, modifying, managing general code like the same.

Why use Docker?

Docker is a developer and for the operation and maintenance are to develop, publishing platform and applications running in the container. Use containers to deploy Linux applications are called containerized. Container is not a new concept, it has the advantage that it is easy to publish an application.

Containerized become very popular because of the container has the following advantages:

  • Flexible: even the most complex applications can be containerized.
  • Lightweight: container maximize the use and sharing core host.
  • Interchangeable: You can instantly deploy updates and upgrades.
  • Convenient: you can build applications locally, and cloud deployment to the container, and run anywhere.
  • Scalable: You can increase the number of containers and automatic distribution.
  • Stackable: You can stack vertically stacked service and instant service.

In addition to running application container which substantially does not consume additional system resources, such high performance applications, while the overhead of the system as small as possible. Traditional virtual machines run 10 different applications will play 10 virtual machines, while Docker only need to start the application to 10 isolated.

Specifically, Docker has a large advantage in the following aspects.

1. Faster delivery and deployment

The development and operation and maintenance (devop) personnel, the most hope is to create or configure once, run anywhere normal.

Developers can use a standard image to build a development container, after completion of the development, operation and maintenance personnel can use this container to deploy the tag directly. Docker can quickly create a container, fast iterative application, and visible throughout the entire process, so that other members of the team easier to understand how the application is to create and work. Docker containers very light soon! Start time is the second level of the container, a large amount of reducing development, testing, deployment time.

2. More efficient virtualization

Run Docker containers do not require additional hypervisor support, it is kernel-level virtualization, it is possible to achieve higher performance and efficiency.

3. easier migration and expansion

Docker containers can run on almost any platform, including physical machines, virtual machines, public cloud, private cloud, PCs, servers and so on. This compatibility allows users to migrate an application from one platform to another directly.

4. Simpler management

Use Docker, only small changes, you can replace a lot of conventional updating. All modifications are distributed and updated incrementally, enabling automated and efficient management.

Docker VS VM

容器和虚拟机对资源的隔离和分配有相同的优势,但不同的是容器虚拟化操作系统而不是硬件,这使得容器更加的方便和高效。

一个容器是在 Linux 服务本地运行,并和其他的容器共享主机的内核。它运行在一个独立的进程中,相对于其他可执行的进程(比如虚拟机)来说,容器的内存占用空间更小,更加的轻量化。

相比之下,虚拟机(VM)运行一个完整的“客户”操作系统,通过虚拟机管理程序虚拟访问主机资源。 一般来说,虚拟机提供的环境比大多数应用程序需要的资源更多。

由于 Docker 轻量、资源占用少,使得 Docker 可以轻易的应用到构建标准化的应用中。但 Docker 目前还不够完善,比如隔离效果不如 VM,共享宿主机操作系统的一些基础库等;网络配置功能相对简单,主要以桥接方式为主;查看日志也不够方便灵活。

Docker 的用途

Docker 的主要用途,目前有三大类。

1. 提供一次性的环境。比如,本地测试他人的软件、持续集成的时候提供单元测试和构建的环境。

2. 提供弹性的云服务。因为 Docker 容器可以随开随关,很适合动态扩容和缩容。

3. 组建微服务架构。通过多个容器,一台机器可以跑多个服务,因此在本机就可以模拟出微服务架构。

三、Docker 基本概念


下面这张图非常的经典,很形象地展示了,什么是容器,什么是镜像,什么是仓库,以及三者之间的联系。

接下来我们来解释一下这张图。现在我们要造一间厨房,在造之前我们首先要干的一件事,就是先列举出我们造厨房需要的东西。我们可能需要一个通了水电煤的房子以及一些必需的厨房用具诸如锅碗瓢勺、煤气灶、冰箱、水槽等等这些东西。现在我们知道需要了什么东西之后,我们就去找这些东西。首先我们先去京东购买一些厨房用具,这些用具就好比我们的Docker镜像,我们厨房的用具到了之后得找个地方把它们放在,不可能随处丢吧,不然后面用的时候就找不到它了,那么我们Docker镜像也是这样,需要一个Docker仓库去存储这些镜像。现在我们有了这些厨房用具之后就可以做饭了吗?答案当然是不能,没水没电没火啊!这时候我们得把厨房用具给装到一个通了水电煤的房子才行,那么Docker镜像也是这样,单纯的Docker镜像是不能用的,它得装到Docker容器中通了水电煤才能使用。等我们装好了厨房用具之后我们就可以开始做饭,那么我们的Docker镜像装到Docker容器之后,我们应用就可以跑起来了。

概念详解

Docker是CS架构,主要有两个概念:

  • Docker daemon: 运行在宿主机上,Docker守护进程,用户通过Docker client(Docker命令)与Docker daemon交互
  • Docker client: Docker 命令行工具,是用户使用Docker的主要方式,Docker client与Docker daemon通信并将结果返回给用户,Docker client也可以通过socket或者RESTful api访问远程的Docker daemon

了解了Docker的组成,再来了解一下Docker的三个主要概念:

  • Docker image: 镜像是只读的,镜像中包含有需要运行的文件。镜像用来创建container,一个镜像可以运行多个container;镜像可以通过Dockerfile创建,也可以从Docker hub/registry上下载。
  • Docker container: 容器是Docker的运行组件,启动一个镜像就是一个容器,容器是一个隔离环境,多个容器之间不会相互影响,保证容器中的程序运行在一个相对安全的环境中。
  • Docker hub/registry: 共享和管理Docker镜像,用户可以上传或者下载上面的镜像,官方地址为https://registry.hub.docker.com/,也可以搭建自己私有的Docker registry。

镜像就相当于打包好的版本,镜像启动之后运行在容器中,仓库就是装存储镜像的地方。

四、Docker 安装


安装过程都大同小异,这里就不细说了,详细的可以参考这里:https://www.runoob.com/docker/macos-docker-install.html

唯一需要注意的就是由于国内网络问题,需要给 Docker 镜像加个速(详细的可以戳上面链接,最后有...)

五、Docker Hello World


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

$ docker image pull library/hello-world

上面代码中,docker image pull是抓取 image 文件的命令。library/hello-world是 image 文件在仓库里面的位置,其中library是 image 文件所在的组,hello-world是 image 文件的名字。

由于 Docker 官方提供的 image 文件,都放在library组里面,所以它的是默认组,可以省略。因此,上面的命令可以写成下面这样。

$ docker image pull hello-world

抓取成功以后,就可以在本机看到这个 image 文件了。

$ docker image ls

现在,运行这个 image 文件

$ docker container run hello-world

docker container run 命令会从 image 文件,生成一个正在运行的容器实例。

注意,docker container run 命令具有自动抓取 image 文件的功能。如果发现本地没有指定的 image 文件,就会从仓库自动抓取。因此,前面的 docker image pull 命令并不是必需的步骤。

如果运行成功,你会在屏幕上读到下面的输出。

$ docker container run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

... ...

输出这段提示以后,hello world 就会停止运行,容器自动终止。

有些容器不会自动终止,因为提供的是服务。比如,安装运行 Ubuntu 的 image,就可以在命令行体验 Ubuntu 系统。

$ docker container run -it ubuntu bash

对于那些不会自动终止的容器,必须使用docker container kill 命令手动终止。

$ docker container kill [containID]

六、常用命令


  • 杀死所有正在运行的容器
    docker kill $(docker ps -a -q)
  • 删除所有已经停止的容器
    docker rm $(docker ps -a -q)
  • 删除所有镜像
    docker rmi $(docker images -q)
  • 关闭容器
    docker stop CONTAINER ID或者NAMES
  • 重新启动关闭的容器
    docker start CONTAINER ID或者NAMES
  • 移除本地容器
    docker rm CONTAINER ID或者NAMES
  • 查看本地容器
    docker ps //查看正在运行的容器
    docker ps -a //查看所有容器
  • 查看本地镜像
    docker images
  • 创建镜像
    docker build -t name:tag Dockerfile路径
  • 修改本地镜像标记
    docker tag IMAGE ID name:tag
    docker rmi name:tag
  • 删除本地镜像
    docker rmi name:tag或者IMAGE ID
  • 进入容器
    docker exec -it IMAGE ID或者NAMES /bin/bash
  • 获取镜像中心的镜像
    docker pull name:tag
  • 获取容器的端口映射配置
    docker port CONTAINER ID或者NAMES

# 参考资料

1. Docker入门教程(阮一峰) - http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
2. Docker(一):Docker入门教程(纯洁的微笑) - https://www.cnblogs.com/ityouknow/p/8520296.html
3. Docker教程(菜鸟教程) - https://www.runoob.com/docker/docker-tutorial.html
4. - Docker入门,看这篇就够了 - https://segmentfault.com/a/1190000009544565#articleHeader6
5. 官方文档 - https://www.docker.com


按照惯例黏一个尾巴:

欢迎转载,转载请注明出处!
简书ID:@我没有三颗心脏
github:wmyskxz
欢迎关注公众微信号:wmyskxz
分享自己的学习 & 学习资料 & 生活
想要交流的朋友也可以加qq群:3382693

Guess you like

Origin www.cnblogs.com/wmyskxz/p/10943169.html