Docker log plugin, Loki and Docker container logging

Docker log plugin, Loki and Docker container logging
December 20, 2023
In today's world, containerization plays a key role in the rapid and efficient deployment of applications. With this growing trend comes new challenges, and one of them is the efficient monitoring and log collection from Docker containers. In this blog post, we will focus on one of the prominent solutions for logging in containers - Loki plugin for Docker.

Why logging in Docker?

Logging is not only important for diagnostics and error detection, but also for performance and security monitoring. In Docker containers,where applications run in isolation, log management is crucialto maintain transparency and operational efficiency.

Loki: Revolutionary logging for Docker

Loki, designed by Grafana, brings an innovative approach to container logging. Its abilityto efficiently collect and store logs from Docker containers brings improvedscalability and easy log management to modern container environments.

In the following sections we will discuss in more detail theinstallation of the Loki plugin, the configuration of the log driver in docker-compose.yml and practical examples of how Loki simplifies the analysis and work with logs.Let's discover together how Loki can make logging your Docker containers moreefficient and clearer.

What is Loki and its benefits

Loki is an open-source system forcollecting, storing and retrieving logs in container environments. Designedwith low resource intensity and efficiency in mind, Loki is an alternative totraditional log management systems. Its integrationwith Docker enables efficient log tracking and analysis directly from thecontainer base.

The benefits of Loki for Docker logging:

  • Scalability: Loki is optimized for scalability, which means it can easily handle a large volume of logs generated in containers.
  • Storage   efficiency: With its architectural approach using indexed chunks of data, Loki minimizes storage requirements, which is key for multi-container environments.
  • Polling and filtering: Loki allows users to perform sophisticated queries and filters on logs, making it easy to quickly find and analyze the information they need.
  • Easy integration with Docker: Loki can be easily integrated with Docker containers, simplifying the configuration process and integration into existing container infrastructures.
  • Graphical interface: Loki works with the Grafana platform, which enables log visualization and facilitates log tracking and analysis with interactive graphs and dashboards.

In the next parts of the blog, we will explore the installation of theLoki plugin for Docker and practical examplesof its use in more detail. Loki provides a modern solution for logging incontainers, allowing us to better understand and manage logs in the dynamic environment of Docker containers.

Loki plugin installation using docker plugin

1. Download and install the Loki plugin:

Use the docker plugin install command to install the Loki plugin. Make sure you have installed a version of Docker that supports plugins.


docker plugin install grafana/loki-docker-driver:latest --alias loki--grant-all-permissions


This command will download and install the Loki plugin into your Docker environment. The --alias parameter defines an alias for ease of use in the log driver configuration.

2. Run the Loki plugin as a Docker service:

After installation, run the Loki plugin as a Docker service. Make sure the plugin is running on the expected port.

3. Docker daemon with Loki driver:

Restart the Docker daemon so that the changes to the plugin configuration take effect. Also configure the Docker daemon to accept the Loki log driver.


Add configurationto the file to enable Loki log driver


4. Save your changesand restart the Docker daemon

After rebooting,the Docker daemon should be ready to use the Loki log driver tocollect logs from containers. Make sure the Lokiplugin is activated and configured as expected.

Adding a log driver to docker-compose.yml

Adding the Loki log driver to the docker-compose.yml file is a key stepin getting logs from specific containers and redirecting them to the Lokisystem. Below is an example of how to perform thisconfiguration:

Adding Loki log driver for container:

In each container you want to collect logs from, add a configuration for the Loki log driver. Here is an example for a container named myapp...

  • loki-url: Specifies the URL of the Loki server where the logs should be sent.
  • loki-retries: number of retries in case of failed log submission.
  • loki-batch-size: The maximum size of a batch of logs sent at one time.

Loki addiction:

Make sure that youhave Loki defined in the services section of docker-compose.yml, which is referenced in the log driverconfiguration.

After these modifications, the myapp containerwill log its events using the Loki log driver.Loki then collects these logs and stores them in its repository, allowing for subsequent analysis and monitoring using theGrafana platform.

In the next part of the blog, we will look at configuring Loki for logcollection and a more detailed look at practical examples of running Docker with Loki logging enabled on Linux.

Configuring Loki for log collection

After successfully adding the Loki log driver to docker-compose.yml, it is now time to focus on configuringLoki to efficiently collect and store logs from Docker containers. Thefollowing steps will help you with this configuration.

Creating a configuration file for Loki:

Create a configuration file for Loki that specifies where and how the logs should be stored. Let's show an example of the loki-local-config.yaml file:

This file contains the configuration for Loki ingester, chunk lifecycle and index storage.

Starting Loki with a specific configuration file:

Run Loki with a specific configuration file...

docker run -v /path/to/loki-local-config.yaml:/etc/loki/local-config.yaml -p 3100:3100 grafana/loki:latest -config.file=/etc/loki/local-config.yaml
This will start Loki with specific settings for saving logs.

Loki availability check:

Check the availability of Loki at http://localhost:3100. This address should be the same as the address you specified in the log driver configuration in docker-compose.yml.

Once these stepsare completed, Loki will be configured to collect logs from specific containersusing the Loki log driver. In the next part of the blog, we will take adetailed look at examples of running Docker containers with Loki loggingenabled on Linux.

Examples of running Docker with logging onLinux

When running Docker containers on Linuxwith Loki logging enabled, it is crucial to followthe correct command syntax and include specific parameters for the Loki logdriver. The following examples illustrate how to run Docker containers withLoki logging enabled.

A simple example:

A basic example of running a container with the Loki log driver activated.

docker run --log-driver=loki --log-opt loki-url="http://loki:3100/loki/api/v1/push" --log-opt loki-retries="5" --log-opt loki-batch-size="100" myapp-image:latest

Using docker-compose:

If using docker-compose.yml, add the Loki logdriver configuration to the specific service.

With the setting of environment variables:

Example of running a container with the Loki log driver activated using the environment.

docker run -e "DOCKER_LOG_DRIVER=loki" -e "DOCKER_LOG_OPTS=loki-url=http://loki:3100/loki/api/v1/push,loki-retries=5,loki-batch-size=100" myapp-image:latest

These examples canbe used to run containers with Loki logging enabled, allowing for efficient monitoring and logging into the Lokisystem. Later in the blog, we'll look at analyzing and querying these logsusing Loki and Grafana to gain deeper insight into the behavior of ourapplications in a containerized environment.

Analysis and queries on logs

Suppose we have successfully set up Lokito collect logs from our Docker containers. Now let's take a look at how Lokimakes it easy to analyze and query the logs. To do this, we'll use theintegration with the Grafana platform, which provides convenient and visually appealing tools for working with logs.

Grafana Dashboard for Loki:

Make sure Grafana is installed next to Loki. Then create a Grafana dashboard that will be linked to Loki and display relevant log information. Grafana dashboards can include charts, tables, and other visualization elements.

Inquiry in Loki:

Loki supports the PromQL query language, which allows you to perform flexible queries on logs. Query examples.

  • Get all error messages: app_level="error"
  • Filteringby container: container_name="myapp"
  • Time range specification: time>ago(1h)


Using Loki labels

Loki uses labels to identify and label logs. Adding labels when logging makes it easier to categorize and filter logs according to specific criteria.

Alerts and event tracking:

In Grafana, you can set up alerts for specific events or trends in logs. This increases the efficiency of monitoring and responding to important changes.

Export and share dashboards:

Grafana allows you to export the dashboards you create, making it easy to share configuration and log analysis with your team or community.

At this stage, Loki is integrated with Grafana and ready for loganalysis from Docker containers. In the next part of the blog, we will focus onbest practices and tips for using Loki and logging effectively in a Docker environment.

Best Practices and Tips

When working with Loki and logging in aDocker environment, it is crucial to followbest practices and use tips that will improve the efficiency and reliability of log management. Hereare some best practices and tips:

  • Storage optimization: Maintain the efficiency of Loki storage. For example, you can regularly archive or delete old logs to prevent uncontrolled data growth.
  • Backup configuration: Regularly back up your Loki and Grafana configuration files to minimize the risk of data loss during unexpected events.
  • Centralized log driver configuration: If you manage multiple containers, you can create a centralized Loki log driver configuration and share it across all containers.
  • Safety considerations: Ifyou use Loki in a production environment, pay attention to security.Consider using encryption and authentication methods toprotect logs.
  • Using Loki labels: Make maximum use of labels to structure and filter logs. Properly labeled logs simplify analysis and tracking of key events.
  • Loki performance monitoring: Regularly monitor Loki performance to prevent overload and ensure smooth operation of log collection and storage.
  • Automation of updates: Keep Loki and other related components up-to-date and use automated update procedures to get new features and bug fixes.
  • Documentation and training: Ensure that the configuration, use and maintenance of Loki and Grafana are properly documented. Provide training to the team on the effective use of these tools.

By following these best practices and tips, you'll be able to more effectively manage logs in Docker containers and extract valuable information for monitoring and diagnostics.

Conclusion and References

In the final part of our journey throughlogging in Docker containers with Loki, we explored key aspects from installation to practical log analysis. We gained adeeper insight into how Loki provides a modern and efficient solution forcollecting, storing and analyzing logs in the containerization world.

What we've learned:

  • Loki installation: We figured out how to integrate Loki with Docker using Docker Compose and then configured it for log collection.
  • Loki configuration: We examined key parts of the Loki configuration file that affect log collection, storage, and availability.
  • Practical use: We presented examples of running Docker containers with Loki logging enabled and showed how these logs can become a valuable tool for analysis.
  • Best Practices and Tips: We shared best practices and tips for effectively using Loki and logging in a Docker environment.

Links for further study:

  • Loki documentation: for a deeper understanding of Loki and its configuration.
  • Grafana documentation: for extending log analysis with visualizations in Grafana.
  • PromQL Documentation: for detailed information about the PromQL queries that Loki uses.

Using thisknowledge, you should be able to effectively log and analyze the behavior ofyour Docker containers. If you have any additionalquestions or need an extension on any of thespecific topics, feel free to ask. Thank you for your attention and I wish yousuccess in your projects!

Share:
Pavel is a trained DevOps guru and keen SysAdmin in charge of the application lifecycle, continuous integration, continuous deployment, and continuous delivery for both customer and internal SABO projects. He trains software developers in our team and coordinates all DevOps activities. He always has some puns up his sleeve, unpredictably switches to Polish, and likes to cook and bake.

Article collaborators

SABO Newsletter icon

SABO NEWSLETTER

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

About SABO Mobile IT

We focus on developing specialized software for our customers in the automotive, supplier, medical and high-tech industries in Germany and other European countries. We connect systems, data and users and generate added value for our customers with products that are intuitive to use.
Learn more about sabo