Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a Docker container

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what is a Docker container". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is a Docker container"?

The Origin of Container Technology

Suppose your company is secretly developing the next "Jinri Toutiao" APP. Let's call it tomorrow's Toutiao. Programmers themselves set up an environment to write code from beginning to end. After writing the code, the programmer will give the code to the tester for testing. At this time, the tester begins to build this environment from beginning to end. Programmers who have problems in the testing process do not have to worry. They can look innocent and act coquettish. Obviously, it can be run in other people's environment.

After the test students can finally go online, when the operation and maintenance students have to re-build this environment from beginning to end, it takes great efforts to build a good environment and begin to go online. Too bad, the online system collapses. At this time, programmers with good psychological quality can perform their acting skills again, "obviously it can be run in other people's environment."

From the whole process, we can see that not only have we repeatedly built three environments, but also forced programmers to change careers and actors to waste acting talent, a typical waste of time and efficiency, smart programmers will never be satisfied with the status quo. so it's time for programmers to change the world, and container technology arises at the historic moment.

Some students may say: "wait, don't change the world, we have a virtual machine, VMware is easy to fly, first build a virtual machine environment and then give the test and operation and maintenance clone out of it?"

Before there was no container technology, this was a good idea, but it wasn't that good.

Let's start with the popularization of science. Now the underlying cornerstone of cloud computing is virtual machine technology. Cloud computing manufacturers can buy a bunch of hardware to build a data center and then use virtual machine technology to split hardware resources. For example, 100 virtual machines can be cut out, which can be sold to a lot of users.

You may wonder why this is not a good idea.

Container technology vs virtual machine

We know that compared with a simple application, the operating system is a heavy and clunky program, referred to as bulky, how bulky is it?

We know that the operating system needs to take up a lot of resources to run, and we must have a deep understanding of this. The newly installed system has not deployed anything, and the simple operating system starts with at least dozens of gigabytes of disk and a few gigabytes of memory.

Suppose I have a machine with 16 gigabytes of memory and need to deploy three applications, then using virtual machine technology can be divided into:

Open three virtual machines on this machine, and deploy an application on each virtual machine, of which VM1 takes up 2G of memory, VM2 takes up 1G of memory, and VM3 takes up 4G of memory.

We can see that virtual itself occupies a total of 7 gigabytes of memory, so there is no way to divide more virtual machines to deploy more applications, but we deploy applications and use applications rather than operating systems.

Wouldn't it be nice to have a technology that allows us to avoid wasting memory on "useless" operating systems? This is problem one, the main reason is that the operating system is too heavy.

There is another problem, and that is the startup time. We know that the operating system restart is very slow, because the operating system has to load everything that should be loaded from beginning to end, and the process is very slow, often for a few minutes, so the operating system is still too stupid.

So is there a technology that allows us to get the benefits of virtual machines and overcome these shortcomings so as to achieve both?

The answer is yes, this is container technology.

What is a container?

The word container is container in English. In fact, container also means container. Container is definitely a great invention in commercial history, which greatly reduces the transportation cost of marine trade. Let's take a look at the benefits of containers:

Containers are isolated from each other

Long-term repeated use

Quick load and unmount

The specification standard can be placed in the port and on board.

Back to the container in the software, in fact, the container and the container are very similar in concept.

One of the major purposes of modern software development is isolation. Applications run independently and do not interfere with each other, which is not easy to achieve. One of the solutions is the virtual machine technology mentioned above. Isolation is achieved by deploying applications in different virtual machines.

But virtual machine technology has all the disadvantages mentioned above, so what about container technology?

Different from the isolation of virtual machines through the operating system, container technology only isolates the runtime environment of the application, but the containers can share the same operating system, which refers to the various libraries and configurations on which the program runs.

From the figure, we can see that containers are more lightweight and take up less resources. compared with the memory consumption of several gigabytes of memory used by the operating system, container technology only needs M space, so we can deploy a large number of containers on the same specification of hardware. this is incomparable to the virtual machine, and unlike the operating system's startup time of a few minutes, the container starts almost instantly. Container technology provides a more efficient way to package service stacks, So cool.

So how do we use the container? It's time to talk about docker.

Note that containers are a general-purpose technology, and docker is just one of them.

What is docker?

Docker is an open source project implemented in GE language, which makes it easy for us to create and use containers. Docker packages the program and all its dependencies into docker container, so that your program can behave consistently in any environment, where the container is like a container, and the container is in an operating system environment like a cargo ship or port. The performance of the program is only related to the container (container), not to which freighter or port (operating system) the container is placed.

So we can see that docker can shield environmental differences, that is, as long as your program is packaged into docker, the behavior of the program is consistent no matter what environment you run in, programmers will no longer be able to perform, and there will be no more "running in my environment" and the real implementation of "build once, run everywhere".

In addition, another advantage of docker is rapid deployment, which is one of the most common application scenarios for Internet companies, one reason is that the container starts very fast, and another reason is that as long as you make sure that the programs in a container run correctly, you can be sure that no matter how much you deploy in a production environment.

How to use docker

There are several concepts in docker:

Dockerfile

Image

Container

In fact, you can simply think of image as an executable program, and container is a running process.

Then writing a program requires source code, so "writing" image requires that dockerfile,dockerfile is the source code of image, and docker is the "compiler".

So we just need to specify which programs are needed in dockerfile, what configuration to rely on, and then give the dockerfile to the "compiler" docker to "compile", that is, the docker build command, the generated executable program is image, and then we can run the image, which is the docker run command, and the image runs after it is docker container.

The specific usage will not be repeated here, you can refer to the official documentation of docker, where there is a detailed explanation.

How does docker work

In fact, docker uses the common CS architecture, that is, client-server mode. Docker client is responsible for handling various commands entered by users, such as docker build and docker run. What really works is server, that is, docker demon. It is worth noting that docker client and docker demon can run on the same machine.

Next, let's use a few commands to explain the workflow of docker:

1,docker build

When we use this command when we finish writing dockerfile and give it to docker to "compile", then client forwards it to docker daemon after receiving the request, and docker daemon then creates an "executable program" image based on dockerfile.

2,docker run

With the "executable program" image, you can run the program. Then use the command docker run,docker daemon to find the specific image after receiving the command, and then load it into memory to start execution. Image execution is the so-called container.

3,docker pull

In fact, docker build and docker run are the two core commands, can use these two commands basically docker can be used, the rest is some additions.

So what does docker pull mean?

As we said before, the concept of image in docker is similar to an "executable program". Where can we download an application written by someone else? Quite simply, that's APP Store, the app store. Similarly, since image is also an "executable program", is there a "Docker Image Store"? The answer is yes, this is Docker Hub,docker 's official "app store", where you can download image written by others, so you don't have to write your own dockerfile.

Docker registry can be used to store a variety of image, and the public repository where anyone can download image is docker Hub. So how to download image from Docker Hub is the docker pull command here.

Therefore, the implementation of this command is also very simple, that is, the user sends a command through docker client, and docker daemon receives the command and sends an image download request to docker registry, which is downloaded and stored locally, so that we can use image.

Finally, let's take a look at the underlying implementation of docker.

The underlying implementation of docker

Docker is based on the Linux kernel to provide several functions:

NameSpace

We know that PID, IPC, network and other resources in Linux are global, and the NameSpace mechanism is a resource isolation scheme. Under this mechanism, these resources are no longer global, but belong to a specific NameSpace, and the resources under each NameSpace do not interfere with each other, which makes each NameSpace look like an independent operating system, but only NameSpace is not enough.

Control groups

Although you can achieve resource isolation with NameSpace technology, processes can still access system resources without control, such as CPU, memory, disk, network, etc. In order to control the access of processes to resources in the container, Docker uses control groups technology (that is, cgroup). With cgroup, you can control the consumption of system resources by processes in the container, for example, you can limit the upper limit of memory used by a container, which CPU can be run on, and so on.

With these two technologies, the container really looks like a separate operating system.

At this point, I believe you have a deeper understanding of "what is a Docker container", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report