Loki is a horizontally scalable, highly available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be cost effective and easy to operate: instead of indexing full log contents, Loki indexes a set of labels for each log stream.
In this guide, we will learn how to install Grafana Loki and Promtail with Docker and Docker Compose. For production systems, please consider installing Grafana Loki with Tanka or Helm.
The configuration in this guide runs Loki as a single binary (great for local/dev). For production, consider Loki distributed mode and deploy with Helm or Tanka.
Related content:
- How to run Grafana OSS in docker and docker-compose
- How to run Prometheus with docker and docker-compose
- How to Setup Promtail, Grafana and Loki for free Log Management in Debian 11
- How to install and set up Grafana in Ubuntu 20.04 using Ansible
- How To Install and Configure Prometheus On a Linux Server
- How To Monitor Linux Servers Using Prometheus Node Exporter
Ensure Docker and Docker Compose are installed
Since we will be using Docker to run our setup, it is important that it is installed and working. If you are using Ubuntu, check out How to Install and Use Docker in Ubuntu 22.04.
Confirm that Docker is working as expected:
| |
Docker Compose is now included with Docker Desktop and is available on Linux as a Docker plugin. Confirm it’s available with:
| |
Running Grafana in Docker
Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.
In our case, we will need Grafana as a UI to the Loki system. We can use it to query the logs or create specific dashboards based on the log patterns.
To run Grafana with docker, use the following command. We are running the latest version of Grafana OSS - version 9.
| |
You can use the -d argument to start in detached mode. Once the server is started, access it by visiting http://server_ip:3000 in your browser.
Running Loki with Docker
Loki is a logging backend optimized for users running Prometheus and Kubernetes with great logs search and visualization. Loki works with promtail, an agent which ships the contents of local logs to an instance of Grafana Loki.
First, create a working directory:
| |
Next, create a configuration file required to start and run Loki. Save this as loki-config.yaml:
| |
Now we can run our loki instance with this command:
| |
Once Loki is running, run Promtail to ship logs from /var/log/*log to Loki. Create a file named promtail-config.yaml:
| |
Start promtail with this command:
| |
When both loki and promtail are running, navigate to http://server_ip:3100/metrics to view the metrics and http://server_ip:3100/ready for readiness.
Note: some Loki/Promtail images run as a non-root user by default. If you use bind mounts and hit permissions issues, either adjust directory permissions or run the container with --user (root is UID 0).
Using Docker Compose to run Grafana, Loki and Promtail
We can add the instructions above to a Docker Compose file to make it easy to create, update and manage the deployments. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Add the following to a docker-compose.yaml file:
| |
Save the file in the directory with the configurations then start the services with this command:
| |
Or you can add the -d argument for detached mode. Since we are binding the same ports, you can access in the same way as listed above.
Logging in to Grafana
Login with your admin user (default admin/admin). You will be prompted to change your password the first time you login.
Open the side menu (Grafana icon) → Connections → Data sources → Add data source → choose Loki. Set the URL to http://loki:3100 and click Save & test.
Next go to Explore and choose Loki as the data source. To view /var/log/*log logs, choose labels → job → varlogs then run a query.
| |
You should see the logs streaming in.
Conclusion
In this guide we managed to run Grafana, Loki, and Promtail using Docker and Docker Compose, then query logs from Grafana Explore.