Why use docker, how to use docker and its advantages/benefits in web development
Docker means Docker, and the function of Docker is similar to that of a container. Usually, a web app does not use all the functions of the operating system/virtual machine, that is, the operating system/virtual machine is too bulky for a web app. For example, if I want to go from the dormitory to the cafeteria. I don’t need an airplane, I only need a bicycle. The Docker image is equivalent to a bicycle. You can understand the Docker image as a lightweight virtual machine, of course, the two are different, but it is beyond the scope of this tutorial.
How to use Docker
Imagine a scenario as consider you need a web app whose function is to register users and insert the registration information into the database. The environment is Ubuntu+Tomcat+Mysql, If you don’t use Docker, you usually do this, using Ubuntu as the operating system, then installing Tomcat and MySQL, and finally deploying the app. In this scenario, there are two ways
1. Still use Ubuntu as the operating system, then build a Docker image with MySQL and Tomcat installed, deploy the app to it, and finally start Docker Mirroring. It seems to be basically the same as not using docker
2. The second method reflects Docker’s “one task per container” idea. We build two images, one only installs Tomcat and deploys our app, the other only installs MySQL, and then starts these two images, one image, two containers are obtained, and then the Docker container interconnection technology is used to connect the two (Docker containers are connected through HTTP).
Advantages of using docker
The biggest advantage of using docker is that you can package and deliver your application and the environment and configuration that the application depends on so that it can be used out of the box.
To deploy applications using containers, you only need to download an image you need, and then run the image, which is the same as starting a binary program.
After the container is started, the application can be accessed directly, without the need to deploy the application. If you are using a virtual machine, you have to install the virtual machine first, then install the operating environment, then install the application, configure, and then start the application, which is troublesome, and the whole process is time-consuming and laborious.
In a virtual machine environment, you can also use templates to automate all these processes, or you can use them out of the box. But the problem is that the creation and operation of virtual machines are time-consuming and resource-intensive, and Docker containers can be started in seconds. Although some virtual machine technologies can be started quickly and occupy very little resources, in general, they are still not lightweight and convenient. With docker mirroring, it is easy to ensure the consistency of development, testing, and production environments. Virtual machines are more suitable for use as IT infrastructure, and containers are more suitable for running applications.
Benefits of Docker
You actually use Docker, when you will feel that you can’t put it down. Roughly speaking, the benefits of Docker are:
- Ensure the consistency of the online and offline environments. After we use Docker to build the image of the web app in the offline development environment, we can directly use an image online to ensure the consistency of the online and offline environments. There will no longer be various errors that run normally in the offline development environment and deploy to the online.
- Greatly simplifies the deployment process of the web app, when we are not using Docker, we must first set up the environment required for the app to run when we deploy the app. Anyone who has done this process knows how boring and tedious it is, and it makes mistakes. With Docker, we only need to directly build an image of our web app and run it, no matter how many servers it is deployed on, this is the case. For another example, it is difficult for novices to build a WordPress before using Docker. With Docker, you only need to pull a WordPress image from DockerHub and start it, which is very convenient.
- The sandbox mechanism is realized and the security is improved.
Since the web app runs in the container and is isolated from the operating system, the operating system is basically impossible to be damaged. In addition, if the web app is paralyzed due to an attack, there is no need to restart the server. Just restart the container or start another image.
- Modularization is realized and reusability is improved. The second way of using Docker in the second can be seen. We run the database and Tomcat in different containers. From a certain point of view, this also reduces The coupling between modules is easy to expand. For example, if we want to replace MySQL with oracle, we only need to build another oracle image and start the connection with Tomcat, which is very convenient. For the mirror image we built, it can be used directly in other apps, and there is no need to repeat the work.
- Realized virtualization and improved hardware utilization.
With Docker, we can run many web apps on one server and make full use of idle resources. At this time, the operating system of the server is similar to the freighter, and each Docker container is equivalent to the containers on the freighter. Many of the hot cloud services markets now use Docker. For example, now we have a server with the operating system Ubuntu 14.04. We build different versions of ubuntu images and start them, and assign different containers to different users. In this way, one server can be used to virtualize n virtual servers running different operating systems, and for users, these are transparent-users think that they have a complete server.