Installing Magento locally with Docker

It you are looking to install magento in your local machine I would recommend using Docker instead of the usual xampp-type solution where you need to configure all the services required by your own.

On top of that, since magento 2.4.0 it is mandatory to have elasticsearch running to install magento. This means yet another setup task to complete before you can even start to thing on magento installation process.

Moreover if you require to have different version of magento running you might also need different setup environments… This is where Docker makes you life easier 🙂

Docker also guarantees that you are running the same environment as your all team members, thus avoid configuration inconsistencies that can make you waste time (and money).

There are several free docker images for magento online but I have to say that the best I came across is the one from Mark Hurst. With just a few commands you can have your magento running in a bit!

Here is a step-by-step guide to help you do it:

#1 Install Docker Desktop

Docker is an open platform to develop, ship and run application. In a nutshell Docker allows you to manage different environment images and create containers from them where you can run your applications in a loosely isolate environment. This allow you to run many containers simultaneously on the same host (which is ideal if you want to have multiple magento installs).

Ok, first step would be to install the Docker Desktop. You can download it from free from the Docker website. Depending on your OS you will find the right file/instruction to install it.

The Docker desktop will provide you with all you need to start creating your containers. However, if you use the Mark Hurst docker images you don’t even need to dig into Docker (at least until you need to do some fancy stuff or change a few configuration aspects)

#2 Download Docker Composer template

Now, go into the command line of your machine and create a folder where you want to have your website. If you are running Windows I would suggest you to use the WSL and create a folder inside the home directory. Run:

curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash

This will pull the template files created by Mark to help you manage your magento site abstracting the fact that the site will actually run inside Docker containers (you will have in fact a copy of the site files outside the containers that you can edit and will be automatically synced with the files inside the containers).

Some useful information about what just happened and what you have now:

  • docker-composer.yml hold your containers configuration. This is where all needed containers for magento are listed and configured (you don’t need to change this unless you want to change php version, service ports or some other service configuration)
  • src is the folder where you will see you magento files. All changes made here will by synced with the container where the actual site lives and run. This allows you to edit your site files from outside the container using the IDE you like the most (I personally use PHPStorm).
  • bin is the folder where Mark has a bunch of useful scripts to help you manage magento from outside the container and to map magento commands to the container magento install (very cleverly done).
    This means that if you run a command like bin/magento cache:flush from the folder where the you installed the template files you are actually running that same command inside the container (without the need to actually go into the container bash).

#3 Download magento

Run bin/download 2.4.2 and this will download magento version 2.4.2 to inside your container.

#4 Install magento

Run bin/setup magento.test and this will install the downloaded magento version inside your container and setup all the development environment you need (included the domain magento.test and the certificate for it).

Note that you also need to DNS host entry on your local machine for this domain.
On a linux based machine you can run

echo "127.0.0.1 ::1 magento.test" | sudo tee -a /etc/hosts

On Windows create a new entry in file SystemRoot > system32 > drivers > etc > hosts

#5 Optional: Install demo data

To install magento sample date simply run the following commands (again all these command listed here are run outside the containers, from that initial folder you created to host your website locally).

bin/magento sampledata:deploy
bin/magento setup:upgrade

#6 Navigate your local website

Go to your favorite browser and open the magento website you just installed 🙂 (if you used the command above type https://magento.test on your browser address bar).

Note that you can start/stop/restart your website (containers) using the bash script that Mark wrote, namely bin/start, bin/stop and bin/restart. Whenever you are not using the website you can just stop it 🙂

Leave a Reply

Your email address will not be published.