Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.
%%LOGO%%
Create a default.vcl file:
vcl 4.0;
backend default {
.host = "www.nytimes.com:80";
}
Then run:
# we need both a configuration file at /etc/varnish/default.vcl
# and our workdir to be mounted as tmpfs to avoid disk I/O
$ docker run -v /path/to/default.vcl:/etc/varnish/default.vcl:ro --tmpfs /var/lib/varnish:exec %%IMAGE%%
Alternatively, a simple Dockerfile can be used to generate a new image that includes the necessary default.vcl (which is a much cleaner solution than the bind mount above):
FROM %%IMAGE%%
COPY default.vcl /etc/varnish/
Place this file in the same directory as your default.vcl, run docker build -t my-varnish ., then start your container:
$ docker --tmpfs /var/lib/varnish:exec my-varnish
The images all ship with varnishreload which allows you to easily update the running configuration without restarting the container (and therefore losing your cache). At its most basic, you just need this:
# update the default.vcl in your container
docker cp new_default.vcl running_container:/etc/varnish/default.vcl
# run varnishreload
docker exec running_container varnishreload
Note that varnishreload also supports reloading other files (it doesn't have to be default.vcl), labels (l), and garbage collection of old labeles (-m) among others. To know more, run
docker run varnish varnishreload -h
By default, the containers will use a cache size of 100MB, which is usually a bit too small, but you can quickly set it through the VARNISH_SIZE environment variable:
$ docker run --tmpfs /var/lib/varnish:exec -e VARNISH_SIZE=2G %%IMAGE%%
Additionally, you can add arguments to docker run affter %%IMAGE%%, if the first one starts with a -, they will be appendend to the default command:
# extend the default keep period
$ docker run --tmpfs /var/lib/varnish:exec -e VARNISH_SIZE=2G %%IMAGE%% -p default_keep=300
If your first argument after %%IMAGE%% doesn't start with -, it will be interpreted as a command to override the default one:
# show the command-line options
$ docker run %%IMAGE%% varnishd -?
# list parameters usable with -p
$ docker run %%IMAGE%% varnishd -x parameter
# run the server with your own parameters (don't forget -F to not daemonize)
$ docker run %%IMAGE%% varnishd -a :8080 -b 127.0.0.1:8181 -t 600 -p feature=+http2
+$ docker run --name my-running-varnish --tmpfs /var/lib/varnish:exec -d -p 8080:80 my-varnish
Then you can hit http://localhost:8080 or http://host-ip:8080 in your browser.