Sitemap
Julie Gvozdikova

Julie Gvozdikova

Full stack developer(Angular/.Net)

How to handle Docker logging using Grafana’s Loki

4 min readMar 14, 2023

Working with logs inside Docker containers can be tiring. Firstly, you can’t see them immediately as you can see them while running containers locally. Secondly, if container is killed all you logs disappear.

The situation might become worse if you have more than 1 server. Because if some error happens and you want to check it you need to check every server’s logs in order to find it.

There is a way to make all these problems gone and it’s name is Loki.

Loki is a log aggregation system in Grafana Cloud and it’s a peace of cake to start working with it.

We will start with an example of docker-compose.yaml file.

I hope you are already familiar with docker-compose. If not — to make long story short — docker-compose allows you to start multiple containers together with one command.

Look at the picture. Here we list all the containers we are going to start. They are our application that we want to get logs from, containers for Loki, Promtail and Grafana.

Press enter or click to view image in full size
Example of docker-compose.yaml file where we list all the services we need to start.

In “lokitestapplication” description we are interested in “logging” section. Here we specify that our logging driver is Loki and specify the URL where we have it.

We’ve already mentioned Loki and Grafana. But what Promtail is and why do we need it?

Promtail is an agent that gets your local logs from the application, label them and ships them to Loki.

To make this work we also need configuration files for Loki and Promtail. They are pretty standard and in most cases are taken from official GitHub.

Here is an example for Loki’s configuration file. The name of it is loki-config.yaml.

auth_enabled: false

server:
http_listen_port: 3100
grpc_listen_port: 9096

common:
instance_addr: 127.0.0.1
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory

query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100

schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h

ruler:
alertmanager_url: http://localhost:9093

Config for Promtail can look like this.

server:
http_listen_port: 9080
grpc_listen_port: 0

positions:
filename: /tmp/positions.yaml

clients:
- url: http://loki:3100/loki/api/v1/push

scrape_configs:
- job_name: dotnet
pipeline_stages:
- stdin:
docker: dotnet
- labels:
job: dotnet

Pay attention to section “clients” — where we specify the url for Loki and “scrape_configs” — where we specify where Promtail should search for logs. In our example row “stdin” means that it should take every log from console. All configs are on the same level with docker-compose.yaml.

Get Julie Gvozdikova’s stories in your inbox

Join Medium for free to get updates from this writer.

Seems like everything is ready for start. Let’s run a command “docker-compose -f docker-compose.yml up” in your console.

If everything is OK and all your containers are running, let’s go to Grafana (http://localhost:3000/) to add Loki. Default credentials for local Grafana is login “admin” and password “admin”.

Here in Settings section you need to choose Data Sources. Then find Loki source. Click on it.

Press enter or click to view image in full size

Then specify the URL for your running Loki container(http://loki:3100).

Press enter or click to view image in full size

Click button “Save and Test” on the bottom of the page. If Promtail is working properly you will get a success message that data source is connected and labels are found.

Press enter or click to view image in full size

Now you can find your logs in “Explore” section. You can filter them by service name(if you have several containers with logs you want to observe). To apply filter use “Run query” button.

Press enter or click to view image in full size

Then you can see all logs for your query and logs timeline. Also you are able to download then in .txt format.

P.S. The example above shows the case where you have all the containers in one docker-compose.yaml file. If you already have Grafana and it is running on different server the process will not change a lot. If you run Grafana with docker run command just create a docker-compose.yaml file and add Grafana and Loki services to it. Then in docker-compose.yaml for you application add section “logging” and add Promtail service.

P.P.S. When running docker-compose on real server pay attention to “volume” section for Loki and Promtail services. It needs to show Docker where to find files with configurations. Without “volume” section you can get an error than config files don’t exist.

Hope this information will help you to make your work with logs more easy!

--

--

Julie Gvozdikova
Julie Gvozdikova

Written by Julie Gvozdikova

Full stack developer(Angular/.Net)