When to use this stack
Loki is a log storage system that indexes only the labels attached to each log stream, not the full text, which keeps it small and cheap to run. Promtail is the agent that reads log files, attaches labels such as job and host, and pushes the lines to Loki. Grafana gives you a query editor (LogQL) and a log browser, so you can filter by service, search for error text and see logs next to metrics. This stack runs all three on one host with the local filesystem as storage.
Use this stack when you want the system logs of one or a few servers in one searchable place, without the weight of Elasticsearch. It also works well alongside the Prometheus stack in this series, since both can share a Grafana instance. Do not use it as a security information system, because it stores what the applications print, including any personal data or tokens written to logs. Do not expect long retention from the default configuration. The local configuration that ships in the Loki image keeps data without an automatic deletion rule, so you must provide your own configuration before the disk becomes a problem. Also note that Promtail is being superseded by Grafana Alloy, so check its current status before you start a new deployment.
docker-compose.yml
services:
loki:
image: grafana/loki:3.1.1
restart: unless-stopped
command: -config.file=/etc/loki/local-config.yaml
expose:
- "3100"
volumes:
- loki_data:/loki
promtail:
image: grafana/promtail:3.1.1
restart: unless-stopped
command: -config.file=/etc/promtail/config.yml
volumes:
- ./promtail-config.yml:/etc/promtail/config.yml:ro
- /var/log:/var/log:ro
- promtail_positions:/var/lib/promtail
depends_on:
- loki
grafana:
image: grafana/grafana:11.2.0
restart: unless-stopped
environment:
GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER}
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD}
GF_USERS_ALLOW_SIGN_UP: "false"
ports:
- "127.0.0.1:${GRAFANA_PORT}:3000"
volumes:
- grafana_data:/var/lib/grafana
depends_on:
- loki
volumes:
loki_data:
promtail_positions:
grafana_data:# Grafana web port, bound to 127.0.0.1
GRAFANA_PORT=3001
# Grafana admin login. The password is applied on the first start only.
GRAFANA_ADMIN_USER=admin
GRAFANA_ADMIN_PASSWORD=CHANGE_ME_long_random_password.env
# Grafana web port, bound to 127.0.0.1
GRAFANA_PORT=3001
# Grafana admin login. The password is applied on the first start only.
GRAFANA_ADMIN_USER=admin
GRAFANA_ADMIN_PASSWORD=CHANGE_ME_long_random_passwordStart and verify
# 1. Create the Promtail configuration next to docker-compose.yml
cat > promtail-config.yml <<'EOF'
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /var/lib/promtail/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
host: CHANGE_ME_host_label
__path__: /var/log/*log
EOF
# 2. Validate, start and check each service
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=30 promtail
# 3. Verify Loki is ready (run inside the Docker network)
docker compose exec loki wget -qO- http://localhost:3100/ready
# 4. Verify Grafana answers
curl -s http://127.0.0.1:3001/api/healthIn Grafana, add a Loki data source with the URL http://loki:3100, open Explore and run the query {job="varlogs"}. Lines from /var/log should appear within a minute.
Production checklist
- Keep Loki unpublished. It is reachable only inside the Docker network, and Grafana is the only service with a host port.
- Promtail mounts /var/log read-only, but the logs themselves may contain IP addresses, usernames and tokens. Restrict Grafana access and avoid sharing query results.
- Add a retention policy in your own Loki configuration with
compactorretention enabled and aretention_periodin limits_config. The default local configuration does not delete old data. - Set ingestion limits in limits_config so one noisy service cannot fill the disk or overload the server.
- Keep
auth_enabled: falseonly on a private network. If several teams share the instance, enable authentication and tenant headers. - Keep the promtail_positions volume. It stores the read offset of each file, so a restart does not send every line again.
- Pin all three images to exact versions. Loki configuration keys change between minor releases, so test a new version on a copy first.
- Set GF_USERS_ALLOW_SIGN_UP to false and use a long admin password.
Common problems
- Symptom: Grafana shows “no data” for a query that should match. Cause: The time range is wrong, or the label in the query does not match the labels Promtail sends. Fix: Widen the time range and run
docker compose logs promtailto confirm that batches are being sent. - Symptom: Promtail logs “permission denied” for some files. Cause: Some files in /var/log are readable only by root. Fix: Keep the default root user for Promtail, or narrow the
__path__glob to the files you need. - Symptom: Loki responds with 429 or “rate limit exceeded”. Cause: The ingestion rate is above the limits in the configuration. Fix: Raise the limits in limits_config after you confirm the disk can handle the volume.
- Symptom: Grafana cannot connect to the Loki data source. Cause: The URL uses localhost, which points at the Grafana container. Fix: Use http://loki:3100.
- Symptom: The same log lines appear twice after a restart. Cause: The positions file was not kept between restarts. Fix: Keep the promtail_positions volume and make sure the positions path in the configuration matches the mount.
Variations
- Docker container logs: Use Promtail’s Docker service discovery to label logs per container. It requires read access to the Docker API, so use a socket proxy and treat the access as sensitive.
- Object storage for Loki: Switch the storage to S3-compatible object storage when the log volume grows beyond one disk.
- Grafana Alloy instead of Promtail: Choose Alloy for new deployments, since it is the successor agent and can collect metrics, logs and traces in one tool.
Questions
What is the difference between Loki and Prometheus?
Prometheus stores numeric metrics, while Loki stores log lines and indexes only their labels. Use Loki for text search in logs and Prometheus for counters and graphs.
Why does Grafana show no data?
Check the time range first, then run docker compose logs promtail and look for errors when it pushes batches to Loki. The label selector in the query must match the job label in promtail-config.yml.
Does Loki need a separate database?
No. The single-binary Loki image stores its index and chunks on the local filesystem inside the loki_data volume.
Why is Loki not published on a host port?
Only Promtail and Grafana need to reach Loki, and both do so over the Docker network. Keeping it unpublished reduces the attack surface.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Prometheus + Grafana + Node Exporter
Monitor a Linux host with Prometheus, Node Exporter and Grafana in Docker Compose. Pinned images, .env secrets, retention settings and localhost-only ports.
Caddy 2 Reverse Proxy + Static Web Server
Caddy 2 in Docker Compose: serve a static site, proxy /api to a backend and get automatic HTTPS on a real domain. Pinned images and .env configuration.
Gitea Git Server with PostgreSQL 16
Self-hosted Gitea with PostgreSQL 16 in Docker Compose: web Git hosting, SSH on port 2222, a healthcheck-gated database start and persistent volumes.
Portainer CE Web UI for Docker Management
Run Portainer CE in Docker Compose as a web UI for containers, images and volumes. Bound to localhost on HTTPS 9443, with persistent data and a pinned image.
Browse more Infrastructure & Monitoring stacks or the full library.