Docker
Container technology is popular nowadays, I believe that as a developer, you must have heard or used, many people will Docker
be understood as a lightweight virtual machine, but in fact Docker
with 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 Docker
it?
With a little bit of doubt in my heart, let us learn Docker
it.
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:
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
CPU
and 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 Hypervisor
isolation 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:
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, Docker
seems even more lightweight, it will not take up too many system resources.
Docker
Use nowadays is very fire Golang
language development of its core technology is Linux
the core Cgroup
, Namespace
and AUFS
classes Union FS
and other technologies that are Linux
the core of the technology already exists for many years, so strictly speaking not a completely innovative technology, Docker
by the underlying Linux
technology, Linux
the process of encapsulation isolation, is also referred to as an isolated process vessel, completely independent of the host process.
So Docker
is 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.
Docker comparison between the virtual machine
Docker
Is 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.
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
Docker
Into 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 CE
There are three update channels, respectively stable
, test
, nightly
, stable
is a stable version, test
is a pre-release version of the test, and nightly
is 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 Docker
have a preliminary understanding, the following entered the Docker
learning journey of.
While learning Docker
the first step, from the installation Docker
began operating environment, our Docker
community version ( CE
) is installed, for example,
Docker
Community version offers Mac OS
, Microsoft Windows
and 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
Docker
To Windows
provide a desktop application installation package management ( Docker Desktop for Windows
), but has the following system requirements:
- It must be a
64
bitWindows10
Professional Edition, Enterprise Edition, Education Edition, built on15063
or later, - In
BIOS
enabled virtualization. Typically, virtualization is enabled by default. - At least
4GB
memory. CPU
SupportSLAT
.
If the operating system meets the above requirements, you can directly download the installation package installed directly after a successful installation, Docker
and 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 7
operating system, in order to use this time Docker
, they need help Docker Toolbox
, Docker Toolbox
are Docker
provided in the older Mac OS
, Windows
install the operating system Docker
toolset environment.
Docker Toolbox
Including docker-cli(就是我们在终端使用的docker命令行工具)
, docker-compose(多容器管理工具)
, docker-mecahine
, VirtualBox(虚拟机)
, Kitematic(docker的GUI管理工具)
.
Essentially using Docker Toolbox
the installation Docker
environment, actually VirtualBox
created in a Linux
virtual machine, and install it on a virtual machineDocker
In addition, the installation process will open Windows
in Hyper-V
module ( Windows
operating system virtualization is a technology), and there are a point to note is that if opened Hyper-V
, it VirtualBox
is no longer in force.
Installed on Mac OS
As Windows
operating systems, Docker
is Mac OS
also provided as a desktop application ( Docker Desktop for Mac
), is relatively simple, the docker
download official line Dokcer.dmg
installation, open Docker.dmg
, as shown below:
Directly drag the Docker
icon will complete the installation.
For older Mac OS
operating system, it can be like Windows
, like, using Docker Toolbox
, this can refer to the above description.
In Mac OS
After the installation is complete, Application
find the Docker
icon, double click on it to launch Docker
, and are as follows:
Installation on Linux
In Linux
the operating system installation, mainly Centos7
, for example, other Linux
release systems, such as Ubuntu
, Debian
, Fedora
etc., you can check yourself Docker
in an official document .
Remove the old version of docker
There may be some Linux
pre-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 Docker
After 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 docker
the most basic and core concepts, grasp and understanding of these three concepts, learning docker
critical.
Image (Image)
What is Docker
mirroring?
Docker
It is essentially a running Linux
application on the operating system, but Linux
the operating system is divided into kernel and user space, whether it is Centos
or Ubuntu
are, after starting the kernel, the mount Root
to provide user-space file system, and is a mirror image Docker Root file system .
Docker
Mirroring 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-world
mirror, 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 pull
command to pull the hello-world
image, of course, use the docker image pull
command is the same.
Usually the default is from Docker Hub
the pull mirroring, Docker Hub
is Docker
the 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 Centos
mirror:
docker pull centos
复制代码
docker pull
The complete wording of command is as follows:
docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
复制代码
Pulling a mirror, need to specify Docker Registry
the address and port number, the default is Docker Hub
also 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 Docker
the 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 run
command 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 Docker
knowledge of the mirror, we briefly introduced here, there is a chance, we write a separate article to talk about, especially to build Docker
knowledge mirror portion, it is necessary to learn in depth about.
Container (Container)
Docker
The 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 Docker
the 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 run
command 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 stop
or docker stop
command, 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 ls
the command:
# 查看所有容器
docker container ls
复制代码
View all containers are also simple wording, as follows:
# 查看所有容器
docker ps
复制代码
Delete container
We can also use the docker container rm
command, or simple written docker rm
commands 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 Hub
Docker is provided for storage and distribution of official mirror Docker Registry
, is also the default Registry
, which is at https://hub.docker.com
the front we use the docker pull
command begins with Docker Hub
the pull mirroring.
Docker Hub
There 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 Hub
register an account, and then send its own built in local mirror to Docker Hub
warehouse them , Docker Registry
contains 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 Docker
after, we can use the command line using the docker
command operation docker, for example, we use the following command to print docker
the version information.
docker verion
复制代码
The results are as follows:
From the above chart, we see played two parts of information: Client
and Server
.
This is because Docker
with most of the same server software (such as MySQL
), are using C/S
architecture 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 Docker
architecture, DOCKER_HOST
Shi Docker server
, and Clinet is what we use in the command docker
command.
Docker Engine
docker server
It provides a container for the client, the mirror, the volume data, and network management functions, in fact, these functions are made Docker Engine
to achieve.
dockerd
: The server daemon.Client docker Cli
: Command Line InterfaceREST API
: In addition to cli command line interface, or byREST API
callingdocker
The following is Docker Engine
an 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, Docker
can be simply and easily solve these problems, so that the deployment of installation and application development environment becomes very simple, and use Docker
than the virtual machine to install the operating system, is much simpler.
Welcome scan code concerned, common learning progress