How to Install Magento 2.4 + ElasticSearch + Varnish with Docker

The following guide is our second tutorial dedicated to Magento installation with Docker. Whether you are just starting with Magento or a seasoned developer, you should be able to install Magento 2.4 in less than 30 minutes after reading the next simple eight steps.

How to Install Magento 2.4 + ElasticSearch + Varnish with Docker

1. Before you begin


This tutorial requires you to have the Devilbox installed along with the Docker and Docker-compose in Ubuntu on Windows (WSL). Please follow our guide to install Magento on Docker with Devilbox before you proceed to install Magento 2.4 using the next steps.


In this guide, we will be using the free Visual Studio Code editor with VSCode .env syntax highlighting extension and new Windows Terminal

2. Docker Configuration for Magento 2.4

First, it’s important to make sure that our configuration file meets all Magento 2.4 system requirements. The following configuration will be set in the environment file: 

  • Apache v2.4
  • PHP v7.4
  • MariaDB v10.4
  • ElasticSearch v7.9.3 (check the list of all available versions)
  • Varnish 6.x
  • RabbitMQ 3.8

Open Devilbox environment file:

cd ~/devilbox/
vi .env

Comment out the Nginx web server line and uncomment Apache


Make sure that PHP version 7.4 is uncommented.


Enable Maria DB version 10.4


We recommend disabling the “xdebug” PHP module to speed up your webserver and “psr” PHP module to avoid errors during Magento Installation.

Find and replace the “PHP_MODULES_DISABLE” value:


Save changes.

3. Configuring ElasticSearch, Varnish, and RabbitMQ for Magento 2.4

According to Magento devdocs, ElasticSearch is required for Magento 2.4.x installation, and the other two services are recommended. However, we will install them all in this tutorial.

In order to add these services to the default Devilbox stack, we need to copy the Docker Compose Override configurations.

cd ~/devilbox/
cp compose/docker-compose.override.yml-all docker-compose.override.yml

Please note:

Optionally, you can remove extra services (like Blackfire, MailHog, Ngrok, etc.) from the new docker-compose override file (docker-compose.override.yml). However, this step can be skipped.

Let’s add new variables at the end of the Devilbox environment file:

cd ~/devilbox/
vi .env

Add settings for ELK stack (ElasticSearch, Logstash, and Kibana): 

Copy & paste these settings at the bottom of .env file.

# ELK stack general
# See here for all versions:
# Elastic Search settings
# Logstash settings
# Kibana settings

Add settings for Varnish Cache & HAProxy.

Copy & paste these settings at the bottom of .env file.

# Varnish version to choose

# Varnish settings
VARNISH_PARAMS=-p default_ttl=3600 -p default_grace=3600

# HAProxy settings

Add settings for RabbitMQ.
Copy & paste these settings at the bottom of the .env file.

# RabbitMQ version to choose



4. Create DNS records in Windows & Ubuntu

In the terminal, edit the Ubuntu hosts file:

sudo vi /etc/hosts

Add this line and save changes: my-magento24.loc

In Windows, open Notepad as administrator and edit the following file:


Add the same line as in Ubuntu and save changes: my-magento24.loc

5. Start the Docker & create a new Virtual Host directory

Start our docker containers in Ubuntu:

sudo service docker start
docker-compose up

Enter the PHP Docker container

Open a new tab in the terminal and enter the following:

cd ~/devilbox/

Create a new Virtual Host directory

mkdir my-magento24

6. Install Magento 2.4 with Docker

Feel free to use the official Devilbox documentation,  as your additional resource. We will add a couple of changes to the installation process.

Install Magento 2.4 using composer

cd my-magento24
git clone
cd magento2
git checkout 2.4.2
composer install
composer update

Symlink webroot

cd /shared/httpd/my-magento24/
ln -s magento2/ htdocs

Add MySQL Database

mysql -u root -h -p -e 'CREATE DATABASE my_magento24;'

Set file permissions

cd /shared/httpd/my-magento24/htdocs
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chmod u+x bin/magento

Testing ElasticSearch: 


If needed, the default ElasticSearch IP address can be changed in your docker-compose.override.yml file.

To test if your ElasticSearch is working, just open the following URL in the browser: http://localhost:9200 or run this command in the terminal:

curl -XGET ''

You should receive a similar response to this:

  "name" : "elastic",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "mQ7yhl5NT1O8A3tcZGDjvg",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "oss",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  "tagline" : "You Know, for Search"

Install Magento 2.4 using command line

Let’s use Magento 2 setup install command from the official online guide, with tiny changes :-):

bin/magento setup:install \
--base-url=http://my-magento24.loc \
--db-host= \
--db-name=my_magento24 \
--db-user=root \
--admin-firstname=admin \
--admin-lastname=admin \ \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/New_York \
--use-rewrites=1 \
--backend-frontname=admin  \

Here’s how your Magento 2.4 frontend will look after a successful installation:

Magento 2.4 frontend

We hope you found this article useful. Please comment and share. Thanks for reading!

About The Author: Oleh Lutsiv

Founder of Plumrocket Inc, obsessed with web performance and crafting innovative solutions in the eCommerce industry. When not in programming and managing, he enjoys fishing, winter, and family time. View more posts