docker.recipes

Varnish Cache

advanced

HTTP accelerator and caching reverse proxy for high-traffic websites.

Overview

Varnish Cache is a powerful HTTP accelerator and caching reverse proxy designed specifically for high-traffic websites. Originally developed by Poul-Henning Kamp in 2006, Varnish sits between your users and your web servers, caching frequently requested content in memory to dramatically reduce response times and server load. Unlike traditional caching solutions, Varnish uses its own configuration language called VCL (Varnish Configuration Language) which compiles to C code for maximum performance, making it capable of handling thousands of requests per second with minimal latency. This deployment creates a two-tier architecture with Varnish Cache as the frontend accelerator and NGINX as the backend web server. The varnish service runs on port 80, intercepting all incoming HTTP requests and serving cached content when available. When content isn't cached or has expired, Varnish forwards requests to the backend NGINX service, which serves the actual web content from the html directory. This configuration demonstrates the classic caching proxy pattern where Varnish acts as an intelligent intermediary, learning which content to cache based on your VCL configuration. This stack is ideal for system administrators managing high-traffic websites, DevOps teams implementing caching strategies, and developers who need to understand HTTP acceleration patterns. The combination of Varnish's sophisticated caching algorithms with NGINX's reliable content serving provides a robust foundation for websites that need to handle sudden traffic spikes, reduce server costs, and improve user experience through faster page loads.

Key Features

  • VCL-based configuration system that compiles to optimized C code for maximum performance
  • Memory-based caching with intelligent cache invalidation and TTL management
  • Advanced cache purging capabilities with pattern-based and selective content removal
  • Built-in health checking for backend servers with automatic failover support
  • Real-time cache statistics and monitoring through varnishstat and varnishlog tools
  • Edge Side Includes (ESI) support for dynamic content assembly and partial page caching
  • HTTP/1.1 pipelining and connection multiplexing for improved throughput
  • Configurable cache storage backends including malloc, file-based, and persistent storage options

Common Use Cases

  • 1E-commerce websites needing to cache product pages and API responses during high-traffic sales events
  • 2News and media sites requiring fast delivery of articles and images to global audiences
  • 3API acceleration for microservices architectures where response caching reduces database load
  • 4Content delivery optimization for WordPress, Drupal, or other CMS-based websites
  • 5Development and staging environments for testing caching strategies before production deployment
  • 6Educational platforms caching course content and user dashboards to improve learning experience
  • 7Corporate websites with high traffic volumes that need cost-effective scaling without additional servers

Prerequisites

  • Minimum 512MB RAM for Varnish cache storage, with 2GB+ recommended for production workloads
  • Port 80 available on the host system for Varnish frontend traffic
  • Basic understanding of HTTP caching concepts, cache headers, and TTL management
  • Familiarity with VCL syntax for customizing caching rules and backend configurations
  • Web content prepared in the ./html directory for NGINX to serve as backend content
  • Docker and Docker Compose installed with network creation permissions

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 varnish:
3 image: varnish:stable
4 container_name: varnish
5 restart: unless-stopped
6 volumes:
7 - ./varnish/default.vcl:/etc/varnish/default.vcl:ro
8 ports:
9 - "80:80"
10 depends_on:
11 - backend
12 networks:
13 - varnish-network
14
15 backend:
16 image: nginx:alpine
17 container_name: backend
18 restart: unless-stopped
19 volumes:
20 - ./html:/usr/share/nginx/html:ro
21 networks:
22 - varnish-network
23
24networks:
25 varnish-network:
26 driver: bridge

.env Template

.env
1# Varnish cache size
2VARNISH_SIZE=256m

Usage Notes

  1. 1Docs: https://varnish-cache.org/docs/
  2. 2Create varnish/default.vcl - VCL is the configuration language
  3. 3Check cache status: varnishstat, varnishlog, varnishtop
  4. 4Purge cache: curl -X PURGE http://localhost/path
  5. 5Backend health defined in VCL with .probe directive
  6. 6Set VARNISH_SIZE based on available RAM (256m-2g typical)

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

varnish
varnish:
  image: varnish:stable
  container_name: varnish
  restart: unless-stopped
  volumes:
    - ./varnish/default.vcl:/etc/varnish/default.vcl:ro
  ports:
    - "80:80"
  depends_on:
    - backend
  networks:
    - varnish-network
backend
backend:
  image: nginx:alpine
  container_name: backend
  restart: unless-stopped
  volumes:
    - ./html:/usr/share/nginx/html:ro
  networks:
    - varnish-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 varnish:
5 image: varnish:stable
6 container_name: varnish
7 restart: unless-stopped
8 volumes:
9 - ./varnish/default.vcl:/etc/varnish/default.vcl:ro
10 ports:
11 - "80:80"
12 depends_on:
13 - backend
14 networks:
15 - varnish-network
16
17 backend:
18 image: nginx:alpine
19 container_name: backend
20 restart: unless-stopped
21 volumes:
22 - ./html:/usr/share/nginx/html:ro
23 networks:
24 - varnish-network
25
26networks:
27 varnish-network:
28 driver: bridge
29EOF
30
31# 2. Create the .env file
32cat > .env << 'EOF'
33# Varnish cache size
34VARNISH_SIZE=256m
35EOF
36
37# 3. Start the services
38docker compose up -d
39
40# 4. View logs
41docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/varnish-cache/run | bash

Troubleshooting

  • Cache not working or always missing: Check VCL configuration syntax and ensure backend is properly defined with correct host and port
  • 503 Backend fetch failed errors: Verify the backend NGINX container is running and accessible on the varnish-network
  • Varnish container fails to start: Ensure the ./varnish/default.vcl file exists and contains valid VCL syntax without compilation errors
  • Memory allocation errors: Increase available system RAM or reduce VARNISH_SIZE parameter in the container configuration
  • Cache purging not working: Verify PURGE requests are properly configured in VCL with correct ACL permissions and URL matching
  • Performance issues under load: Monitor cache hit ratio with varnishstat and tune cache storage backend settings

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space