Introduction to docker
Docker is an open source application container engine. Compared with traditional virtual machine technology, docker container performance overhead is extremely low, so it is also widely loved by developers. With more and more developers based on Docker, Docker images have become richer. In the future, various enterprise-level complete solutions can be used directly by downloading the images. Therefore docker becomes more and more important.
Purpose of this article
This article uses a project example to introduce how to manage and monitor docker containers through the docker external interface.
Application scenarios:
Manage the server resource pool in a unified manner through docker, allocate resources and create containers on demand to maximize resource utilization. At the same time, the isolation between each business (container) is ensured. And can support online deployment of projects.
1. Create a docker tomcat container through the interface and configure the quota.
2. Upload the war package and deploy it to the container.
3. Dynamically monitor the resource usage of all docker containers.
The code is based on docker-java open source components, and function development is completed based on spring-boot. The interface is as shown below:
Basic environment
1, eclipse 2019-03;
2, docker for windows. For the purpose of learning and simplicity, the Linux environment is not used, but the principles and basic commands are the same.
Related technologies and knowledge points:
maven, spring-boot; spring mvc; spring upload and download, jquery ajax and upload.
Implementation Principle
Docker provides rich interfaces (such as restfull api) to the outside world, and docker can be managed through these interfaces. The docker-java open source component is encapsulated based on this interface, making development easier. Of course, there are other packaged components on the market that you can compare and learn by yourself. Thank you to the open source authors here.
Preliminary preparation for the implementation process
1. Upgrade windows
Since we are using a windows environment, the author’s own computer is pre-installed with win10 family version, while docker for windows needs to run on a higher-level operating system to use virtualization technology, so it needs to be upgraded to the professional version.
If you need to upgrade, the upgrade process is relatively simple. A brief introduction is as follows (you can skip it if you are already a professional version):
Click to change the product password After entering the professional version key, wait for the gradual upgrade. You can find the key online by yourself, it is there anyway.
2. Install docker for windows
After downloading from the official website, just follow the next step. After installation, it will start by itself, and the docker logo will appear on the toolbar after startup.
After the installation is successful, you can type some commands to test whether it is successful. For example: View basic information about docker.
#3. Modify the docker image address.
Because the default mirror address is slow, a tomcat mirror is more than 500 meters long. If the network is not good, the download will be very slow. You can configure the mirror address to NetEase's mirror address, as follows:
4. Start remote management
Check the last item. The tcp//xxx inside is the remote connection address.
After starting, type the address in the browser to see if the result can be returned:
(This address is the same as entering docker indo in cmd) The content returned is in json format:
Implementation process code development
1. Create a maven project.
Since the process is simple, this process is omitted. The code structure of the entire project is as follows:
2. Configure the maven dependencies of docker-java and spring boot. The final pom file content is as follows
By looking at the jar package, the bottom layer of docker-java should use netty dependency.
3. Add the spring configuration file application.properties
Configure the location of jsp and the management address of docker.
4. Introduction to the main classes:
1.spring boot starts the main class.
Of course, you can also configure other containers or use maven's jetty plug-in to start.
You can refer to another article: spring-boot principle (with the implementation of a spring-boot-starter instance) with source code download
Pay attention to this class It is best to place it in the root directory, so that all subdirectories under the root directory can be scanned by spring.
2.dockerclientoperaterserver (class that interacts with docker), as follows:
Includes the following methods:
initclient: Initialization link.
getdockerinfo: Get docker basic information;
stat: real-time statistical information;
refreshcontainers: get basic information of all containers and put it in cache;
createandstartrcontaineranddeployapp: Create and start the container according to the parameters passed by the interface, and deploy the project at the same time;
stopcontainer: Stop the container;
rmcontainer: Delete the container;
closeclient: Close the link;
pushapptocotainer: Deploy the war package to tomcat;
Since the code snippet is too long, here are a few main methods:
createcontainer (Create container):
In this method, docker-java is used to create a container, allocate quotas, configure port mapping, etc.
pushapptocotainer: Deploy the project to tomcat
stat, monitoring method:
There is no docker-java method for monitoring this. In fact, It is supported, but since the returned results require various processing, an opportunistic approach is used. Use java to call cmd to execute (docker stats) to obtain monitoring results.
docker-java code to obtain monitoring information is as follows:
3.dockerclientcontroller (controller that interacts with the interface ).
Main method introduction:
addcontainer: add container and upload war package
The above is the explanation of the main code.
For specific code details, please download the source code and follow the process to view it.
Main interface after implementation:
Since it is a sample project, the code does not use any vue and bootstrap frameworks. I wrote some css and js myself. The specific effects are as follows:
1. Monitoring and management home page
2. Add container;
The above is the detailed content of How to implement dynamic management and monitoring of docker containers based on springboot and docker-java. For more information, please follow other related articles on the PHP Chinese website!