Varnish Docker Image for Magento 2

Updated 28 February 2018

In this blog. we will be looking into documentation for Varnish docker image, and its applicability with Magento 2. 

This Docker Image can be used to run a Docker container with Varnish Cache as one of its service. Varnish Configuration File present at location /etc/varnish/default.vcl is fully compatible with Magento 2 latest version on OS ubuntu 16.04.

To begin with this image, docker must be installed and running on your server. To pull this Image, run the following command on your docker installed server:

 

 

Now when Image has been pulled, you can check its presence by running command:

 

 

Now, you can run docker container from this Image to start a Varnish Cache server. There must be a Magento 2 running on the (same or different) server. Varnish will act as a HTTP accelerator for your Magento 2 server.

Inside the docker container, Varnish cache will run on port 6081. You can map it whichever port on the host. So let’s launch the container from the image as:

docker run -tidp host_port:container_port -e BACKEND_PORT=magento2_server_port -e BACKEND_HOST=magento2_server_IP webkul/varnish_for_magento2

Let us assume our Magento 2 server is running on port 80, server IP is 192.168.X.X. Also, the Varnish server is on same server. This port and IP address will act as Varnish backend port and backend host respectively. As Varnish is on same server, we will have to assign a port other than 80.

 

 

You can check the running container by command:

 

 

Hit 192.168.X.X:8080 on browser to check if it is working or not.

Also, for Varnish Cache server to run on port 80 presuming that your webserver is running on some other port:

 

 

Your server will be running on 192.168.X.X .

If Varnish and Magento 2 server is running on different servers, we can run both servers on port 80. Let us say Magento 2 server is on 192.168.X.X and Varnish Cache server is on 192.168.X.Y. Run this command:

 

 

Please take a note that if Varnish and Webserver are on separate server then your domain should be mapped with IP address of Varnish Server.

Hit 192.168.X.Y on browser to check.

We have setup our Varnish Cache server and now we have to configure Magento 2 to use Varnish Cache instead of default built-in cache. Log In to Magento 2 Admin and and go to Stores -> Configuration -> Advanced -> System -> Full Page Cache. Modify Caching application and set Varnish configuration. Then refresh all the Cache.

Finally, to check if Varnish server is caching or not, set your Magento 2 in developer mode. Curl the URL with Varnish port. As if Varnish cache is running on port 80 and our IP is 192.168.X.X, then:

 

 

or in case of port 8080:

 

 

If you see headers like X-Magento-Cache-Debug, X-Varnish-Cache getting values as HIT and header Age is increasing with each Curl request, then you Varnish Cache is working properly.

Visit https://hub.docker.com/r/webkul/varnish_for_magento2/ for Docker Image Repository.

Category(s) Uncategorized
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project






    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home