# Monitoring in Docker: Code Examples, Demos, and Results

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/devops/monitoring-in-docker-code-examples-demos-and-results'>Monitoring in Docker: Code Examples, Demos, and Results</a></div><div class="line-post"> 
 <h2 class="title-post">1. Introduction to Docker Monitoring</h2> 
</div>
<div class="line-post">
  Monitoring Docker containers involves tracking various metrics and logs to ensure that your applications are running smoothly and to troubleshoot any issues that arise. Effective monitoring helps in identifying performance bottlenecks, understanding resource usage, and ensuring the overall health of your containers. 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Setting Up Monitoring with Docker</h2> 
</div>
<div class="line-post">
  To get started with monitoring, you need to choose the right tools and configure them to collect and visualize data from your Docker containers. Here’s a step-by-step guide to setting up a comprehensive monitoring solution. 
</div>
<div class="line-post"> 
 <div class="post-ref" post-ref-url="https://www.tuanh.net/blog/devops/spring-boot-applications-with-docker-and-kubernetes" post-ref-image="https://resource.tuanh.net/article/image/c784d692-5da4-44f9-b96b-e89a1bc63c05.png" post-ref-title="Spring Boot Applications with Docker and Kubernetes" post-ref-description="Deploying Spring Boot applications on Docker and Kubernetes can significantly streamline development and operations, offering scalability, portability, and simplified management. This guide will walk you through the process with detailed examples, demos, and results."></div> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Using Docker Stats</h3> 
</div>
<div class="line-post">
  Docker provides built-in commands for monitoring the resource usage of containers. The docker stats command is a straightforward way to get real-time statistics. 
</div>
<div class="line-post"> <strong>Example Code:</strong> 
</div>
<pre class="code">docker stats</pre>
<div class="line-post">
  Demo Result: 
</div>
<div class="images-post"> 
 <img width="80%" class="element_344176-image" src="https://resource.tuanh.net/article/image/fdf0ba5c-f25a-429f-b08f-5a6ed86bfc25.png" alt="Image"> 
</div>
<div class="line-post">
  This output shows real-time CPU usage, memory usage, and network I/O for each container. It’s a useful command for quick checks. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.2 Using docker ps</h3> 
</div>
<div class="line-post">
  The docker ps command lists all running containers along with their essential details. 
</div>
<div class="line-post"> <strong>Example Code:</strong> 
</div>
<pre class="code">docker ps</pre>
<div class="line-post"> <strong>Demo Result:</strong> 
</div>
<pre class="code">CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS          PORTS                    NAMES<br>abcd1234efgh   my-image       "python app.py"          2 hours ago    Up 2 hours      0.0.0.0:8080-&gt;8080/tcp   my-container</pre>
<div class="line-post">
  The output shows each container's ID, image, command, creation time, status, exposed ports, and name. This information is useful for understanding which containers are running and their basic configuration. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.3 Using docker top</h3> 
</div>
<div class="line-post">
  The docker top command displays the running processes inside a container, similar to the top command in Linux. 
</div>
<div class="line-post"> <strong>Example Code:</strong> 
</div>
<pre class="code">docker top my-container</pre>
<div class="line-post"> <strong>Demo Result:</strong> 
</div>
<pre class="code">UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD<br>root                12345               1                   0                   16:00               ?                   00:00:01            python app.py</pre>
<div class="line-post">
  This command provides details about the processes running within the specified container, including their process IDs (PIDs), CPU usage, and command. It’s useful for diagnosing issues related to containerized applications. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.4 Using Sysdig</h3> 
</div>
<div class="line-post">
  Sysdig is a powerful tool for container monitoring and troubleshooting, offering a detailed view of system calls and container activity. 
</div>
<div class="line-post"> <strong>Example Setup:</strong> 
</div>
<div class="line-post"> <strong>Install Sysdig:</strong> 
</div>
<div class="line-post"> <strong>Run Sysdig:</strong> 
</div>
<pre class="code">sysdig</pre>
<div class="line-post"> <strong>Filter and Analyze:</strong> 
</div>
<div class="line-post">
  Use Sysdig to capture and analyze system calls related to Docker containers: 
</div>
<pre class="code">sysdig -c topprocs_cpu</pre>
<div class="line-post"> <strong>Demo Result:</strong> 
</div>
<pre class="code">Time (s)   Proc Name        PID     CPU%    Memory%<br>0.0        my-container    12345   0.5     12.3<br>0.5        another-container 67890   1.2     8.7</pre>
<div class="line-post">
  Sysdig’s output provides insights into CPU and memory usage of processes within containers. You can also use Sysdig to troubleshoot specific issues by capturing and analyzing system calls. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.5 Integrating Docker Monitoring with Prometheus</h3> 
</div>
<div class="line-post">
  For a more sophisticated monitoring setup, integrating Docker with Prometheus allows for detailed metric collection and visualization. 
</div>
<div class="line-post"> <strong>Run Prometheus:</strong> 
</div>
<div class="line-post">
  Create a prometheus.yml configuration file to scrape metrics from Docker: 
</div>
<pre class="code">global:<br>  scrape_interval: 15s<br>scrape_configs:<br>  - job_name: 'docker'<br>    static_configs:<br>      - targets: ['localhost:9090']</pre>
<div class="line-post">
  Start Prometheus: 
</div>
<pre class="code">docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus</pre>
<div class="line-post"> <strong>Run the Prometheus Docker Exporter:</strong> 
</div>
<pre class="code">docker run -d -p 9273:9273 --name=prometheus-docker-exporter --link my-container:my-container prom/dockerd-exporter</pre>
<div class="line-post"> <strong>Visualize Metrics with Grafana</strong> 
</div>
<div class="line-post">
  Add Prometheus as a data source in Grafana and create dashboards to visualize the collected metrics. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.6 Using ELK Stack for Log Management</h3> 
</div>
<div class="line-post">
  The ELK Stack (Elasticsearch, Logstash, and Kibana) is a popular solution for log management. It helps in aggregating and visualizing logs from Docker containers. 
</div>
<div class="line-post"> <strong>Example Setup:</strong> 
</div>
<div class="line-post"> <strong>Run Elasticsearch:</strong> 
</div>
<pre class="code">docker run -d -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.10.0</pre>
<div class="line-post"> <strong>Run Logstash:</strong> 
</div>
<div class="line-post">
  Create a logstash.conf file: 
</div>
<pre class="code">input {<br>  docker {<br>    codec =&gt; json<br>    port =&gt; 5044<br>  }<br>}<br>output {<br>  elasticsearch {<br>    hosts =&gt; ["http://localhost:9200"]<br>  }<br>}</pre>
<div class="line-post">
  Start Logstash: 
</div>
<pre class="code">docker run -d -p 5044:5044 -v $(pwd)/logstash.conf:/usr/share/logstash/pipeline/logstash.conf docker.elastic.co/logstash/logstash:7.10.0</pre>
<div class="line-post">
  Run Kibana: 
</div>
<pre class="code">docker run -d -p 5601:5601 docker.elastic.co/kibana/kibana:7.10.0</pre>
<div class="line-post"> <strong>Access Kibana:</strong> 
</div>
<div class="line-post">
  Navigate to http://localhost:5601 to view and analyze logs. 
</div>
<div class="images-post"> 
 <img width="80%" class="element_830527-image" src="https://resource.tuanh.net/article/image/8f432cb4-dad0-4ac6-b200-e3e17f61d04c.png" alt="Image"> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.7 Using cAdvisor for Container Metrics</h3> 
</div>
<div class="line-post">
  cAdvisor provides detailed statistics about container resource usage and performance. 
</div>
<div class="line-post"> <strong>Example Code:</strong> 
</div>
<pre class="code">docker run -d -p 8080:8080 --name=cadvisor google/cadvisor:latest</pre>
<div class="line-post">
  Access cAdvisor at http://localhost:8080 to view detailed metrics such as CPU usage, memory usage, and network statistics for each container. 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. Conclusion</h2> 
</div>
<div class="line-post">
  Monitoring Docker containers effectively involves using a combination of built-in tools, third-party monitoring solutions, and log management systems. By setting up Docker Stats, Prometheus with Grafana, ELK Stack, and cAdvisor, you can gain valuable insights into your containerized applications and ensure their optimal performance. 
</div>
<div class="line-post">
  If you have any questions or need further clarification, feel free to comment below! 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/devops/monitoring-in-docker-code-examples-demos-and-results'>Monitoring in Docker: Code Examples, Demos, and Results</a></p>
