A Docker image running Varnish HTTP Cache, based on Alpine Linux.
This README only aims at getting you up and running with the Docker image, and documents only a very basic usage of Varnish. Consult the Varnish Documentation for more details.
See the image on Docker Hub.
The image is based on alpine-3 and provides Varnish Cache 6.5.1, which has EOL date 2022-09-15.
This image is created for development purposes (e.g for testing how load balanced backends behaves), and does not support SSL/TLS termination. See How to configure Varnish with SSL termination using the Hitch TLS proxy for details.
If you require TLS termination, use the official Varnish Docker image instead.
- Varnish listens on port 80.
- The default.vcl assumes a backend listening on localhost:8080.
- The default.vcl location us
/etc/varnish/default.vcl
The images uses the following environmental variables. See the varnishd documentation for more details.
VCL_DIR '/etc/varnish'
VCL_FILE 'default.vcl'
VARNISH_CACHE_SIZE 64m
VARNISH_ADDRESS '0.0.0.0'
VARNISH_PORT 80
A dummy backend JSON REST API is provided in order to ease testing:
- Install JSON Server or any other equivalent dummy backend tool.
- Serve the file using:
# The default JSON server port is 3000, but the default VCL-file assumes a backend running on port 8080.
json-server --watch backend/db.json --port 8080
Verify that the dummy backend is running with:
curl localhost:8080/posts
# Should return:
[
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
]
The image can be used directly or as a base for you own image.
Out of the box the image assumes a backend on localhost:8080, which implies that the docker image has to be on the same network as the host. That works well for testing, but be aware of port collisions.
docker run -it --rm --name myvarnish --network host njmittet/alpine-varnish
Verify that Varnish works by requesting on port 80:
curl localhost/posts
# Should return:
[
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
]
Run with a different VCL-file using bind mounts:
# Replace the default.vcl file.
docker run -it --rm --name myvarnish --network host -v $(pwd)/alt.vcl:/etc/varnish/default.vcl:ro njmittet/alpine-varnish
# Use a VCL file with a different name.
docker run -it --rm --name myvarnish --network host -v $(pwd)/alt.vcl:/etc/varnish/alt.vcl:ro -e VCL_FILE='alt.vcl' njmittet/alpine-varnish
To change the port Varnish listens on, both the Varnish port and the port exposed by Docker must be changed:
docker run -it --rm --name myvarnish --network host -e VARNISH_PORT=9000 --expose=9000 njmittet/alpine-varnish
Create an image containing your own VCL-file by creating a Dockerfile with the following content:
FROM njmittet/alpine-varnish:latest
COPY alt.vcl $VCL_DIR/default.vcl
# Build and run the container.
docker build -t myvarnish .
docker run -it --rm --name myvarnish --network host myvarnish
See the examples for a cluster example. Consult the Varnish VCL Examples for a great list of other examples.