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.
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:
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:
docker -version
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.
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.
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.
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
.
admin
), password (password123
), and backend URL (http://yourdomain.com/admin
).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.
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 |
3.15.189.199