With the development of container technology, Docker has become one of the most popular container platforms currently. Docker can not only make applications more lightweight and cross-platform, but also improve application portability and flexibility. Docker also provides a wealth of tools and components to realize automated operation, maintenance and monitoring of containers. In this article, we will delve into how to use Docker for automated operation, maintenance and monitoring of containers.
Prerequisite knowledge
Before learning how to use Docker for automated operation, maintenance and monitoring of containers, we need to master the following basic knowledge:
Automated container operation and maintenance
Docker provides some automated operation and maintenance methods, making container life cycle management more convenient and efficient. In this chapter, we will introduce three aspects of using Docker for automated container operation and maintenance:
When our containers are abnormal, we can Using the automatic restart function provided by Docker, Docker will automatically restart the container when the container stops running.
docker run --restart always image_name
When the Docker image version is updated, Docker provides an automatic update method to automatically pull and start the container from the new version of the image.
docker run -d --name my_container --restart=always image_name:latest
In addition to Docker’s own container health check, we can also use Docker’s own monitoring tools, such as the Docker Stats command for real-time monitoring Container running status information.
docker stats container_name或者container_id
Container Monitoring
Docker provides tools for monitoring containers. One of the more commonly used ones is Prometheus, which is an open source tool set for indicator recording and display, and can implement container Time series data collection and presentation. In this section, we will introduce in detail how to use Prometheus to monitor Docker containers.
First, we need to download the latest installation package from the official website of Prometheus (https://prometheus.io/download/), and then unzip it to Linux.
tar -zxvf prometheus-*.tar.gz cd prometheus-*
Add the following content to the prometheus.yml file to configure Docker monitoring on Prometheus
scrape_configs: - job_name: 'prometheus' scrape_interval: 5s static_configs: - targets: ['localhost:9090']
The easiest way to start Prometheus related services (Docker daemon, Prometheus) is to use Docker Compose. The following is a sample docker-compose.yml file for starting Prometheus and related services.
version: '3' services: prometheus: image: prom/prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml restart: always
Add the following content to the prometheus.yml file to configure Docker monitoring on Prometheus.
scrape_configs: - job_name: 'docker' scrape_interval: 5s static_configs: - targets: ['localhost:9323']
To export Docker status as Prometheus metrics, you need to use Prometheus Exporter. The following is an example of docker-compose.yml file.
version: '3' services: prometheus: image: prom/prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml restart: always prometheus-exporter: image: prom/node-exporter:v0.15.2 command: - '--path.rootfs=/hostfs' ports: - "9323:9323" volumes: - /proc:/hostfs/proc:ro - /sys:/hostfs/sys:ro - /:/hostfs:ro restart: always
Restart the Docker service and check the monitoring information of Prometheus. You can see the CPU, memory and other indicators, as well as the Docker daemon's index.
sudo systemctl daemon-reload sudo systemctl restart docker docker-compose up http://localhost:9090
Summary
This article introduces how to use Docker for automated container operation, maintenance and monitoring, starting with automatic container restart, automatic container update, and automatic container monitoring, and explains in detail how to use Prometheus to monitor Docker container running status. We learned that using Docker can make container operation, maintenance and monitoring more efficient and simpler, allowing us to better manage containerized applications.
The above is the detailed content of How to use Docker for automated operation, maintenance and monitoring of containers. For more information, please follow other related articles on the PHP Chinese website!