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

Prerequisites:

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.
    #!/bin/bash
    #############################################
    MAGENTO_PUBLIC_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    MAGENTO_PRIVATE_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  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

17 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):
    Username:
    Password:
    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?

    Thanks!

    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

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

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

    [PDOException]
    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.

      https://www.magemodule.com/all-things-magento/magento2-freebies/magento2-docker-vm-script/

  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
    Password:

    [Composer\Downloader\TransportException]
    Invalid credentials for ‘https://repo.magento.com/packages.json’, aborting.

  6. Thank you for your effort on these script. Is also updated to last (at the moment) Magento, what is great!
    I’ve tried to install using my composer on mac version 2.0.7, who gives me some errors.
    At final it ask for complete rollback.
    Could it be the composer version in use?

    1. LOG (useful part, I think…)

      – Installing magento/magento-composer-installer (0.1.13): Extracting archive
      The “magento/magento-composer-installer” plugin was skipped because it requires a Plugin API version (“^1.0”) that does not match your Composer installation (“2.0.0”). You may need to run composer update with the “–no-plugins” option.
      – Installing laminas/laminas-dependency-plugin (1.0.4): Extracting archive
      The “laminas/laminas-dependency-plugin” plugin was skipped because it requires a Plugin API version (“^1.1”) that does not match your Composer installation (“2.0.0”). You may need to run composer update with the “–no-plugins” option.

      – Installing dealerdirect/phpcodesniffer-composer-installer (v0.5.0): Extracting archive
      The “dealerdirect/phpcodesniffer-composer-installer” plugin was skipped because it requires a Plugin API version (“^1.0”) that does not match your Composer installation (“2.0.0”). You may need to run composer update with the “–no-plugins” option.
      – Installing magento/composer-root-update-plugin (1.0.0): Extracting archive
      The “magento/composer-root-update-plugin” plugin was skipped because it requires a Plugin API version (“^1.0”) that does not match your Composer installation (“2.0.0”). You may need to run composer update with the “–no-plugins” option.
      – Installing magento/inventory-composer-installer (1.1.0): Extracting archive
      The “magento/inventory-composer-installer” plugin was skipped because it requires a Plugin API version (“^1.1”) that does not match your Composer installation (“2.0.0”). You may need to run composer update with the “–no-plugins” option.

      Upgrading to version 1.10.13 (stable channel).

      Use composer self-update –rollback to return to version 2.0.8
      Loading composer repositories with package information
      Updating dependencies (including require-dev)
      Nothing to install or update
      Package container-interop/container-interop is abandoned, you should avoid using it. Use psr/container instead.
      Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
      Package sebastian/finder-facade is abandoned, you should avoid using it. No replacement was suggested.
      Writing lock file
      Generating autoload files

      [ErrorException]
      file_put_contents(/app/app/etc/vendor_path.php): failed to open stream: No such file or directory

      update [–prefer-source] … [–] []…

      Could not open input file: /app/bin/magento

      An error has occurred and the script can no longer continue. Do you want to remove all new Docker containers, volumes and/or installed project files? [N]

      (at final there’s no App folder. Just vendor)

      1. Hi Shawn,
        Your idea with this script is great and should save a lot of time!
        I notice now version of composer should be 2 since composer 1 is deprecated and I script is failing to finish. I’ve locally composer 2.1.3 and replace in your script line for update composer to use 2:
        docker exec -uroot $DOCKER_WEB_CONTAINER_NAME /bin/bash -c “composer self-update –2”

        But still I see warnings about Composer 1 and script failed on updating dependencies:
        Generating autoload files
        96 packages you are using are looking for funding.
        Use the composer fund command to find out more!
        Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
        ./composer.json has been updated
        Loading composer repositories with package information
        Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
        Updating dependencies (including require-dev)

        An error has occurred and the script can no longer continue. Do you want to remove all new Docker containers, volumes and/or installed project files? [N]

        I tried both latest ce versions 2.4.1 and 2.4.2 and see this error. Could you suggest how to fix it?
        Thanks!

          1. Hi Shawn,
            Finally I’m able to make it work. It looks like my issues on update dependencies step was in memory or docker engine version. Once I updated Docker Desktop to 3.4 and increased memory on docker resources to 4G I was able to finish the script successfully and I see working local magento 2.4.2 ce.
            The only remaining issue I noticed once I stop docker and start again (both in command line or via docker desktop UI) I see web container is constantly restarting with error in log:
            error: failed switching to “root”: operation not permitted

            I tried to google it and see many people complain about it like it works only on fist run and then they see this error once restarted docker. And looks like no real solution to it. So far I use workaround with delete web container after stop it and it will be recreated again on next docker compose build command. So once recreated it’s working fine again. Did you faced similar issue and found any solution?

            Thanks!

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.