search
HomeJavajavaTutorialHow to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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):

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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.

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

After the installation is successful, you can type some commands to test whether it is successful. For example: View basic information about docker.

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

#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:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

4. Start remote management

Check the last item. The tcp//xxx inside is the remote connection address.

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

2. Configure the maven dependencies of docker-java and spring boot. The final pom file content is as follows

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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.

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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.

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

docker-java code to obtain monitoring information is as follows:

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

3.dockerclientcontroller (controller that interacts with the interface ).

Main method introduction:

addcontainer: add container and upload war package

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

2. Add container;

How to implement dynamic management and monitoring of docker containers based on springboot and docker-java

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft