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:
1 |
docker pull webkul/varnish_for_magento2 |
Now when Image has been pulled, you can check its presence by running command:
1 |
docker images |
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.
1 |
docker run -tidp 8080:6081 -e BACKEND_PORT=80 -e BACKEND_HOST=192.168.X.X webkul/varnish_for_magento2 |
You can check the running container by command:
1 |
docker ps |
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:
1 |
docker run -tidp 80:6081 -e BACKEND_PORT=8080 -e BACKEND_HOST=192.168.X.X webkul/varnish_for_magento2 |
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:
1 |
docker run -tidp 80:6081 -e BACKEND_PORT=80 -e BACKEND_HOST=192.168.X.X webkul/varnish_for_magento2 |
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:
1 |
curl -I 192.168.X.X |
or in case of port 8080:
1 |
curl -I 192.168.X.X:6081 |
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.