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:
- Power off your current Droplet in the DigitalOcean control panel.
- Select Take Snapshot and choose a name. (This may take some time depending on how big your current environment is.)
- 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:
- Log in to your Docker Droplet. You can check your current Docker version with the following command:
docker -version
- 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.
- 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. - 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. - 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.
- Now open your browser and, depending on your public IP or domain name, execute this.
- 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
. - 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 running containers |
|
Docker local containers |
|
Access to running container shell |
|
Remove all running containers |
|
Remove all containers |