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

How to quickly master the necessary basic knowledge of Docker

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to quickly master the necessary basic knowledge of Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A primitive age without virtualization technology

Let's think about it, in the "ancient" days when there was no computing virtualization technology, what were the general steps if we were to deploy an application (Application)?

The first step must be to prepare a physical server, then install an operating system (Operating System) on the physical server, and then install and run our applications on the operating system. This process can be shown in the following figure:

Schematic diagram of physical server deployment application

So what's wrong with this approach? In fact, deploying applications on physical machines has the following disadvantages:

Deployment is very slow: because we have to prepare the hardware server, then install the operating system, and then deploy the application, and the application still depends on a lot of software, so the process is slow.

The cost is very high: the main reason is that the cost of the physical device is so high that even a simple application requires a server.

Waste of resources: if the application is too simple, it is easy to waste hardware resources, such as CPU and memory

Migration and expansion are too slow: if you need to migrate or expand applications, you have to prepare other physical servers, which is cumbersome and slow.

So is there any way to solve these problems? The answer is virtualization technology.

The era of using virtual machines to deploy applications

What is virtualization technology?

When it comes to computer virtualization technology, what we think of directly is the virtual machine, which allows us to simulate multiple machines on one physical computer. To put it simply, virtualization technology is on a physical computer, using the intermediate virtual software layer Hypervisor to isolate hardware resources such as CPU and memory, and virtualize multiple virtual servers. In this way, multiple applications can be installed on one physical server. Maximize resource utilization, and multiple applications are isolated from each other, as shown in the following figure:

Schematic diagram of deploying applications on virtual machines

Benefits of virtual machines

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

Instead of deploying applications directly on physical machines, virtual machines are easier to expand applications.

Cloud services: cloud services can be built quickly by virtualizing different physical resources through virtual machines.

Inadequacies of virtual machines

The deficiency of virtual machine lies in the consumption of physical server resources. when we create a virtual machine on a physical server, we need to virtualize a set of hardware and run a complete operating system on it. each virtual machine takes up a lot of server resources.

What is Docker?

Compared with the bulky virtual machine, Docker is more lightweight, so it will not take up too much system resources.

Docker is developed using the popular Golang language nowadays, and its technical core is Cgroup,Namespace of the Linux kernel and Union FS of the AUFS class. These technologies have existed in the Linux kernel for many years, so strictly speaking, Docker is not a completely innovative technology. Docker encapsulates and isolates the Linux process through these underlying Linux technologies, and the isolated process is also called a container, which is completely independent of the host process.

So Docker is not only an implementation of container technology, but also a kind of virtualization at the operating system level, which is completely different from the virtual machine installing the operating system through a set of hardware.

Schematic diagram of the relationship between docker container and system

Comparison between Docker and virtual machine

Docker is isolated at the operating system process level, while virtual machines are isolated at the physical resource level, the two are completely different. In addition, we can also understand the fundamental difference between the two through a comparison below.

Comparison of containers and virtual machines [excerpt from Docker- from getting started to practice]

From the comparison between the container and the virtual machine above, we understand the advantages of container technology.

Container solves the problem of development and production environment.

The development environment and the production environment reflect the contradiction between the developers and the operation and maintenance personnel. perhaps we often hear the developers say to the operators: "there is nothing wrong with the operation of my computer." it must be your problem, while the operation and maintenance staff think it is the developer's problem.

Developers need to install a variety of test environments locally, so the more software and dependencies a project needs, the more complex the installation environment will be.

Similarly, operators need to provide a production environment for projects developed by developers, and operators not only deal with the dependence between software, but also need to consider the compatibility between installation software and hardware.

That's it, so we often see developers and operators throwing pots at each other, how to solve this problem?

The container is a good solution, and the container can become the language of communication between the developer and the operation and maintenance staff, because the container, like a container, provides a minimum environment for the software to run, and packages the application with the environment it needs to become a mirror image, so that it can communicate and transmit between the development and operation and maintenance.

The version of Docker

Docker is divided into two versions: community version (CE) and enterprise version (EE). The community version can be used for free, while the enterprise version needs to be used for a fee. For us individual developers or small businesses, we generally use the community version.

Docker CE has three update channels. Stable, test and nightly,stable are stable versions, test is the pre-release version after testing, and nightly is the version that is ready to be officially released in the next version. We can download and install it according to our own needs.

How do I install Docker?

Well, through the previous introduction, we should have a preliminary understanding of Docker, and let's start the learning journey of Docker.

For the first step in learning Docker, let's start with installing the Docker runtime environment and take the Community version (CE) installation of Docker as an example.

The Docker community version provides installation packages for operating systems such as Mac OS,Microsoft Windows and Linux (Centos,Ubuntu,Fedora,Debian). It also supports installation on cloud servers, such as AWS Cloud.

Install on Windows system

Docker Desktop for Windows

Docker provides a desktop application-managed installation package (Docker Desktop for Windows) for Windows, but has the following requirements for the system:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Must be 64-bit Windows10 Professional, Enterprise, Educational, built on version 15063 or later

Enable virtualization in BIOS. Typically, virtualization is enabled by default.

At least there is 4GB memory.

CPU supports SLAT.

If the operating system meets the above requirements, you can download the installation package directly and install it directly. After the installation is successful, Docker will not start automatically, but we need to start it ourselves. We can find Docker in the start menu, such as the figure below. Click start to start.

Docker Toolbox

If the system does not meet the above requirements, for example, if you are using the Windows 7 operating system, if you want to use Docker, you need to use the toolset provided by Docker to install the Docker environment on the older Mac OS,Windows operating system.

Docker Toolbox includes docker-cli (the docker command-line tool we use on the terminal), docker-compose (multi-container management tool), docker-mecahine,VirtualBox (virtual machine), and Kitematic (docker's GUI management tool).

Essentially using Docker Toolbox to install the Docker environment is actually creating a Linux virtual machine in VirtualBox and installing Docker on the virtual machine.

In addition, the Hyper-V module of Windows (a technology for virtualization of the Windows operating system) is opened during installation, and it is important to note that if Hyper-V is enabled, VirtualBox is no longer in effect.

Install on Mac OS

Like the Windows operating system, Docker also provides a desktop application (Docker Desktop for Mac) for Mac OS, which is relatively simple. Download the Dokcer.dmg installation from the docker official website and open Docker.dmg, as shown in the following figure:

Drag the Docker icon directly to complete the installation.

For older Mac OS operating systems, you can also use Docker Toolbox, like Windows, as described above.

After the installation on Mac OS is complete, find the Docker icon in Application, and double-click to open it to launch Docker, as follows:

Install on Linux

The installation on the Linux operating system mainly takes Centos7 as an example. Other distributions of Linux systems, such as Ubuntu,Debian,Fedora, can query the official documents of Docker.

Delete the old docker version

Some Linux may be pre-installed with Docker, but the general version is older, so you can first execute the following code to remove the old version of 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 copy the code

Specify the installation version

$sudo yum-config-manager\-- add-repo\ https://download.docker.com/linux/centos/docker-ce.repo copy code

Install docker using yum

$sudo yum install docker-ce docker-ce-cli containerd.io copy code

Start the docker server

# start the docker daemon $sudo systemctl start docker copy code

Test whether the installation is successful

After installing Docker in the above ways, we can check whether the installation is successful by using the following methods.

Print docker version

# print docker version $docker version copy code

Pull the image and run the container

# pull hello-world image docker pull hello-world # run a container docker run hello-world copy code using hello-world

After running the above command, if you have the output shown in the following figure, the installation has been successful.

The basic concept of Docker

Mirror (Image), container (Container) and warehouse (Repository) are the most basic and core concepts in docker. The mastery and understanding of these three concepts is the key to learning docker.

Mirror (Image)

What is a mirror image of Docker?

Docker is essentially an application running on the Linux operating system, while the Linux operating system is divided into kernel and user space, whether Centos or Ubuntu, provide user space by mounting the Root file system after starting the kernel, and the Docker image is a Root file system.

Docker image is a special file system that provides programs, libraries, resources, configuration and other files needed for the container to run, as well as some configuration parameters (such as anonymous volumes, environment variables, users, etc.) prepared for the runtime.

Mirroring is a static concept that does not contain any dynamic data and its content will not be changed after it is built.

The following commands are some basic operations for mirroring, as follows:

View the list of images

# list all image docker image ls replication codes

Since we have pulled the hello-world image earlier, we will output the following:

REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 7 months ago 1.84kB copy Code

The following command can also view the local image list, and write it more succinctly.

# list all image docker images copy codes

Pull the image from the warehouse

We have demonstrated earlier that we used the docker pull command to pull the hello-world image, and of course the same is true with the docker image pull command.

Generally speaking, images are pulled from Docker Hub by default. Docker Hub is an official image repository service (Docker Registry) provided by Docker. There are a large number of official or third-party images for us to use. For example, we can directly pull a Centos image by entering the following command on the command line:

Docker pull centos copy code

The complete docker pull command is written as follows:

Docker pull [option] [Docker Registry address [: Port number] /] Warehouse name [: label] copy code

To pull an image, you need to specify the address and port number of the Docker Registry. The default is Docker Hub, and you also need to specify the warehouse name and tag. The warehouse name and tag uniquely determine an image, while the tag may be omitted. If omitted, latest is used as the signature by default. In addition, the repository name consists of the author name and the software name.

So, we use centos above, because the author name is omitted, and the author name is library, which represents the official image of Docker, so the above command is equivalent to:

Docker pull library/centos:latest copy code

Therefore, if you pull an unofficial third-party image, you need to specify the full repository name, as follows:

Docker pull mysql/mysql-server:latest copy code

Run Mirror

Using the docker run command, you can create a container by mirroring, as follows:

Docker run-it centos / bin/bash copy code

Delete Mirror

When we do not need some local images, we can also delete the image to save storage space. However, it is important to note that if a container created with the image is not deleted, the image is not allowed to be deleted.

# image_name indicates the name of the image, and image_id indicates the copy code of the image id dockere image rm image_name/image_id

Shortcut command to delete a mirror:

Docker rmi image_name/image_id copy code

All right, we will briefly introduce the relevant knowledge of Docker image here. If we have the opportunity, we will write a separate article to talk about, especially the relevant knowledge of building Docker image. It is necessary to study it in depth.

Container (Container)

The image of Docker is used to generate the template of the container. The image is hierarchical, and the relationship between the image and the container is the relationship between the class and the object in object-oriented programming. We define each class, and then use the class to create the object. Corresponding to the use of Docker, we build each image, and then use the image to create the container we need.

Start and stop the container

There are two ways to launch a container. One is to create a brand new container by mirroring it using the docker run command as described earlier, as follows:

Docker run hello-world copy code

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

# container_id indicates the id docker start container_id copy code of the container

To stop a running container, use the docker container stop or docker stop command, as follows:

# container_id indicates the id docker stop container_id copy code of the container

View all containers

If you want to view all local containers, you can use the docker container ls command:

# View all container docker container ls replication codes

Check that all containers are also written succinctly, as follows:

# View all container docker ps replication codes

Delete Container

We can also use the docker container rm command, or the concise docker rm command to delete the container, but we are not allowed to delete the running container, so if we want to delete it, we must first stop the container.

# container_id represents the container id. You can see the container id $docker rm container_id copy code through docker ps

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

# Delete all containers

Docker rm $(docker ps-Q)

Copy the code

# Delete all exited container docker container prune replication codes

Enter the container

# enter the container, and container_id indicates that the id,command of the container represents the linux command, such as / bin/bash docker exec-it container_id command to copy the code

Warehouse (Repository)

In the previous example, we built the image in two ways. After the build is complete, we can run the image locally to generate a container, but what if we run the image on more servers? Obviously, at this point we need a service that allows us to store and distribute images centrally, just as Github allows us to store and distribute our own code.

Docker Hub is the official Docker Registry provided by Docker to store and distribute images, and it is also the default Registry. Its URL is https://hub.docker.com. We pulled the image from Docker Hub using the docker pull command earlier.

Docker Hub has many high-quality images provided by official or other developers for us to use. Of course, if we want to upload our self-built images to Docker Hub, we need to register an account on Docker Hub, and then send our locally built images to Docker Hub's warehouse. Docker Registry contains many repositories, each warehouse corresponds to multiple tags, and different tags correspond to different versions of the software.

The composition and structure of Docker

After installing and starting Docker, we can use the docker command on the command line to manipulate docker, such as printing the version information of docker using the following command.

Docker verion copy code

The results are as follows:

From the figure above, we can see that two parts of the message are typed out: Client and Server.

This is because Docker, like most server-side software (such as MySQL), uses the Cmax S architecture model, that is, to call the server through the client, except that we now happen to have both the server and the client on the same machine.

Therefore, we can use the following figure to represent the architecture of Docker, DOCKER_HOST is Docker server, and Clinet is that we use the docker command in the command.

Docker Engine

Docker server provides the client with container, mirror, data volume, network management and other functions, in fact, these functions are realized by Docker Engine.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Dockerd: server daemon.

Client docker Cli: command line interface

REST API: in addition to the cli command line interface, you can also call docker through REST API

The following is an example diagram of Docker Engine:

This is the end of the content of "how to quickly grasp the necessary basic knowledge of Docker". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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