Day 75 - Sending Docker Log to Grafana

Day 75 - Sending Docker Log to Grafana

Prerequisites

Before we embark on this journey, ensure that you have the following:

  1. An AWS account with an EC2 instance running Linux.

  2. Basic knowledge of Docker.

  3. Grafana installed and accessible.

Step 1: Install Docker and Start the Service

We begin by installing Docker on our Linux EC2 instance using the ‘USER DATA’ option. USER DATA allows us to run scripts when launching an EC2 instance. In this script, make sure to install Docker and start the Docker service. This sets the foundation for running containers.

Step 2: Create and Run Docker Containers

Now, let’s create two Docker containers and run basic applications within them. You can use a simple “to-do” application for this purpose. Docker’s ease of use makes it a great choice for containerizing applications, and it allows us to manage them efficiently.

# Example Docker command for running a container with a to-do app
docker run -d --name container1 -p 8080:80 your-todo-app-image
docker run -d --name container2 -p 8081:80 your-todo-app-image

This will launch two containers named container1 and container2, each exposing a to-do app on a different port.

Step 3: Integrate Docker Containers with Grafana

To monitor Docker containers using Grafana, you need to enable the Docker plugin within Grafana. Follow these steps:

  1. Log in to your Grafana instance.

  2. Go to “Configuration” from the side menu.

  3. Select “Data Sources.”

  4. Click on “Add data source.”

  5. Search for “Docker” and select it.

  6. Configure the Docker data source by providing the necessary details, such as the Docker API URL (usually tcp://localhost:2375) and a name for the data source.

Once your Docker data source is configured, Grafana can collect data from Docker, including logs.

Step 4: Monitor Docker Container Logs

Now that Grafana is connected to your Docker containers, you can start monitoring their logs in real-time. Follow these steps to view Docker container logs in Grafana:

  1. Create a new dashboard or open an existing one.

  2. Add a panel to the dashboard.

  3. In the panel settings, select your Docker data source.

  4. Choose the metric you want to display; in this case, select “Docker Logs.”

  5. Customize the panel to display the logs from your Docker containers, such as filtering by container name or other relevant criteria.

And there you have it! You now have real-time access to Docker container logs right within Grafana, making it easier than ever to monitor and troubleshoot your containerized applications