Prerequisites
Before we embark on this journey, ensure that you have the following:
An AWS account with an EC2 instance running Linux.
Basic knowledge of Docker.
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:
Log in to your Grafana instance.
Go to “Configuration” from the side menu.
Select “Data Sources.”
Click on “Add data source.”
Search for “Docker” and select it.
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:
Create a new dashboard or open an existing one.
Add a panel to the dashboard.
In the panel settings, select your Docker data source.
Choose the metric you want to display; in this case, select “Docker Logs.”
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