How to set up your Local Machine with Magento 2 v2.2 and Docker Composer on Linux Ubuntu 16? – A Story of a Programmer interest in Technology, Life Style

Magento 2

Have you ever tried to set up your Magento web application on Docker? Okay, today I thought to share tips to easily set up the Magento application on Docker. Please comment and let me know if you would get any issues while you are trying to set up the Docker-based Magento Application.

What is Docker?

Docker is similar to a container that carries all the environment with its components including the Operating System. We could build a Docker image by Dockerfile with including all the OS services, other application services (MySQL, Elasticsearch, etc) which we want to run for our web application on docker. Docker images will work similarly to Bitbucket archives in Docker Center. Docker Container is the framework for running our web application and by using the docker image. We could build the Docker Container. Docker containers are architecture lightweight and ideal to allow application creation for microservices. Accelerate the development, deployment, and rollback of tens or hundreds of single-application containers.

What is Docker Compose?

Image belongs to codefresh.io

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you could use a Compose file to configure your application’s services. The configuration file is the docker-compose.yml and it includes all the environment variable binding values with Docker Container and your local Dev PC Environment. Then, using a single command, you could create and start all the services from your configuration.

What is Composer?

PHP Composer

Composer is a dependency manager for PHP. The composer will manage the dependencies which require on your project. 

This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.

 All the required libraries and dependencies will include in the composer.json file which locates in the root folder. 

After running the composer update command it will create the composer.lock file and which includes all the records of changes.

Steps to follow:

1. Add Dev URL and registry URL to /etc/hosts file

2. Add Authentication to auth.json in the composer

3. Add docker-compose.yml file

4. Install/update composer

5. Install M2 via install command

6. Add stack module to codepool/app/code

1. Add Dev URL and registry URL to /etc/hosts file

File path : sudo vim /etc/hosts
sudo vim /etc/hosts 
10.0.0.4 m2demo.com.au
10.94.10.97 domainpath.com.au

2. Add Authentication to auth.json in the composer

File path : sudo vim /home/user/.composer/auth.json
sudo vim /home/user/.composer/auth.json 

  "http-basic": 
     "repo.magento.com":  
         "username": "****",
         "password": "****"
    
  

3. Add docker-compose.yml file

add docker-compose.yml file where the code pool folder exists location.

File location: Dev PC project folder path/docker-compose.yml
path/docker-compose.yml 

version: '2'
services:
 web-cont:
   image: dockerimagedomain.com/phpfpm71_project:1.1
   container_name: m2demo-web-cont
   volumes:
     - /home/user/workspace/dockers/m2demo:/var/www/html

   environment:
     - SERVER_NAME=m2demo.com.au
     - USER_ID=1000
   networks:
    m2stackeagletheme_app_net: 
      ipv4_address: 10.0.0.4

 mysql-cont:
  image: dockerimagedomain.com/mysql57_mysql:1.0
  container_name: m2demo-mysql-cont
  network_mode: service:web-cont
  environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_DATABASE=m2demo
    - MYSQL_USER=m2demo
    - MYSQL_PASSWORD=m2demo

networks:
 m2stackeagletheme_app_net:
   external: true

If you are already NOT SETUP the NETWORK,
Replace below networks section with above yml network section:

version: '2'
services:
 web-cont:
   image: dockerimagedomain.com/phpfpm71_project:1.0
   container_name: m2demo-web-cont
   volumes:
     - /home/username/workspace/dockers/m2demo:/var/www/html
   environment:
     - SERVER_NAME=m2demo.com.au
     - USER_ID=1000
   networks:
    m2stackeagletheme_app_net: 
      ipv4_address: 10.0.0.4

 mysql-cont:
  image: dockerimagedomain.com/mysql57_mysql:1.0
  container_name: m2demo-mysql-cont
  network_mode: service:web-cont
  environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_DATABASE=m2demo
    - MYSQL_USER=m2demo
    - MYSQL_PASSWORD=m2demo

networks:
 m2stackeagletheme_app_net:
  driver: bridge
  ipam:
  driver: default
  config:
   - subnet: 10.0.0.0/24
     gateway: 10.0.0.1

- rename your codepool path with docker-compose.yml volumes path
- if require, rename container_name (web container), SERVER_NAME, ipv4_address, container_name (mysql-cont), MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD

- If first time docker-compose run,
docker-compose up
if the second time run,
docker-compose start
- check the docker containers status up or existed by run
docker ps -a
- This command can go inside the docker containers (inside the docker image server)
docker exec -it dockerid (check docker ps -a result) /bin/bash

4. Install/update composer

Install composer if not already installed

sudo apt-get install composer

Add below composer.json to codepool folder:

File location : Dev PC project folder path/composer.json

composer.json 

  "name": "magento/project-enterprise-edition",
  "description": "eCommerce Platform for Growth (Enterprise Edition)",
  "type": "project",
  "version": "2.2.1",
  "license": [
  "proprietary"
],
"require": 
  "magento/product-enterprise-edition": "2.2.1",
  "composer/composer": "@alpha",
  "magento/module-bundle-sample-data": "100.2.*",
  "magento/module-catalog-sample-data": "100.2.*",
  "magento/module-customer-sample-data": "100.2.*",
  "magento/module-theme-sample-data": "100.2.*",
  "magento/module-widget-sample-data": "100.2.*",
  "magento/module-cms-sample-data": "100.2.*",
  "magento/module-review-sample-data": "100.2.*",
  "magento/module-catalog-rule-sample-data": "100.2.*",
  "magento/module-sales-rule-sample-data": "100.2.*",
  "magento/module-sales-sample-data": "100.2.*",
  "magento/module-tax-sample-data": "100.2.*",
  "magento/module-downloadable-sample-data": "100.2.*",
  "magento/module-grouped-product-sample-data": "100.2.*",
  "magento/module-msrp-sample-data": "100.2.*",
  "magento/module-wishlist-sample-data": "100.2.*",
  "magento/module-gift-card-sample-data": "100.2.*",
  "magento/module-product-links-sample-data": "100.2.*",
  "magento/module-configurable-sample-data": "100.2.*",
  "magento/module-customer-balance-sample-data": "100.2.*",
  "magento/module-target-rule-sample-data": "100.2.*",
  "magento/module-gift-registry-sample-data": "100.2.*",
  "magento/module-multiple-wishlist-sample-data": "100.2.*",
  "magento/module-swatches-sample-data": "100.2.*",
  "magento/sample-data-media": "100.2.*",
  "magento/module-offline-shipping-sample-data": "100.2.*"
,
"require-dev": 
  "phpunit/phpunit": "~6.2.0",
  "squizlabs/php_codesniffer": "3.0.1",
  "phpmd/phpmd": "@stable",
  "pdepend/pdepend": "2.5.0",
  "sjparkinson/static-review": "~4.1",
  "friendsofphp/php-cs-fixer": "~2.1.1",
  "lusitanian/oauth": "~0.8.10",
  "sebastian/phpcpd": "2.0.4"
,
"config": 
  "use-include-path": true,
  "secure-http":false
,
"autoload": 
  "psr-4": 
    "Magento\\Framework\\": "lib/internal/Magento/Framework/", 
    "Magento\\Setup\\": "setup/src/Magento/Setup/",
    "Magento\\": "app/code/Magento/"
  ,
"psr-0": 
"": [
"app/code/"
]
,
"files": [
  "app/etc/NonComposerComponentRegistration.php"
],
"exclude-from-classmap": [
  "**/dev/**",
  "**/update/**",
  "**/Test/**"
]
,
"autoload-dev": 
"psr-4": 
  "Magento\\Sniffs\\": "dev/tests/static/framework/Magento/Sniffs/",
  "Magento\\Tools\\": "dev/tools/Magento/Tools/",
  "Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/Magento/Tools/Sanity/",
  "Magento\\TestFramework\\Inspection\\": "dev/tests/static/framework/Magento/TestFramework/Inspection/",
  "Magento\\TestFramework\\Utility\\": "dev/tests/static/framework/Magento/TestFramework/Utility/",
  "Magento\\ToolkitFramework\\": "dev/tools/performance-toolkit/framework/Magento/ToolkitFramework/"
 
,
"minimum-stability": "dev",
"prefer-stable": false,
"extra": 
"magento-force": "override"
 


- run composer update (This will download all the M2 vendor folder modules)

composer update

5. Install M2 via install command

Install the Magento 2 via setup:install command

- create empty db inside the docker IP (10.0.0.4)

magento setup:install 

mysql -h 10.0.0.4 -um2demo -pm2demo

create database m2demo;

php bin/magento setup:install --base-url=" --db-host="10.0.0.4" --db-name="m2demo" --db-user="m2demo" --db-password="m2demo" --admin-firstname="firsnamet" --admin-lastname="lastname" --admin-email="username@email.com" --admin-user="admin" --admin-password="****" --language="en_US" --currency="USD" --timezone="America/Chicago" --use-rewrites="1" --backend-frontname="admin"

Note: Please change the variable values in command as per your requirement 

6. Add Stack Modules to codepool/app/code

- go to codepool/app/code
- create codepool/app/code/Module folder
- inside the Module folder run git clone stack module repo bitbucket
ex: git clone 

git clone 
- git clone will create a module folder and source code inside the folder
- rename the git clone folder name by check the namespace by comparing a model or block class
- run php codepool/bin/magento setup:upgrade
magento setup:upgrade 
php codepool/bin/magento setup:upgrade

Please comment on your issues and happy to help you out!

References:

Scroll to Top