Not Just Another Magento 2 + Docker Install Script
Happy New Year everyone! For this first post of 2019, I am going to share a script I recently created for myself. This script allows you to spin up a Magento 2 + Docker Virtual Machine with one simple command. You will be able to choose the specific version of Magento, whether or not to use Redis, and whether or not to use sample data. After the VM is spun up, Xdebug will be automatically enabled and active, ready for use. The files will live on your local Mac and be mounted into the Docker web container. The page load time of the VM is pretty good too!
Born of Necessity
Here’s the story behind the script. Whether I am starting a new Magento 2 project or creating Magento extensions, I often need a clean installation of Magento. When I do, I usually refer back to my previous blog post, Docker for Magento 2 Development, and borrow from my own instructions. However, I find myself needing to do this constantly. The instructions in my previous blog post work well but I need to spin up Magento instances even quicker. I am a programmer and that means when I realize I am performing the same task over and over again, I program it. Thus, the quick Magento 2 + Docker virtual machine script was born!
The End Result
- Magento 2.x.x running on Docker
- Xdebug active and ready for development
- Files mounted from Mac into Docker container
- Persistent database
- Docker web container
- Docker database container
- Docker Redis container (optional)
- Installed sample data (optional)
- Automatic domain entry to hosts file
- Decent page load time
- Subsequent VM spin-ups in 5 seconds or less
A few things about the script and then we will get into the details. This script requires that you are developing on a Mac. When you issue the new magento2-install command, you will be asked a series of questions. Most of the questions already have valid default answers so you can skip over them unless you need to change something. After the questions are answered, Magento 2 will be downloaded and installed via composer.
Preparation for Magento 2 Script Install
- Developing on a Mac (not tested on Windows)
- Install Docker
- Install Composer
- Obtain Free Magento Access Keys
- Download the Script from Github
Add New CLI Command
Assuming you are developing on a Mac, have installed Docker and Composer, and have obtained your free Magento access keys, you’re ready to roll. After you follow these steps, you will have a new command to use in your terminal, magento2-install.
To install the new command, follow these steps:
- Move the downloaded magento2-install.txt file to your user’s bin directory. This can be done easily via the command line. After the file is moved, it should no longer have the .txt extension.
mv ~/Downloads/magento2-install.txt /usr/local/bin/magento2-install
- Make the new file executable.
chmod +x /usr/local/bin/magento2-install
- Open /usr/local/bin/magento2-install in the text editor of your choice. Edit lines 3-4 by replacing the placeholder “X”s with your Magento public and private keys.
#!/bin/bash ############################################# MAGENTO_PUBLIC_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MAGENTO_PRIVATE_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- Feel free to change any of the default values contained near the top of the file.
- Save your changes and close the file.
- Open a new terminal window as the new scripts may not yet be active in your current terminal session.
- Done! The script is now installed and ready for use.
Install Magento 2 With a New CLI Command
Now that you’ve installed the new CLI command, you’re ready to spin up unlimited instances of Magento. It should be noted that at the time of this writing, there is an issue when installing Magento 2.3.0. This is not the result of an error in our script but rather it is a core bug that is likely known to Magento already. When you run magento2-install, you will be asked several questions. Just about every question has a default value and you can simply hit the enter key to skip the question and use the default. If a question has a default value available, the default value will be shown in square brackets next to the question. Additionally, there is some light validation to the questions so that you don’t inadvertently enter an invalid answer.
To install Magento 2, follow these steps:
- Make sure you don’t have any other Docker containers bound to ports 80, 443, 6379, 32834, or 33067. To check this, just run
docker psfrom the command line. If any containers are bound to any of the aforementioned ports, please stop or remove those containers.
- In your terminal, run this command:
- Which version of Magento do you want to spin up?
Enter any valid Magento 2 version number. For example, 2.2.6
- Which edition, community or enterprise?
Press the enter key to use the default value of community. If you choose enterprise, your Magento access keys must be associated with an enterprise license. Valid values for this field are: community, ce, enterprise, or ee.
- What is the path to your private SSH key?
Press the enter key to use the default value of ~/.ssh/id_rsa. If this is not the SSH key that you use, just enter the path to the appropriate key and hit enter.
- In which directory should we create the project?
Press the enter key to use the default location of ~/Websites/Magento2. Don’t worry, the project will be installed in that directory inside of another folder with a unique name so that you can install multiple projects at this location. If the directory entered does not exist, you will be alerted in the terminal and asked to try again.
- Which directory name should we use for the project?
Press the enter key to use the default project name. The default project name that the script creates is a combination of “magento” + version number + edition abbreviation. For example, magento226ce would be the directory name for Magento 2.2.6 CE. You can change this directory name to anything you’d like.
- What domain name should we use?
Press the enter key to use the automatically generated domain name. The default domain name uses the previously entered directory name. For Magento 2.2.6 CE, the default domain name would be local.magento226ce.com. However, you can use any domain name that you would like.
- Install sample data?
Press enter to use the default value of yes.
- Which Docker web image?
Press the enter key to use the default web image webdevops/php-apache-dev:7.1. When installing Magento versions 2.0.x or 2.1.x, the default web image will be webdevops/php-apache-dev:7.0. You can use any valid Docker image but this one has all of the goodies needed to develop on Magento 2. I’ve been using it for a couple of years and it’s great. WebDevOps offers a number of free images that are fantastic for web development. We are not affiliated with them in any way, we just love the Docker images they offer. Click here to see more.
- Which Docker database image?
Press the enter key to use the default database image mariadb:10. You can use any compatible MySQL image but MariaDB works great.
- Use Redis for caching and session storage?
Press the enter key to use the default value of yes. You should note, however, that this option won’t be presented when installing Magento 2.0.x or 2.1.x. This isn’t because you can’t use Redis on those versions, it’s just that the installation process is different. We have not yet taken the time to adjust the install script to accommodate that different process.
- Which Docker Redis image?
This question is only asked if you answered yes to the previous question. Press the enter key to use the default value of redis:latest. However, you can replace this value with any valid redis image.
- Please enter sudo password so that an entry can be made to the /etc/hosts file
When you enter your password, an entry for the website’s local domain name will be added to the /etc/hosts file. The script will check to see if it already exists in the hosts file and only add it if needed.
- After entering the sudo password mentioned above, the full installation process will begin. Once the process completes, if all went well, you will be shown a summary of all the website-specific information you need. It will look something like this:
- Done! All that is left do is visit your new website in the browser. The first page load will be slow, which is typical. Subsequent page loads will be a bit faster.
The steps listed above demonstrated how to create an instance of Magento. You only need to run that script for the initial creation of the Magento instance. After the instance has been created, you can start and stop it as needed. These basic commands will point you in the right direction.
To stop the Docker vm, run the following command from the root of the Magento installation:
To tear down the Docker vm, run the following command from the root of the Magento installation:
To spin the vm back up, run the following command from the root of the Magento installation:
docker-compose up -d --build