Quick Magento 2 + Docker Virtual Machine Script

Docker + Magento + Redis + Xdebug

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


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:

  1. 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
  2. Make the new file executable.
    chmod +x /usr/local/bin/magento2-install
  3. 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.
  4. Feel free to change any of the default values contained near the top of the file.
  5. Save your changes and close the file.
  6. Open a new terminal window as the new scripts may not yet be active in your current terminal session. cmd+t
  7. 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:

  1. 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 ps from the command line. If any containers are bound to any of the aforementioned ports, please stop or remove those containers.
  2. In your terminal, run this command: magento2-install
  3. Which version of Magento do you want to spin up?
    Enter any valid Magento 2 version number. For example, 2.2.6
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. Install sample data?
    Press enter to use the default value of yes.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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:
    Installation success content

  16. 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.

Docker Commands

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:

docker-compose stop

To tear down the Docker vm, run the following command from the root of the Magento installation:

docker-compose down

To spin the vm back up, run the following command from the root of the Magento installation:

docker-compose up -d --build

10 Replies to “Quick Magento 2 + Docker Virtual Machine Script”

  1. Thank you Shawn Abramson! That worked perfectly fine.

    I have one question where I have updated magento access keys in magento2-install file.

    But not sure why this has asked me for credentials again.

    Authentication required (repo.magento.com):
    Do you want to store credentials for repo.magento.com in /Users/Username/.composer/auth.json ? [Yn]

    1. Thanks Ashwin! That is your composer asking you if you want to install those keys globally on your machine. After you do that the first time, you shouldn’t have to do that again. I forget about it sometimes because my keys have been stored there for so long. Glad it worked for you!

  2. Hi Shawn,
    I am totally new to Magento 2, Docker and MacOS. I followed the steps and keep getting this error;

    Composer could not find a composer.json file in /app
    To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ “Getting Started” section
    Could not open input file: /app/bin/magento

    I have follow the composer documentation and have placed the composer.json file in and root dir inside an /app directory. I have also tried it in the root dir and anywhere I thought it would work but no dice…
    Can you guide me through this error?


    1. Hi Mike, it sounds like your Magento files are not mounted to the /app directory inside of the “web” container. Run this command on your mac and see if where your files mount to. The command is: docker inspect web

  3. Hi Shawan:

    I get following error when I run any magento CLI commands

    SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename
    nor servname provided, or not known

    SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename
    nor servname provided, or not known

    PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor
    servname provided, or not known

    1. Hi Jobin, this means you most likely missed a step somewhere. That error is indicative that you are not making a successful connection with the database. Did you follow the tutorial exactly as written?

      Also, I now have a one-line CLI command that does everything in this tutorial. All you gotta do is answer a few questions and the new command does everything.


  4. Hi Shawan,

    Awesome Tutorial. Thanks for taking the time to create it. If I want to use this script for a production-ready version, what thing do you recommend me to take into account?

  5. I got the following error after enter the password:

    Please enter sudo password so that an entry can be made to the /etc/hosts file

    Invalid credentials for ‘https://repo.magento.com/packages.json’, aborting.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.