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%%
VARNISH_BACKEND_HOST and VARNISH_BACKEND_PORTYou just need to know where your backend (the server that Varnish will accelerate) is:
# we define VARNISH_BACKEND_HOST/VARNISH_BACKEND_PORT
# our workdir has to be mounted as tmpfs to avoid disk I/O,
# and we'll use port 8080 to talk to our container (internally listening on 80)
$ docker run \
-e VARNISH_BACKEND_HOST=example.com -e VARNISH_BACKEND_PORT=80 \
--tmpfs /var/lib/varnish/varnishd:exec \
-p 8080:80 \
%%IMAGE%%
From there, you can visit localhost:8080 in your browser and see the example.com homepage.
If you already have a VCL file, you can directly mount it as /etc/varnish/default.vcl:
# we need the configuration file at /etc/varnish/default.vcl,
# our workdir has to be mounted as tmpfs to avoid disk I/O,
# and we'll use port 8080 to talk to our container (internally listening on 80)
$ docker run \
-v /path/to/default.vcl:/etc/varnish/default.vcl:ro \
--tmpfs /var/lib/varnish/varnishd:exec \
-p 8080:80 \
%%IMAGE%%
Alternatively, a simple Dockerfile can be used to generate a new image that includes the necessary default.vcl:
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/varnishd:exec -p 8080:80 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/varnishd:exec -p 8080:80 -e VARNISH_SIZE=2G %%IMAGE%%
Varnish will listen to HTTP traffic on port 80, and this can be overridden by setting the environment variable VARNISH_HTTP_PORT. Similarly, the variable VARNISH_PROXY_PORT (defaulting to 8443) dictate the listening port for the PROXY protocol used notably to interact with hitch (which, coincidentally, uses 8443 as a default too!).
# instruct varnish to listening to port 7777 instead of 80
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -p 8080:7777 -e VARNISH_HTTP_PORT=7777 %%IMAGE%%
Additionally, you can add arguments to docker run after %%IMAGE%%, if the first argument starts with a -, the whole list will be appendend to the default command:
# extend the default keep period
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -p 8080:80 -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 -F -a :8080 -b 127.0.0.1:8181 -t 600 -p feature=+http2
As mentioned above, you can use vmod_dynamic for backend resolution. The varnish-modules collection is also included in the image. All the documentation regarding usage and syntax can be found in the src/ directory of the repository.
On top of this, images include install-vmod, a helper script to quickly download, compile and install vmods while creating your own images. Note that images set the ENV variable VMOD_DEPS to ease the task further.
FROM %%IMAGE%%:7.1
# set the user to root, and install build dependencies
USER root
RUN set -e; \
apt-get update; \
apt-get -y install $VMOD_DEPS /pkgs/*.deb; \
\
# install one, possibly multiple vmods
install-vmod https://github.com/varnish/varnish-modules/releases/download/0.20.0/varnish-modules-0.20.0.tar.gz; \
\
# clean up and set the user back to varnish
apt-get -y purge --auto-remove $VMOD_DEPS varnish-dev; \
rm -rf /var/lib/apt/lists/*
USER varnish
FROM %%IMAGE%%:7.1-alpine
# install build dependencies
USER root
RUN set -e; \
apk add --no-cache $VMOD_DEPS; \
\
# install one, possibly multiple vmods
install-vmod https://github.com/varnish/varnish-modules/releases/download/0.20.0/varnish-modules-0.20.0.tar.gz; \
\
# clean up
apk del --no-network $VMOD_DEPS
USER varnish