commit 66d863906efab4c0c2bb84def6079506aaad240a Author: dpedu Date: Sat Sep 5 20:37:34 2015 -0700 Initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..748564b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM ubuntu:trusty + +ADD start /start + +RUN chmod +x /start ;\ + locale-gen en_US en_US.UTF-8 ;\ + apt-get update ;\ + apt-get install -y apt-transport-https curl supervisor inotify-tools ;\ + curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add - ;\ + echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list ;\ + apt-get update ;\ + DEBIAN_FRONTEND=noninteractive apt-get install -y varnish ;\ + rm /etc/varnish/secret + +ADD varnish.conf /etc/supervisor/conf.d/varnish.conf +ADD supervisor.conf /etc/supervisor/conf.d/supervisor.conf + +WORKDIR /etc/varnish +CMD ["/start"] + +EXPOSE 80 6082 diff --git a/README.md b/README.md new file mode 100644 index 0000000..08b7f3f --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +docker-varnish +============== + +Container for running [Varnish Cache](https://www.varnish-cache.org/). Automatically checks vcl validity before running varnish and reports compile errors in stdout. + +*Usage:* + +* Build: `docker build -t varnish .` +* Run: `docker run -d -v /my/test.vcl:/etc/varnish/default.vcl -p 80:80 varnish` + +*Advanced* + +* Set memory size: `docker run` with `-e MEMSIZE=128m` +* Use pre-set secret: `-e SECRET=7d40f8f1-9107-4cce-a2b6-f5caf6fc7b9d` +* Expose control terminal: `-p 1234:6082` + +*Extras* + +* test.vcl - example default.vcl file diff --git a/start b/start new file mode 100644 index 0000000..2bf2577 --- /dev/null +++ b/start @@ -0,0 +1,20 @@ +#!/bin/bash + +# Cleanly kill supervisor when container is stopped +trap 'kill $(jobs -p)' EXIT + +if [ -z "$MEMSIZE" ]; then + export MEMSIZE=64M +fi + +if [ ! -f /etc/varnish/secret ]; then + if [ -z "$SECRET" ]; then + SECRET=`cat /proc/sys/kernel/random/uuid` + fi + echo $SECRET > /etc/varnish/secret +fi + +echo "Secret is `cat /etc/varnish/secret`" +varnishd -C -f /etc/varnish/default.vcl > /dev/null || exit 1 + +supervisord diff --git a/supervisor.conf b/supervisor.conf new file mode 100644 index 0000000..54296b8 --- /dev/null +++ b/supervisor.conf @@ -0,0 +1,2 @@ +[supervisord] +nodaemon=true diff --git a/test.vcl b/test.vcl new file mode 100644 index 0000000..9239510 --- /dev/null +++ b/test.vcl @@ -0,0 +1,34 @@ +vcl 4.0; +import std; +import directors; + +backend default { + .host = "dpedu.io"; + .port = "80"; +} + +# Setup function +sub vcl_init { + new default_balancer = directors.round_robin(); + default_balancer.add_backend(default); +} + +sub vcl_recv { + # If X-Real-IP is unset, assume we are talking directly to the client + if (!req.http.X-Real-IP) { + set req.http.X-Real-IP = client.ip; + } + + # Set backend to handle request + set req.backend_hint = default_balancer.backend(); + set req.http.host = "dpedu.io"; + + # Disabled cookies for static assets + if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") { + unset req.http.Cookie; + # return cache for object + return (pass); + } + + return (pass); +} diff --git a/varnish.conf b/varnish.conf new file mode 100644 index 0000000..55170d9 --- /dev/null +++ b/varnish.conf @@ -0,0 +1,5 @@ +[program:varnishd] +command=/usr/sbin/varnishd -f /etc/varnish/default.vcl -a 0.0.0.0:80 -T 0.0.0.0:6082 -S /etc/varnish/secret -s malloc,%(ENV_MEMSIZE)s -F +autostart=true +autorestart=true +redirect_stderr=true