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

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

May 12, 2023 pm 03:25 PM
java docker springboot

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

See all articles