Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Magento 2 Cookbook

You're reading from   Magento 2 Cookbook Exploring Magento 2 in the form of recipes

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785887062
Length 342 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Installing Magento 2 on Apache and NGINX 2. Magento 2 System Tools FREE CHAPTER 3. Enabling Performance in Magento 2 4. Creating Catalogs and Categories 5. Managing Your Store 6. Creating a Magento 2 Theme 7. Creating Magento 2 Extensions – the Basics 8. Creating Magento 2 Extensions – Advanced 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.)
    Getting ready
  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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime