Lightweight Ubuntu-based Varnish 7.0 image with:
- Prometheus exporter built-in (exposes metrics on 9131 when admin secret is present)
- Flexible storage backends (malloc or file)
- Access logging via varnishncsa with selectable formats
- Helper utilities for hot-reloading VCL and log management
- Optional Matomo log import helper
Image: pastakhov/varnish:7.0
Use this service definition as a starting point. It includes common settings, metrics, and a cron sidecar example for running Matomo log imports.
varnish:
container_name: ${COMPOSE_PROJECT_NAME}_varnish # don't allow to scale the container
image: pastakhov/varnish:7.0
restart: unless-stopped
networks:
- default
- traefik-public
depends_on:
- web
tmpfs:
- /var/lib/varnish:exec
environment:
- VARNISH_SIZE=5G
- VARNISH_STORAGE_KIND=file
- VARNISH_LOG_DIR=/var/log/varnish
- VARNISH_LOG_FORMAT=X-Real-IP
- MATOMO_USER=admin
- MATOMO_PASSWORD=${MATOMO_PASSWORD?Variable MATOMO_PASSWORD not set}
volumes:
- ./_resources/varnish:/etc/varnish:ro
- varnish_data:/data
- ./_logs/varnish:/var/log/varnish
- matomo_data:/var/www/html
labels:
# cron
- cron.enabled=true
# Every hour at the 59th minute
- cron.import_logs_matomo.schedule=59 * * * *
- cron.import_logs_matomo.command=import_logs_matomo
cron:
container_name: ${COMPOSE_PROJECT_NAME}_cron # don't allow to scale the container
image: ghcr.io/wikiteq/cron:20250709-2da693f
restart: unless-stopped
environment:
- COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME}
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./_logs/cron:/var/log/cronNotes:
- The cron sidecar watches container labels and runs the specified command inside the labeled container according to the schedule. Ensure Docker socket is mounted read-only.
tmpfs: /var/lib/varnish:execis recommended for performance.
- 80: Varnish HTTP listener
- 9131: Prometheus metrics (
prometheus_varnish_exporter) – enabled whenVARNISH_SECRETexists
/etc/varnish(ro): Provide yourdefault.vcland optionalsecret/data: Used whenVARNISH_STORAGE_KIND=file(default file at/data/cache.bin)/var/log/varnish: IfVARNISH_LOG_DIRis set, varnishncsa writesaccess_loghere/var/www/html: Matomo installation directory (needed forimport_logs_matomohelper)
VARNISH_CONFIG(default:/etc/varnish/default.vcl): Path to main VCLVARNISH_SECRET(default:/etc/varnish/secret): When present, admin interface is enabled on127.0.0.1:6082and metrics exporter startsVARNISH_SIZE(default:100M): Cache size, e.g.5GVARNISH_STORAGE_KIND(default:malloc):mallocorfileVARNISH_STORAGE_FILE(default:/data/cache.bin): Cache file path used whenfilestorage is selectedVARNISH_STORAGE_SPECIFICATION(optional): Full-svalue to override auto-generated one, e.g.file,/data/cache.bin,5GVARNISH_LOG_DIR(optional): Directory for access logs; enablesvarnishncsabackground process writing${VARNISH_LOG_DIR}/access_logVARNISH_LOG_FORMAT(optional): One ofX-Forwarded-For,X-Real-IP, or a customvarnishncsa -Fformat string. Defaults to a combined-like formatMATOMO_USER(default:admin): Matomo user for the log importerMATOMO_PASSWORD(required for Matomo import): Can also be read from/run/secrets/matomo_passwordMATOMO_URL(default:http://matomo): Base URL for Matomo API used by importerLOG_FILES_COMPRESS_DELAY(default:3600): Upper bound for random delay (seconds) before compressing rotated logs;0disables delayLOG_FILES_REMOVE_OLDER_THAN_DAYS(default:10): Remove archived gzip files (access_log_*.gz) older than N days; set tofalseto disable
Storage notes:
- When
VARNISH_STORAGE_KIND=file, the entrypoint willchownVARNISH_STORAGE_FILEto uservcacheand usefile,<path>,<size>. - Otherwise it uses
<kind>,<size>.
Logging notes:
- If
VARNISH_LOG_DIRis set,varnishncsaruns in the background and writes to${VARNISH_LOG_DIR}/access_log. VARNISH_LOG_FORMAT=X-Forwarded-FororX-Real-IPare conveniences for common proxy headers.
Metrics notes:
- Metrics are exposed on port 9131 via
prometheus_varnish_exporterwhenVARNISH_SECRETexists (admin interface enabled). Scrapehttp://<container>:9131/metrics.
At container start:
- Computes storage specification from
VARNISH_STORAGE_KIND,VARNISH_STORAGE_FILE, andVARNISH_SIZEunlessVARNISH_STORAGE_SPECIFICATIONis provided - Optionally starts
varnishncsaifVARNISH_LOG_DIRis set - Enables admin and starts
prometheus_varnish_exporterifVARNISH_SECRETexists - Starts
varnishdin the foreground (-F) for container health
varnish_reload_vcl: Hot-loads the VCL specified byVARNISH_CONFIGand activates it viavarnishadmvarnishncsa_sighup: SendsSIGHUPtovarnishncsato reopen the log file after rotationimport_logs_matomo: Rotates currentaccess_logif older than a day, imports it, lock-processes older backlog, renames imported files to*_imported, then triggers background compression and cleanupcompress_old_logs [delay]: Compresses*_importedfiles to.gz(removes_importedsuffix) using a lock to prevent concurrent runs
Examples:
docker exec -it <varnish_container> varnish_reload_vcl
docker exec -it <varnish_container> varnishncsa_sighup
docker exec -it <varnish_container> import_logs_matomoMatomo import details:
- Uses Matomo's
misc/log-analytics/import_logs.pyfrom the mounted Matomo directory (/var/www/html). - Imports into site ID
1with--recorders=4by default. - If the current
access_logis missing, the importer sendsSIGHUPtovarnishncsato start/reopen logging and exits. - If the first entry in
access_logis older than one day, it is rotated toaccess_log_YYYYMMDDandvarnishncsais signaled (SIGHUP) to reopen the file. - After a successful import, processed rotated files are renamed to
*_importedand a background job compresses all*_importedfiles to.gz(dropping the_importedsuffix). - Older not-yet-imported files matching
access_log_YYYYMMDDare discovered and imported under a lock to prevent concurrent runs; on success they are renamed to*_imported. - Archived gzip files may be cleaned up based on
LOG_FILES_REMOVE_OLDER_THAN_DAYS(positive integer), or retained if set tofalse. - Lock files used:
.import_lockfor backlog importing and.compress_lockfor compression to avoid concurrent executions.
docker build -t pastakhov/varnish:7.0 .Provide your VCL and (optionally) secret in a bind mount to /etc/varnish.