The following is my current preferred strategy for Magento 2 development. It is a little more work to set up, but has the least disadvantages all up. The approach is based on using a tool to watch a directory of local files and copying them into the Docker container whenever the files are modified.
The idea is to create a relatively sparse directory structure of files that you want to override or contribute to the Docker container. For example, if you want to add a dependency on an extension, you would make a copy of the “composer.json” file and modify it. This file is then copied back into the container whenever a change is made to the file. Similarly, if creating a new theme, create the “app/design” directory locally and create the theme under this directory. The whole directory tree will then be copied into the Docker container.
The approach has the following benefits.
- All files to be modified are maintained on your local host. This implies it is safe to delete the container at any time and create a new one.
- The approach is pretty easy to understand – it is just copying files over the top of the default directory structure.
- Tools like Gulp work – they are able to reliably spot file system changes. (This is not true of mounting volumes for example.)
- Standard native text editors including any other file manipulation utilities can be used on the local host.
- There is no need for .gitignore files – you can easily use source code control on the local set of files.
- This approach can also be used with a remote AWS or similar server running in the cloud, although it is recommended to harden the container first (e.g. change the default SFTP password).
For Windows, WinSCP makes pushing file changes straightforward; for Linux, lsyncd provides the same functionality. The following describes how to get WinSCP running to copy file changes as soon as they are made.
- Step 1 is to get Docker installed and the container up and running. This is described on the “Introduction to Docker” page. Make sure port 22 (SSH/SFTP) is available on port 2222.
- Step 2 is to register all the SFTP connection settings with WinSCP. To create a new connection, select “SFTP” as the protocol, the IP address of your docker container instance for “Host name” (192.168.99.100 in my case), Port number 2222 for connections, and enter a username of “magento” with password “magento”.
- Click “Save” to save the settings for future use and “Login” to connect. The right hand window will show the code base in the Docker container.
- Next, change the root directory of the left hand panel to the directory holding the locally developed code. Change the root directory of the right hand side to /magento2. You can then manually drag and drop files over to the new container if desired.
- From the menu, turn on “Commands” / “Keep Remote Directory up to Date” (or just press control-U). Select the “Update subdirectories” and “Synchronize on start” checkboxes. Do not select “Delete files” or “Existing files only”. The other settings you may adjust as you see fit. Once enabled, all changes made to the local directory will be automatically copied into the Docker container, ready for use. (This is similar to the PHP Storm mode described on another page.)
TODO: Need to add instructions for lsyncd. Need to make sure ssh and rsync is working correctly into the Docker container.
Once WinSCP (for Windows) or lsyncd (for Mac) is running, you can proceed with developing your application locally.
See the page on PHP Storm if you want to have the complete set of files available locally for reference. You would copy the files out of the container into a different directory on the local file system and tell PHP Storm (or similar IDE) the two directories to include in the project. For other related pages, see the “Book” menu at the top right of this page.