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

Before you begin

Prerequisites

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.

Tools

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

1. Configure Docker 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

HTTPD_SERVER=apache-2.4
#HTTPD_SERVER=nginx-stable

Make sure that PHP version 7.4 is uncommented.

#PHP_SERVER=7.3
PHP_SERVER=7.4

Enable Maria DB version 10.4

MYSQL_SERVER=mariadb-10.4
#MYSQL_SERVER=mariadb-10.5

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:

PHP_MODULES_DISABLE=oci8,PDO_OCI,pdo_sqlsrv,sqlsrv,rdkafka,swoole,xdebug,psr

Save changes.

2. Configure 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: https://www.docker.elastic.co/
ELK_SERVER=7.9.3
# Elastic Search settings
HOST_PORT_ELK_ELASTIC=9200
# Logstash settings
HOST_PORT_ELK_LOGSTASH=9600
# Kibana settings
HOST_PORT_ELK_KIBANA=5601

Add settings for Varnish Cache & HAProxy.

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

# Varnish version to choose
#VARNISH_SERVER=4
#VARNISH_SERVER=5
VARNISH_SERVER=6

# Varnish settings
VARNISH_CONFIG=/etc/varnish/default.vcl
VARNICS_CACHE_SIZE=128m
VARNISH_PARAMS=-p default_ttl=3600 -p default_grace=3600
HOST_PORT_VARNISH=6081

# HAProxy settings
HOST_PORT_HAPROXY=8080
HOST_PORT_HAPROXY_SSL=8443

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

# RabbitMQ version to choose
#RABBIT_SERVER=3.6
#RABBIT_SERVER=3.6-management
#RABBIT_SERVER=3.7
#RABBIT_SERVER=3.7-management
#RABBIT_SERVER=latest
#RABBIT_SERVER=management
RABBIT_SERVER=3.8

RABBIT_DEFAULT_VHOST=my_vhost
RABBIT_DEFAULT_USER=guest
RABBIT_DEFAULT_PASS=guest

HOST_PORT_RABBIT=5672
HOST_PORT_RABBIT_MGMT=15672

3. Create DNS records in Windows & Ubuntu

In the terminal, edit the Ubuntu hosts file:

sudo vi /etc/hosts

Add this line and save changes:

127.0.0.1 my-magento24.loc

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

c:\Windows\System32\Drivers\etc\hosts

Add the same line as in Ubuntu and save changes: 

127.0.0.1 my-magento24.loc

4. 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/
./shell.sh

Create a new Virtual Host directory

mkdir my-magento24

5. 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 https://github.com/magento/magento2
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 127.0.0.1 -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: 

Tip:

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 '172.16.238.240:9200'

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=127.0.0.1 \
--db-name=my_magento24 \
--db-user=root \
--admin-firstname=admin \
--admin-lastname=admin \
--admin-email=admin@admin.com \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/New_York \
--use-rewrites=1 \
--backend-frontname=admin  \
--elasticsearch-host=172.16.238.240

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. If you have any difficulties, we are happy to help you with our Magento Development services. 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