We have already shown how to install a fresh Magento with Docker and how to turn that shop into a multi-store. In practice, though, you often need to get an existing shop running. It is not as hard as it may seem at first glance, but a few challenges can still come up during the installation. In this article, I share how I handled it on our projects.
While a fresh installation takes a single command and a few minutes, we will split the installation of an existing shop into several steps. Once again, I took inspiration from Mark Shust’s repository, which also describes the whole process. It looks simple, but I had to put together my own procedure.
So first we create a directory and download the Docker Compose template into it with the command
curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash
Changing the configuration in compose.yaml
Before we start the containers, we make changes to the files
- compose.yaml
- compose.dev.yaml
- compose.healthcheck.yaml
- bin/setup-install
You may need to change some of these files, all of them, or none at all. The latter is the case if you don’t want to change the settings of any of the services.
At the time of writing, the current Magento version is 2.4.7, and the service settings match it: PHP 8.3, MariaDB 10.6, OpenSearch 2.12 and so on. I needed to get Magento 2.4.5 running, which we had installed on one of our projects. That meant PHP 7.4, MariaDB 10.4 and, instead of OpenSearch, we were still running Elasticsearch 7.17.
You can find the required service versions for each Magento version at https://experienceleague.adobe.com/en/docs/commerce-operations/installation-guide/system-requirements
So in compose.yaml I changed the image for the following services:
- phpfpm: markoshust/magento-php:7.4-fpm
- db: mariadb:10.4
- elasticsearch: markoshust/magento-elasticsearch:7.17-1
I commented out the entire opensearch definition and uncommented the elasticsearch definition.
I didn’t change anything in compose.dev.yaml for now. But after a successful installation, you can uncomment some volumes there (e.g. the src/var/log directory) to keep them synced between the host and the container.
If you switched from opensearch to elasticsearch like I did, you will also need to change compose.healthcheck.yaml. Again, comment out everything related to opensearch and uncomment elasticsearch. Don’t forget the phpfpm section either, which should look something like this:
phpfpm:
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
# opensearch:
# condition: service_healthy
rabbitmq:
condition: service_healthy
elasticsearch:
condition: service_healthy
If you switched to elasticsearch, you will also need to edit bin/setup-install and remove the parameters
- –opensearch-host
- –opensearch-port
and set the value of –search-engine to, for example, elasticsearch7, depending on your Elasticsearch version.
Installing Magento
Done. Now we can download a fresh Magento. From the project root directory (where your compose.yaml is), run
bin/download community 2.4.5
where community 2.4.5 is the Magento version we are downloading.
Next, the command
bin/setup magento.test
runs the actual Magento installation on the local domain magento.test. It is essentially the same script that ran when we installed a fresh Magento. Once it finishes, you can open your browser and you should see a fresh Magento in that version.
Importing existing files and data
Now we need to bring the files and the database from the existing project into our new project. We start by copying the existing modules and themes into the directories
- src/app/code
- src/app/design/frontend
- and, if needed, src/app/design/adminhtml
These directories are synced. What is not synced with the container, however, is the content of the src/pub/media folder, which holds the images. So if your project has product images, for example, copy them into src/pub/media and then copy the contents of this directory to the container with
bin/copytocontainer pub/media
The original project probably includes some modules installed via Composer. So update the “require” section in src/composer.json. If any module requires authentication, add the keys to src/auth.json. Keep in mind, though, that this file is not synced by default either, unless you changed compose.dev.yaml.
Now you can run
bin/composer update
which installs all the required modules into the vendor directory (inside the container!!!).
You can also enable these modules with
bin/magento module:enable Vendor_Module [–all]
but don’t run the database upgrade yet. First we need to import the database from the original project into this one. Before the import itself, I recommend backing up the current database with
bin/mysqldump > new_db.sql
Now you can start importing the original database. Make sure you do it via bin/clinotty as root, otherwise the import may fail with an error saying you don’t have sufficient privileges. The full command is
bin/clinotty mysql -hdb -uroot -pmagento magento < existing_db.sql
I took the values for the host, root password and database name from env/db.env. If you haven’t changed anything there, they will be exactly the same as in my command.
Don’t forget to make all the necessary changes in the imported database. At the very least, change the base URL to magento.test, because the original project was probably running on a different URL.
Now you can run the database scripts themselves with
bin/magento setup:upgrade
If you get the error “Could not validate a connection to Elasticsearch. No alive nodes found in your cluster”, it is because the original database probably had the elasticsearch server hostname set to “localhost”. In the container, however, this service is called “elasticsearch”, or “opensearch” in the case of OpenSearch.
So go to the database and change the value of
catalog/search/elasticsearch7_server_hostname to elasticsearch.
As a final step, I would delete the contents of the src/vendor directory on the host and copy over the contents of vendor from the container
bin/copyfromcontainer vendor
I hope this guide helped you get your existing Magento project running on Docker. If not, go through the whole procedure once more to check whether you missed any of the settings I recommended. Another good source for troubleshooting is the “Issues” section of this repository on GitHub.