Day 75 of 90 Days of DevOps Challenge: Sending Docker Logs to Grafana
Today, we will take your monitoring skills to the next level by integrating Docker with Grafana. We'll install Docker on a Linux EC2 instance, run two Docker containers, and send their logs to Grafana.
Task Overview
Install Docker on a Linux EC2 Instance via User Data.
Create and Run Two Docker Containers (Todo App).
Integrate Docker Logs with Grafana.
Step-by-Step Guide
Part 1: Install Docker via User Data
Launch a New Linux EC2 Instance:
- Use the following User Data script to install Docker and start the Docker service:
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
Connect to the EC2 Instance:
- Use SSH to connect to your instance after it's running:
ssh -i "your-key.pem" ec2-user@your-linux-ec2-public-ip
Part 2: Create and Run Two Docker Containers
Pull the Todo App Docker Image:
- You can use a simple Todo application image from Docker Hub. For example:
docker run -d --name todo-app1 -p 8081:80 <docker-todo-app-image>
docker run -d --name todo-app2 -p 8082:80 <docker-todo-app-image>
- Replace
<docker-todo-app-image>
with an actual Todo app image name from Docker Hub. You can use a simple web application liketodobackend
or any other lightweight Todo app.
Verify the Containers are Running:
docker ps
Part 3: Integrate Docker Logs with Grafana
The challenge suggests uing Grafana Docker Plugin however its no longer in use and now we have to get the logs using loki or prometheus
Install Loki:
You can run Loki as a Docker container:
docker run -d --name=loki -p 3100:3100 grafana/loki:latest -config.file=/etc/loki/local-config.yaml
Install Promtail:
Promtail is a log collector for Loki, which helps to collect logs from Docker containers.
docker run -d --name=promtail -v /var/log:/var/log -v /etc/promtail:/etc/promtail grafana/promtail:latest -config.file=/etc/promtail/config.yml
Configure Promtail:
Promtail configuration (/etc/promtail/config.yml) to scrape Docker logs:
server: http_listen_port: 9080 grpc_listen_port: 0 clients: - url: http://<LOKI_HOST>:3100/loki/api/v1/push scrape_configs: - job_name: docker docker_sd_configs: - host: unix:///var/run/docker.sock relabel_configs: - source_labels: [__meta_docker_container_name] target_label: container
Add Loki as a Data Source in Grafana:
Go to your Grafana instance.
Navigate to Configuration → Data Sources. Click Add Data Source and search for Loki.
Enter http://<LOKI_HOST>:3100 as the URL and click Save & Test.
Create a Dashboard:
Create a new dashboard and add a panel.
In the Query section, select Loki as the data source.
Use LogQL queries to visualize your Docker logs, e.g.,
{job="docker"}
Visualize Docker Logs:
- Now, you should be able to see the real-time logs from both Docker containers in your Grafana dashboard.
Conclusion
You have successfully set up Docker on a Linux EC2 instance, created two containers running a Todo app, and integrated their logs with Grafana. This project will significantly enhance your resume and monitoring skills. Let me know if you need any help with configurations or troubleshooting!