Video Lesson: Set up Docker for an existing Magento project

Set up Docker for an existing Magento project

Learn how to set up a Docker development environment for an existing Magento project.


Lesson Content

We’ll use the existing Magento source code, database dump, and env.php file to create a development instance with Docker Magento.

Before starting, make sure you have:

  • An SQL dump of the existing Magento database

  • The existing app/etc/env.php file

  • The existing Magento source code

The Docker Magento documentation includes instructions for existing projects. Open the Docker Magento repository, select Setup, and find the Existing projects section under Manual setup.

First, create a database dump from the existing Magento project. Docker Magento includes a helper command for this:

bin/mysqldump > ~/Sites/existing/magento.sql

If the database is on another server, use the mysqldump command with the appropriate host, username, and password options:

mysqldump -h HOST -u USER -p DATABASE > magento.sql

Keep the resulting magento.sql file available because you import it after the new Docker Magento environment is running.

Next, create a directory for the new project and download the Docker Magento template:

mkdir ~/Sites/magento
cd $_
curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash

The template creates the structure for the Docker Magento environment. The existing Magento source code is not included yet, so copy the source files from the existing project into the new project’s src directory, or clone the project directly into that directory:

cp -R ~/Sites/existing/src src

The src directory should contain the root of the Magento installation, including directories such as app, bin, pub, and vendor.

Before starting the new containers, stop any containers from the existing Magento environment so they do not compete for the same host ports:

bin/stop

Start the new environment directly with Docker Compose, using only the docker-compose.yml file:

docker-compose -f docker-compose.yml up -d

Use this command instead of bin/start. The bin/start script uses an additional Docker Compose file that references directories that are not set up during this existing-project migration.

Check that the containers started successfully:

bin/status

The status output should show the containers running without port conflicts or other errors.

Now copy the complete Magento source tree into the Docker volume:

bin/copytocontainer --all

Import the database dump you created earlier. Replace the path with the location of your SQL file:

bin/mysql < ../existing/magento.sql

If the import reports permission or connection errors, run the import as the MySQL root user. The default root password in this environment is magento:

bin/clinotty mysql -hdb -uroot -pmagento magento < ../existing/magento.sql

Next, update the database connection in app/etc/env.php. Open the file and find the db configuration. The connection values for the Docker Magento environment should be:

'host' => 'db',
'dbname' => 'magento',
'username' => 'magento',
'password' => 'magento',

The exact formatting depends on the existing file, but the host must be db, and the database name, username, and password must match the Docker Magento defaults.

Review the rest of env.php for settings that reference the old environment. For example, change the existing.test entry under downloadable_domains to the domain you plan to use for this project:

'downloadable_domains' => [
    'magento.test'
],

Your existing file may contain different values, so update any environment-specific settings that still point to the old installation.

Import the application-specific configuration from the existing project:

bin/magento app:config:import

This applies configuration stored in files such as app/etc/config.php to the database. If the existing project does not have any changes to import, Magento reports that there is nothing to do.

Create the local DNS and SSL configuration for the new domain. Replace magento.test with your own domain if necessary:

bin/setup-domain magento.test

The command asks for your system password so it can add the domain to /etc/hosts. It also creates an SSL certificate. On macOS, you may see another prompt asking to modify the system certificate trust settings. Enter your system password again to trust the certificate.

After the domain setup completes, the containers restart automatically and the development tools package is installed.

Verify that Magento is running in developer mode:

bin/magento deploy:mode:show

The command should report that developer mode is enabled.

Also make sure the cache is enabled:

bin/magento cache:enable

Checking both settings is important because a newly migrated environment can sometimes have an unexpected deployment mode or disabled caches, which causes poor performance.

Open the configured domain in your browser:

https://magento.test

You should see the existing Magento installation running through Docker Magento. The existing content, such as the home page title, confirms that the source code and database were migrated successfully.

The project is now ready for development in Docker Magento using the existing Magento instance data.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!