Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Ultimate Docker Container Book

You're reading from   The Ultimate Docker Container Book Build, test, ship, and run containers with Docker and Kubernetes

Arrow left icon
Product type Paperback
Published in Aug 2023
Publisher Packt
ISBN-13 9781804613986
Length 626 pages
Edition 3rd Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Dr. Gabriel N. Schenker Dr. Gabriel N. Schenker
Author Profile Icon Dr. Gabriel N. Schenker
Dr. Gabriel N. Schenker
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part 1:Introduction
2. Chapter 1: What Are Containers and Why Should I Use Them? FREE CHAPTER 3. Chapter 2: Setting Up a Working Environment 4. Part 2:Containerization Fundamentals
5. Chapter 3: Mastering Containers 6. Chapter 4: Creating and Managing Container Images 7. Chapter 5: Data Volumes and Configuration 8. Chapter 6: Debugging Code Running in Containers 9. Chapter 7: Testing Applications Running in Containers 10. Chapter 8: Increasing Productivity with Docker Tips and Tricks 11. Part 3:Orchestration Fundamentals
12. Chapter 9: Learning about Distributed Application Architecture 13. Chapter 10: Using Single-Host Networking 14. Chapter 11: Managing Containers with Docker Compose 15. Chapter 12: Shipping Logs and Monitoring Containers 16. Chapter 13: Introducing Container Orchestration 17. Chapter 14: Introducing Docker Swarm 18. Chapter 15: Deploying and Running a Distributed Application on Docker Swarm 19. Part 4:Docker, Kubernetes, and the Cloud
20. Chapter 16: Introducing Kubernetes 21. Chapter 17: Deploying, Updating, and Securing an Application with Kubernetes 22. Chapter 18: Running a Containerized Application in the Cloud 23. Chapter 19: Monitoring and Troubleshooting an Application Running in Production 24. Index 25. Other Books You May Enjoy

Collecting and scraping metrics

To collect and scrape metrics from containers running on a system with Docker Desktop installed, you can use Prometheus and Container Advisor (cAdvisor). Prometheus is a powerful open source monitoring and alerting toolkit, while cAdvisor provides container users with an understanding of the resource usage and performance characteristics of their running containers.

In this section, we’ll provide a step-by-step guide to setting up Prometheus and cAdvisor to collect and scrape metrics from containers running on Docker Desktop.

Step 1 – running cAdvisor in a Docker container

cAdvisor is a Google-developed tool that collects, processes, and exports container metrics. Let’s take a look:

  1. In the chapter folder, ch12, create a new subfolder called metrics:
    mkdir metrics
  2. In this folder, create a file called docker-compose.yml and add the following snippet to it:
    version: '3.8'
    services:
      cadvisor:
        image: gcr.io/cadvisor/cadvisor:v0.45.0
        container_name: cadvisor
        restart: always
        ports:
        - 8080:8080
        volumes:
        - /:/rootfs:ro
        - /var/run:/var/run:rw
        - /sys:/sys:ro
        - /var/lib/docker/:/var/lib/docker:ro
  3. Run cAdvisor in a Docker container using the following command:
    docker compose up cadvisor --detach

    Replace v0.45.0 with the latest cAdvisor version available on the cAdvisor repository.

    This command mounts the necessary directories from the host system and exposes cAdvisor’s web interface on port 8080.

Attention

A version lower than the one shown here will not run, for example, on a Mac with an M1 or M2 processor.

  1. You can access the cAdvisor web interface by navigating to http://localhost:8080 in your browser.

Step 2 – setting up and running Prometheus

Next, let’s set up Prometheus using the following step-by-step instructions:

  1. Create a subfolder called prometheus in the metrics folder.
  2. In this new folder, create a prometheus.yml configuration file with the following contents:
    global:
      scrape_interval: 15s
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']
      - job_name: 'cadvisor'
        static_configs:
          - targets: ['host.docker.internal:8080']

    This configuration specifies the global scrape interval and two scrape jobs: one for Prometheus itself and another for cAdvisor running on port 8080.

  3. Add the following snippet to the end of the docker-compose.yml file:
    prometheus:
      image: prom/prometheus:latest
      container_name: prometheus
      restart: always
      ports:
        - 9090:9090
      volumes:
        - ./prometheus:/etc/prometheus
        - prometheus_data:/prometheus

    This instruction mounts the prometheus.yml configuration file and exposes Prometheus on port 9090.

  4. The preceding prometheus service uses a volume called prometheus_data. To define this, please add the following two lines to the end of the docker-compose.yml file:
    volumes:
      prometheus_data:
  5. You can access the Prometheus web interface by navigating to http://localhost:9090 in your browser.

Once Prometheus is up and running, you can verify that it’s successfully scraping metrics from cAdvisor:

  1. Open the Prometheus web interface at http://localhost:9090.
  2. Click on Status in the top navigation bar, then select Targets.
  3. Ensure that both the prometheus and cadvisor targets are listed with State set to UP.

Now, Prometheus can collect and store metrics from the containers running on your Docker Desktop system. You can use Prometheus’ built-in expression browser to query metrics or set up Grafana for advanced visualization and dashboarding:

  1. In the query text field, enter something like container_start_time_seconds to get the value for the startup time of all containers.
  2. To refine the query and only get the value for the cAdvisor container, enter container_start_time_seconds{job="cadvisor"}.

Note that in the query text field, you get IntelliSense, which is convenient when you do not remember all the details of a command and its parameters.

Before you continue, stop cAdvisor and Prometheus with the following command:

docker compose down -v

In the last section of this chapter, you will learn how to monitor a containerized application using a tool such as Grafana.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image