Book Image

Magento 2 Cookbook

Book Image

Magento 2 Cookbook

Overview of this book

Magento 2 is an open source e-commerce platform that has all the functionality to function from small to large online stores. It is preferred by developers and merchants due to its new architecture, which makes it possible to extend the functionalities with plugins, a lot of which are now created by the community. This merchant and developer guide is packed with recipes that cover all aspects of Magento 2. The recipes start with simple how-to’s then delve into more advanced topics as the book progresses. We start with the basics of setting up a Magento 2 project on Apache or Nginx. Next, you will learn about basics including system tools and caching to get your Magento 2 system ready for the real work. We move on to simple tasks such as managing your store and catalog configuration. When you are familiar with this, we cover more complex features such as module and extension development. Then we will jump to the final part: advanced Magento 2 extensions. By the end of this book, you’ll be competent with all the development phases of Magento 2 and its most common elements.
Table of Contents (16 chapters)
Magento 2 Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Managing Magento 2 on Docker


Docker is a new way of packaging your application and building containers for every single process. It is very lightweight and easy to set up. Creating building blocks for Apache, NGINX, MySQL, PHP, HHVM, and Magento individually and running them together can save lots of time during development, testing, and production.

In this recipe, we will not cover the Docker fundamentals but learn how to run a Magento 2 Docker setup on your DigitalOcean Droplet using existing containers.

Getting ready

Before we can start using Magento 2 on Docker, we need to create a clean Droplet. Back up your current Droplet by creating a snapshot. Here are some easy steps to create a snapshot and start a new Droplet with Docker preinstalled:

  1. Power off your current Droplet in the DigitalOcean control panel.

  2. Select Take Snapshot and choose a name. (This may take some time depending on how big your current environment is.)

  3. Select Rebuild Droplet, which is in the Destroy menu, choose Ubuntu docker (1.9.1 on 14.04), and press Rebuild from Image.

How to do it…

For the purpose of this recipe, let's assume that we need to create a Magento 2 Docker setup. The following steps will guide you through this:

  1. Log in to your Docker Droplet. You can check your current Docker version with the following command:

    docker -version
    
  2. Before we start pulling the Magento 2 Docker container, we first need to pull a MySQL container. Run the following command in your shell:

    docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin mysql:5.6
    

    The Docker run command will automatically download a MySQL 5.6 container, which will run in the background once it's done.

  3. Now we can check whether the MySQL container works. Run the dockers images command, and then check which images are available. If you want to log in to the container, run the following command:

    docker exec -it mysql bash
    

    You can run any other command here, and then check whether MySQL is running. For example, run the ps --aux command and you will see the process of MySQL.

  4. Now we start pulling the Magento 2 Docker container to our local machine. We are using a prebuilt Docker container hosted at the Docker hub (https://hub.docker.com/u/raybogman/).

    Run the following command to install a clean Magento 2 setup:

    docker run --rm --name magento2 -it -p 80:80 --link mysql:mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e PUBLIC_HOST=
    yourdomain.com raybogman/mage2cookbook-docker $*
    

    Run the following command to install a Magento 2 setup including sample data:

    docker run --rm --name magento2 -it -p 80:80 --link mysql:mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e PUBLIC_HOST=yourdomain.com raybogman/mage2cookbook-sample-docker $*
    

    Change the PUBLIC_HOST setting with your own IP or domain name. The SampleData version has all of its assets to create a preinstalled Magento 2 setup.

  5. Be patient now; this may take some time. The latest Magento 2 container is downloaded and executed on the fly. The final phase is the execution of Apache that will run in the foreground.

  6. Now open your browser and, depending on your public IP or domain name, execute this.

  7. Now we can check whether the Magento 2 container works. Run the dockers images command, and then check which images are available. If you want to log in to the container, run the following command:

    docker exec -it magento2 bash
    

    You can run any other command here, and then check whether Apache 2 is running. For example, run the ps --aux command and you will see the process of Apache 2. All Magento 2 files are located at /var/www/magento2.

  8. Congratulations, you have Magento 2 running using a Docker container. The login credentials are the Magento username (admin), password (password123), and backend URL (http://yourdomain.com/admin).

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 8, we create a Docker setup for Magento 2.

In steps 2 and 3, we set up a MySQL container that will be downloaded automatically and run in the background.

In step 4, we download the prebuilt Magento 2 Docker container and connect it to the MySQL container. The installation process can take some time; Magento needs to deploy the whole setup.

In step 7, you learn how to get shell access to the Magento container and maintain it.

There's more…

To kill/shut down the current Magento 2 container, use the Ctrl + C command in the running shell.

This Magento 2 Docker container is designed only for demo purposes, and is there to run in the foreground and not as a daemon in the background.

Check out the source code of the Magento 2 Docker container on GitHub:

https://github.com/mage2cookbook.

Some basic commands to use Docker are as follows:

docker ps

Docker running containers

docker images

Docker local containers

docker exec -it bash

Access to running container shell

docker rm -f $(docker ps -a -q)

Remove all running containers

docker rmi -f $(docker images -q)

Remove all containers