October 19, 2021
3 min read time

Two-Minute Tech Tuesdays - Varnish Docker Image

This week's 2-Minute Tech Tuesday features the official Docker image for Varnish. We'll explain what Docker is, how it works, and what commands you need to use to run a Varnish container on Docker.


What it is

Docker is an open platform for developing, shipping, and running applications. It's pretty much containerization technology that leverages Linux containers behind the scenes.

One of the benefits of using Docker is the fact that it's extremely lightweight and fast to deploy. It allows for portable application stacks that are built for automation and orchestration.

By doing this, multiple workloads on a single server are supported. As a matter of fact, there's an official Varnish Cache image up on the Docker Hub which allows you to run varnish quite easily.


How it works

When you call Docker to run a Varnish container, the Varnish image is pulled off the Docker Hub, a container is created and the necessary customizations are applied. In the end, that container is run on a server.

As a minimum, you can run "docker run varnish" to get started, but as you might expect, there's a lot more to it. In this example, we're using the "-v" parameter to define a bind mount, where the default.vcl file in the present working directory of my host system is mounted into /etc/varnish/default.vcl of the container with read-only permissions.

The --tmpfs parameter allows us to load "/var/lib/varnish" into tmpfs, which is a memory-based file system.

We named this container my-varnish-container, and we perform port forwarding through the -p parameter and actually forward port 80 from the container onto port 80 of our hosts system.

Environment variables can be set with the minus -e parameter, which allows us to set the size of the cache to two gigabytes. The specific version of Varnish that we're running is represented by the "stable" tag and this refers to Varnish Cache 6.0 LTS.


We can orchestrate the entire process and bootstrap multiple containers using the docker compose command. Docker Compose depends on the docker-compose.yaml file, which contains the container definition. This yaml file defines two containers:

  1. On the one hand, we have Varnish with all of its parameters.
  2. On the other hand, we have an "httpd" container which is Apache's httpd web server, that Varnish proxies to. Run "docker compose up" to bootstrap the two containers.

Stay tuned for next's week episode where we'll cover VCL syntax!