What Docker in the end is that to solve any problem, benefits and where?

One of the biggest software development trouble, is the environment configuration. User computer environment is different, how do you know own software, you can run up in those machines? The user must ensure two things: install the operating system settings, various libraries and components. Only they have the right software to run. For example, to install a  Python  application, your computer must have Python engine, must also have various dependencies, may have to configure the environment variables. If some old modules are not compatible with the current environment, it is in trouble. Developers often say: "It's in my machine can run" (It works on my machine) , by inference, other machines is likely to run. Environment configuration so troublesome, replace the machine, we must do it all over, be time consuming. A lot of people think, can not solve the problem fundamentally, the software can be installed with the environment? In other words, when installed, the original environment exactly copied over.

Second, the virtual machine

VM (virtual machine) is installed in the environment with a solution. It can run another operating system on which operating systems, such as systems running Windows inside  Linux  system. There is no application to this perception, because the virtual machine looks exactly the same with the real system, and for the underlying system, the virtual machine is a regular file, you do not need the deleted, there is no impact on other parts. Although the user can restore the original software virtual machine environment. However, this solution has several drawbacks.

Resource consumption and more

Virtual machine exclusive part of the memory and hard disk space. When it is running, other programs will not be able to use those resources. Even inside the virtual machine applications, the use of real memory is only 1MB, virtual machines still need several hundred MB of memory to run.

Redundant multi-step

A virtual machine is a complete operating system, some system-level steps, often can not be skipped, such as user login.

Slow start

Start the operating system how long, how long you need to start the virtual machine. May have to wait a few minutes, the application can really run.

Three, Linux containers

Because of these disadvantages virtual machines, Linux developed another virtualization technology: Linux container (Linux Containe rs , 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.

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.

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.

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.

Fourth, what Docker that?

Docker belongs Linux package container, the container provides an easy to use interfaces . It is the most popular Linux container solutions. 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.

Five, Docker use

Docker's main purpose, there are three categories.

One-off environment. For example, local test other people's software, continuous integration when providing unit test and build environment.

Provide flexible cloud services. Because Docker can open with the container closed, it is suitable for dynamic expansion and volume reduction.

The formation of micro-services architecture. By a plurality of containers, the machine can run a plurality of services, the machine can be simulated in the micro-service architecture.

Six, Docker installation

Docker is an open source commercial product, available in two versions: Community Edition (Community Community Edi Ti ON, abbreviated as CE) and Enterprise Edition (En TE rprise Edition, abbreviated as EE). Enterprise Edition includes a number of fee-based services, individual developers generally use less. The following are introduced for Community Edition. Docker CE installation please refer to the official documentation.

Mac:https://docs.docker.com/docker-for-mac/install/

Windows:https://docs.docker.com/docker-for-windows/install/

Ubuntu:https://docs.docker.com/install/linux/docker-ce/ubuntu/

Debian:https://docs.docker.com/install/linux/docker-ce/debian/

CentOS:https://docs.docker.com/install/linux/docker-ce/centos/

Fedora:https://docs.docker.com/install/linux/docker-ce/fedora/

Other Linux distributions: https: //docs.docker.com/install/linux/docker-ce/binaries/

After the installation is complete, run the following command to verify that the installation was successful.

$ Docker version # or $ docker infoDocker requires a user with sudo privileges, in order to avoid sudo each command are entered, the user can join Docker User Group (official document [1]). $ Sudo usermod -aG docker $ USERDocker is a server - client architecture. Docker time command line to run the command, the machine needs to have Docker service. If the service does not start, you can use the following command to start (official document [2]). # Service usage command sudo Service Docker Start # $ SYST EMC usage $ sudo systemctl start docker tl command

Seven, image files

Docker to the application and its dependencies, packaged in the image file inside. Only through this document, in order to generate Docker containers. image files can be seen as a template container. Examples vessel image file generated according Docker. The same image file, the container may generate a plurality of instances running simultaneously. image is a binary file. The actual development, a image file, often through inheritance another image file, plus some personalized settings generated. For example, you can image on the basis of Ubuntu, entered, and joined the Apache server, form your image.

# List all image files of the machine. $ Docker image ls # delete image files $ docker image rm [imageName] image file is generic, a machine image files are copied to another machine, it can still be used. In general, in order to save time, we should try to use someone else make a good image file, rather than your own. Even if you want to customize, it should also be based on other people's image processing file, rather than starting from scratch production. In order to facilitate sharing, the image file is created, it can be uploaded to the online repository. Docker's official repository Docker Hub is the most important, the most common image repository. In addition, the sale of produce their own image file is also possible.

Eight examples: hello world

Below, we adopted the most simple image file "hello world [3]", feel Docker. First, run the following command to grab the image file from the warehouse to the local.

$ Docker image pull library / hello-world code above, docker image pull crawl image file commands. library / hello-world is the location of the image file inside the warehouse, where the library is a set of image files are located, hello-world is the name of the image file. Since the image file Docker official offer, are placed in library [4] set inside, so it is the default group, can be omitted. Thus, the above command can be written as follows. After $ docker image pull hello-world success crawl, you can see the image files in the machine. $ Docker image ls now, run the image file. $ Docker container run hello-worlddocker container run command from the image file to generate a container instance running. Note, docker container run command has automatically grab image file function. If you do not specify a local image file found, it will automatically grab from the warehouse. Accordingly, the foregoing docker image pull command is not a required step. If it is successful, you will read the following output on the screen. $ Docker container run hello-worldHello from Docker! This message shows that your installation appears to be working correctly .... ... After the output of this prompt, hello world will stop running, the container is automatically terminated. Some containers are not automatically terminated because the service is provided. For example, running the Ubuntu installation image, you can experience the Ubuntu system on the command line. $ Docker container run -it ubuntu bash for those vessels not automatically terminated, you must use the docker container kill command manually terminated. $ Docker container kill [containID]

Nine, container file

Examples of the image file generating container, is also a document, called the container file. In other words, once the container is generated, it will at the same time there are two files: image files and container file. Close the container and the container does not delete files, but the container to stop running it.

# Listed in the machine running the container $ docker container ls # lists all containers of the present machine, comprising terminates into output containers $ docker container ls --all above command includes an ID of the container. Many places are required to provide this ID, such as the termination of a docker container kill command vessel operation. Terminate the operation of container files, will still occupy disk space, you can use docker container rm command to delete. After $ docker container rm [containerID] run the above command, then use the docker container ls --all command, you will find deleted files container has disappeared.

Ten, Docke the RF Ile files

After learning to use image file, the next question is, how can generate image files? If you want to promote your own software, it is bound to produce their own image files. That's where Dockerfile file. It is a text file used to configure image. Docker binary image file generated according to the document. Below through an example that demonstrates how to write Dockerfile file.

XI Example: make your own Docker container

Here I koa-demos [5] project as an example, how to write Dockerfile file, which allows users to run Koa framework Docker container inside. As preparation, please download the source code [6].

$ git clone https://github.com/ruanyf/koa-demos.git$ cd koa-demos

11.1 write Dockerfile File First, in the root directory of the project, create a new text file .dockerignore, write the following content [7].

.gitnode_modulesnpm-debug.log above code indicates that the three paths to exclude, not packed into the image file. If you do not want to exclude the path, the file may not be new. Then, in the root directory of the project, a new text file Dockerfile, write the following contents [8]. FROM node:. 8.4COPY / appWORKDIR / appRUN npm install --registry = https: //registry.npm.taobao.orgEXPOSE 3000 a total of five lines of code above, the following meanings.

FROM node: 8.4: The image file inherits official node image, colon represents the label, where the label is 8.4, namely 8.4 version node.

. COPY / app: all the files in the current directory (except .dockerignore exclude the path), are copied into the image file / app directory.

WORKDIR / app: Specifies the path for the next job / app.

RUN npm install: In / app directory, run npm install command to install the dependencies. Note that after installing all the dependencies, will be packaged into the image file.

EXPOSE 3000: 3000 the vessel port is exposed, allowing the external connection port.

After 11.2 create image files have Dockerfile file, you can create image files using docker image build commands.

$ Docker image build -t koa-demo # or $ docker image build -t koa-demo:.. 0.0.1 above code, -t parameter to specify the name of the image file, followed by a colon can also specify a label. If not specified, the default label is the latest. The final point that represents the path Dockerfile file is located, the example is the current path, it is a point. If it is successful, you can see the image files koa-demo of the new generation. $ Docker image ls

11.3 generate container docker container run command generates a container from image files.

$ Docker container run -p 8000: 3000 -it koa-demo / bin / bash # or $ docker container run -p 8000: 3000 -it koa-demo: Meaning of each parameter 0.0.1 / bin / bash above command is as follows:

-p parameters: port mapping 3000 to 8000 of the container port of the machine.

-it parameters: Shell vessel mapped to the current Shell, then you are in the command window, enter the machine, it will pass the container.

koa-demo: 0.0.1: name of the image file (if any label, also need to provide the label, the default is the latest label).

/ Bin / bash: start after the container, the first internal command execution. Here is the start Bash, to ensure that users can use the Shell.

If all goes well, run the above command, we'll return to a command prompt.

root @ 66d80f4aaf1e: / app # This means that you have inside the container, and the prompt return of the vessel is the internal Shell prompt. Execute the following command. root @ 66d80f4aaf1e: / app # node demos / 01.js At this time, Koa framework has been up and running. Open the browser of the machine, access http://127.0.0.1:8000, the page displays "Not Found", because this demo [9] did not write the route. In this example, the Node process running in a virtual environment inside Docker container, exposed to the process and the file system are virtual network interface, with the native file system and the network interfaces are isolated, it is necessary to define the physical machine container port mapping (map). Now, in the container of the command line and press Ctrl + c to stop Node process, then press Ctrl + d (or type exit) to exit the vessel. Furthermore, the container may be run terminated with docker container kill. # In another terminal window of the machine, to identify the container ID $ docker container ls # Stops the specified container runs $ docker container kill [containerID] After the container is stopped, and will not disappear, delete the container file with the following command. # Identify the container ID $ docker container ls --all # delete the specified container file $ docker container rm [containerID] can also be used --rm parameters docker container run commands automatically delete files after a container vessel terminates. $ Docker container run --rm -p 8000: 3000 -it koa-demo / bin / bash

11.4 CMD commands on a case which, after the container starts, enter commands node demos / 01.js manually. We can write this command in Dockerfile inside, so that after the container starts, the command has been executed, no longer need to manually enter.

FROM node: 8.4COPY npm install --registry = https / appWORKDIR / appRUN:. //Registry.npm.taobao.orgEXPOSE 3000CMD node demos / 01.js Dockerfile above which, more than the last line CMD node demos / 01.js , which represents the container is performed automatically after starting node demos / 01.js. You may ask, What is the difference with the RUN command CMD command in? Simply put, RUN command is executed during the construction phase image file, the results will be packaged into the image file; CMD command is executed after the container starts. In addition, a Dockerfile can contain multiple RUN command, but only one CMD command. Note that the specified after the CMD command, docker container run command can not append commands (such as in front of the / bin / bash), otherwise it will cover the CMD command. Now, start the container can use the following command. $ Docker container run --rm -p 8000: 3000 -it koa-demo: 0.0.1

11.5 release image file container runs successfully, it confirmed the validity of image files. At this point, we can consider sharing image files online, allowing others to use. First, go hub.docker.com or cloud.docker.com register an account. Then, log in with the following command.

$ Docker login Then, the user name and label for the local version of the image. $ Docker image tag [imageName] [username] / [repository]: [tag] # instances $ docker image tag koa-demos: 0.0.1 ruanyf / koa-demos: 0.0.1 or may not be marked user name, rebuild it image files. $ Docker image build -t [username] / [repository]:. [Tag] Finally, release image file. $ Docker image push [username] / [repository]: [tag] released after successful login hub.docker.com, you can see the image files have been released.

XII. Other useful commands

Docker is the main uses of these, in addition to several commands, also useful above. (1) in front of the docker container start docker container run command is new container, once per run, will create a new container. The same command run twice, it will generate two identical container file. If you want to re-use container, it is necessary to use docker container start command, which starts already generated container file has stopped running.

$ Docker container start [containerID] (2) in front of the docker container stop docker container kill command to terminate operation of the container corresponds with a SIGKILL signal to the main process inside the container. The docker container stop command is used to terminate operation of the vessel, corresponding to the main process SIGTERM signal sent inside the container, and then over a period of time with a SIGKILL signal. The difference between these two signals $ bash container stop [containerID], the application after receiving SIGTERM signal, may be ending their own clean-up work, but also can ignore this signal. If you receive a SIGKILL signal, it will be forced to immediately terminate the operation of those in progress will be lost. (3) docker container logsdocker container logs command to view the docker container output, i.e., the standard output Shell inside the container. If docker run command to run when the container is not used -it argument, you must use this command to view the output. $ Docker container logs [containerID] (4) docker container execdocker container exec command to enter a container docker running. If the time docker run command to run the container, do not use -it argument, you must use this command into the container. Once inside the container, you can execute commands in the Shell vessel. $ Docker container exec -it [containerID] / bin / bash (5) docker container cpdocker container cp command from running inside the container Docker, copy the file to the machine. Below is a copy of the wording of the current directory. $ Docker container cp [containID]: [/ path / to / file].

Guess you like

Origin www.cnblogs.com/772933011qq/p/11542185.html