10 minutes to quickly master the necessary basics Docker

DockerContainer technology is popular nowadays, I believe that as a developer, you must have heard or used, many people will Dockerbe understood as a lightweight virtual machine, but in fact Dockerwith the virtual machine ( VM) are two different virtual computers technology, there are a lot of people would think, with the virtual machine, then why use Dockerit?

With a little bit of doubt in my heart, let us learn Dockerit.

No original's virtualization technology

We think about it, in the "ancient" is not calculated's virtualization technology, if we want to deploy an application ( Application), the general procedure is kind of how?

The first step must be first to prepare a physical server, then install an operating system on a physical server ( Operating System), the arrival of the operating system, then install our application running on the operating system, this process can use the following chart To represent:

Physical schematic deployment server application

So, in this way what is the problem? In fact, deploy applications on a physical machine has the following disadvantages:

  • Very slow deployment : because we have to prepare the server hardware, and then have to install the operating system, and then deploy the application, and the application software, there are many dependencies, so the process is relatively slow.

  • The cost is very high : mainly physical device cost is too high, even for the deployment of a simple application, but also need a server.

  • Waste of resources : If the application is too simple, too easy to waste hardware resources, such CPUand memory

  • Migration and expansion is too slow : If you need to migrate applications or extensions, must then prepare other physical servers, the process is cumbersome, and very slow.

So what is the way to solve these problems? The answer is virtualization technology.

Use the virtual machine's deployment of applications

What is virtualization technology

Virtualization technology comes to computers, we thought is a direct virtual machines, virtual machines allow us to simulate in a single physical computer out of multiple machines, simple to understand, virtualization technology is on a single physical computer, through an intermediate virtual software layer Hypervisorisolation CPU, memory and other hardware resources, a virtual multiple virtual servers, to do so, a single physical server can install a plurality of applications, to maximize the utilization of resources, and a plurality of isolation between applications, as FIG. below:

Application Diagram deployed on virtual machines

Advantages of a virtual machine

  • Resources can be allocated to different virtual machines, to maximize the use of hardware resources

  • And deploy applications directly on physical machines, virtual applications more easily extended.

  • Cloud services: virtual machine virtual different physical resources, you can quickly build cloud services.

The inadequacies of virtual machines

Outside of insufficient consumption of virtual machines from physical server resources, when we create a virtual machine on a physical server, we need a set of virtual hardware and operating system running on top of a full, each virtual machine can take many server resources.

What Docker that?

Relative to the bulky virtual machine, Dockerseems even more lightweight, it will not take up too many system resources.

DockerUse nowadays is very fire Golanglanguage development of its core technology is Linuxthe core Cgroup, Namespaceand AUFSclasses Union FSand other technologies that are Linuxthe core of the technology already exists for many years, so strictly speaking not a completely innovative technology, Dockerby the underlying Linuxtechnology, Linuxthe process of encapsulation isolation, is also referred to as an isolated process vessel, completely independent of the host process.

So Dockeris an implementation of container technology, also an operating system-level virtualization, and virtual machines through a set of hardware and then install the operating system is completely different.

Relationship between the container and the system schematic docker

Docker comparison between the virtual machine

DockerIs isolated in the process of the operating system level, while the virtual machine is isolated from the physical resource level, the two are completely different. In addition, we can also compare a following, to understand the fundamental difference between the two.

Comparison of container and virtual machines [excerpt from "Docker- from entry to practice"]

From the above comparison containers and virtual machines, we understand the advantages of container technology.

Container solve the problem of development and production environments

Development and production environments refraction is the development of the contradiction between the staff and the operation and maintenance personnel, maybe we often hear developers say the operation and maintenance of this sentence: "in my computer no problem, how to you there on the problem, and certainly your problem ", and operation and maintenance personnel is considered to be a problem for developers.

Developers need to install this machine a wide variety of test environments, so the development of the project requires more software, more and more dependent on the installation environment is also more complex.

Similarly, operation and maintenance personnel need to provide a production environment for developers of the project, and in addition to operation and maintenance personnel to deal with the dependencies between software, also need to consider compatibility issues between hardware and software installation.

Like this, so we often see mutual development and operation and maintenance thrown pot, how to solve this problem?

Container is a good solution, the container can become the language of communication between the development and operation and maintenance, because the same container as a container, provides a minimal environment software running, the application will be packaged into a mirror with the needs of its environment, it you can communicate and transfer between the development and operation and maintenance.

Docker version

DockerInto Community Edition ( CE) and Enterprise Edition ( EE) two versions, the community version can be used free of charge and you need to pay to use the Enterprise Edition, for our individual developers or small businesses, generally using Community Edition.

Docker CEThere are three update channels, respectively stable, test, nightly, stableis a stable version, testis a pre-release version of the test, and nightlyis ready to develop the next version of the official release version, we can download and install according to their needs.

How to install Docker?

Well, by the previous introduction, we should Dockerhave a preliminary understanding, the following entered the Dockerlearning journey of.

While learning Dockerthe first step, from the installation Dockerbegan operating environment, our Dockercommunity version ( CE) is installed, for example,

DockerCommunity version offers Mac OS, Microsoft Windowsand Linux( Centos, Ubuntu, Fedora, Debian) and other operating systems installation package also supports installation on a cloud server, for example AWS Cloud.

Installed on a Windows system

Docker Desktop for Windows

DockerTo Windowsprovide a desktop application installation package management ( Docker Desktop for Windows), but has the following system requirements:

  1. It must be a 64bit Windows10Professional Edition, Enterprise Edition, Education Edition, built on 15063or later,
  2. In BIOSenabled virtualization. Typically, virtualization is enabled by default.
  3. At least 4GBmemory.
  4. CPUSupport SLAT.

If the operating system meets the above requirements, you can directly download the installation package installed directly after a successful installation, Dockerand does not automatically start, we need to start their own, we can begin to find the menu Docker, as shown below, click start you can start.

Docker Toolbox

If the system can not reach the above requirements, for example, you use the Windows 7operating system, in order to use this time Docker, they need help Docker Toolbox, Docker Toolboxare Dockerprovided in the older Mac OS, Windowsinstall the operating system Dockertoolset environment.

Docker ToolboxIncluding docker-cli(就是我们在终端使用的docker命令行工具), docker-compose(多容器管理工具), docker-mecahine, VirtualBox(虚拟机), Kitematic(docker的GUI管理工具).

Essentially using Docker Toolboxthe installation Dockerenvironment, actually VirtualBoxcreated in a Linuxvirtual machine, and install it on a virtual machineDocker

In addition, the installation process will open Windowsin Hyper-Vmodule ( Windowsoperating system virtualization is a technology), and there are a point to note is that if opened Hyper-V, it VirtualBoxis no longer in force.

Installed on Mac OS

As Windowsoperating systems, Dockeris Mac OSalso provided as a desktop application ( Docker Desktop for Mac), is relatively simple, the dockerdownload official line Dokcer.dmginstallation, open Docker.dmg, as shown below:

Directly drag the Dockericon will complete the installation.

For older Mac OSoperating system, it can be like Windows, like, using Docker Toolbox, this can refer to the above description.

In Mac OSAfter the installation is complete, Applicationfind the Dockericon, double click on it to launch Docker, and are as follows:

Installation on Linux

In Linuxthe operating system installation, mainly Centos7, for example, other Linuxrelease systems, such as Ubuntu, Debian, Fedoraetc., you can check yourself Dockerin an official document .

Remove the old version of docker

There may be some Linuxpre-installed Docker, but generally the older version, so you can perform the following code to remove the old version Docker.

$ sudo dnf remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine
复制代码
Specify the installation version
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
复制代码
Using yum install docker
$ sudo yum install docker-ce docker-ce-cli containerd.io
复制代码
Start docker server
# 启动docker守护进程
$ sudo systemctl start docker
复制代码

Test whether the installation is successful

Installed in several ways above DockerAfter that, we can use the following method to detect the installation was successful.

Print version docker
# 打印docker版本
$ docker version 
复制代码
Mirror operation and pulling the container
# 拉取hello-world镜像
docker pull hello-world

# 使用hello-world运行一个容器
docker run hello-world
复制代码

After running the above command, if there is an output result shown below, then the installation has succeeded.

The basic concept of Docker

Mirror ( Image), container ( Container) and warehouse ( Repository), these three are dockerthe most basic and core concepts, grasp and understanding of these three concepts, learning dockercritical.

Image (Image)

What is Dockermirroring?

DockerIt is essentially a running Linuxapplication on the operating system, but Linuxthe operating system is divided into kernel and user space, whether it is Centosor Ubuntuare, after starting the kernel, the mount Rootto provide user-space file system, and is a mirror image Docker Root file system .

DockerMirroring is a special file system to provide the desired container runtime library, resource, configuration files, etc., and also includes a number of configuration parameters to prepare for the operation (such as anonymous volume, environment variables, user, etc.).

Mirroring is a static concept, does not contain any dynamic data, its contents will not be changed after the construct.

The following are some of the basic operations of the command image, as follows:

View mirror list
# 列出所有镜像
docker image ls
复制代码

Since we have already pulled the hello-worldmirror, it will output the following:

REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
hello-world                                     latest              fce289e99eb9        7 months ago        1.84kB
复制代码

The following command will also be able to view local mirror list, but the wording more concise.

# 列表所有镜像
docker images
复制代码
Pulling the mirror from the warehouse

We have already demonstrated it using the docker pullcommand to pull the hello-worldimage, of course, use the docker image pullcommand is the same.

Usually the default is from Docker Hubthe pull mirroring, Docker Hubis Dockerthe official repository mirroring service ( Docker Registry), a large number of third-party official or a mirror for us to use, for example, we can enter the following command at the command line directly pulls a Centosmirror:

docker pull centos
复制代码

docker pullThe complete wording of command is as follows:

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
复制代码

Pulling a mirror, need to specify Docker Registrythe address and port number, the default is Docker Hubalso need to specify the repository names and tags, the repository name and the tag uniquely identify an image, and the tag is possible to omit if omitted, the default use the latest as the label name, additionally, warehouse name by author name, and software components.

So, we use the above centos, it is because the author's name is omitted, the author's name library, represents Dockerthe official mirror, so the above command is equivalent to:

docker pull library/centos:latest
复制代码

Therefore, if pulled unofficial third-party image, you need to specify the full name of the warehouse, as follows:

docker pull mysql/mysql-server:latest
复制代码
Run-time image

Use docker runcommand to create a container through the mirror, as follows:

docker run -it centos /bin/bash
复制代码
Remove Mirror

When some local mirror we do not need, then we can remove the mirror, to save storage space, but be careful if you have to use the mirror to create a container is not deleted, the image can not be deleted.

# image_name表示镜像名,image_id表示镜像id
dockere image rm image_name/image_id
复制代码

Delete shortcut command mirror:

docker rmi image_name/image_id
复制代码

Well, on Dockerknowledge of the mirror, we briefly introduced here, there is a chance, we write a separate article to talk about, especially to build Dockerknowledge mirror portion, it is necessary to learn in depth about.

Container (Container)

DockerThe image is a template for generating container, layered mirror, mirror-image relationship with the container, that is, the relationship between object-oriented programming classes and objects, we will be good for each class, and then use the class to create an object corresponding to Dockerthe use, each building is a good mirror, and then use the mirror to create a container we need.

Start and stop container

Start container, there are two ways, one is that we have already introduced the use docker runcommand to create a new container by mirroring, as follows:

docker run hello-world
复制代码

Another way to start is to start a container vessel has stopped running:

# container_id表示容器的id
docker start container_id
复制代码

To stop a running container you can use docker container stopor docker stopcommand, as follows:

# container_id表示容器的id
docker stop container_id
复制代码
View all containers

If you want to see all of the local container, you can use docker container lsthe command:

# 查看所有容器
docker container ls
复制代码

View all containers are also simple wording, as follows:

# 查看所有容器
docker ps
复制代码
Delete container

We can also use the docker container rmcommand, or simple written docker rmcommands to remove the container, but the container is not allowed to delete the running, so if you want to delete, then it must stop the vessel,

# container_id表示容器id,通过docker ps可以看到容器id
$ docker rm container_id
复制代码

When we need to delete all bulk containers, you can use the following command:

# 删除所有容器
docker rm $(docker ps -q)
复制代码
# 删除所有退出的容器
docker container prune
复制代码
Into the container
# 进入容器,container_id表示容器的id,command表示linux命令,如/bin/bash
docker exec -it container_id command
复制代码

Warehouse (Repository)

In the previous example, we used two ways to build a mirror, after the building is completed, the mirror can be run locally, generating vessel, but if more servers running the mirror it? Obviously, this time we need one that will allow us to deploy images stored centrally and services, like Github allows us to store and distribute the code as their own.

Docker HubDocker is provided for storage and distribution of official mirror Docker Registry, is also the default Registry, which is at https://hub.docker.comthe front we use the docker pullcommand begins with Docker Hubthe pull mirroring.

Docker HubThere are many high-quality image, or other official development provided for our use, of course, if you want to build our own image upload Docker Hub, we need to Docker Hubregister an account, and then send its own built in local mirror to Docker Hubwarehouse them , Docker Registrycontains many warehouses, each warehouse corresponding number of labels, different label corresponds to a different version of the software.

Docker's composition and architecture

In the installed and started Dockerafter, we can use the command line using the dockercommand operation docker, for example, we use the following command to print dockerthe version information.

docker verion
复制代码

The results are as follows:

From the above chart, we see played two parts of information: Clientand Server.

This is because Dockerwith most of the same server software (such as MySQL), are using C/Sarchitecture model, that is, calls the server through the client, but we're just the server and the client are on the same machine only.

Therefore, we can use the following graph to represent Dockerarchitecture, DOCKER_HOSTShi Docker server, and Clinet is what we use in the command dockercommand.

Docker Engine

docker serverIt provides a container for the client, the mirror, the volume data, and network management functions, in fact, these functions are made Docker Engineto achieve.

  1. dockerd: The server daemon.
  2. Client docker Cli: Command Line Interface
  3. REST API: In addition to cli command line interface, or by REST APIcallingdocker

The following is Docker Enginean example of diagram:

summary

As a developer, learning or development process, always need to install a wide variety of development environments. In addition, a technical team in the process of development projects, often requires a unified development environment, which may avoid some of the inconsistencies caused by the environment problem.

Although the use of virtual machines can solve the above problem, but the virtual machine is too heavy, too much consumption of host resources, and technology as lightweight containers, Dockercan be simply and easily solve these problems, so that the deployment of installation and application development environment becomes very simple, and use Dockerthan the virtual machine to install the operating system, is much simpler.


Welcome scan code concerned, common learning progress

Guess you like

Origin juejin.im/post/5d4522c1f265da03e05af5f5