Home
About
Search
🌐
English Română
  • How I use Magento2 on my local with Docker and Docker Compose

    Citește postarea în română

    Nov 29, 2018 docker docker-compose magento2 nginx PHP
    Share on:

    Disclaimer

    Please note that this solution is tailored specifically for my needs and, while your needs may vary, don’t worry, everything is on GitHub, so feel free to take what you need.

    I would like to add that this is not a “you’ve been using Docker with Magento2 wrong, this is how it’s done” kind of blog, I just want to share what I’m using and how. It may not be the best fit for you, but maybe you will find something useful.

    Intro

    For almost 2 years I’ve been using Magento2 in Docker containers. I’ve been doing that before, but I must admit that it was because I had to, not because I’ve seen the light, I mean advantages.

    As you may know Magento2 is not exactly a small and light app, it’s quite heavy on the resources, especially during development.

    Compared to a VM, with Docker you get:

    • Speed: I think the speed is one of the biggest advantages, you can stop and start containers very fast, only the first build will take time, after that it will be very fast;
    • Light on resources: Compared to a VM, the container does not need to include the entire operating system, so it will not take a lot of space on disk and will not use a lot of processing power, because it’s not an entire OS doing… well… OS stuff, it’s just a server most of the time.

    What you don’t get:

    • Learning curve: if you don’t know Docker and Docker Compose, it will be less intuitive at first;
    • First setup: harder to setup at first, if you have been using a VM for a long time, you will feel that you are going against the tide, but I assure you, in the long term it will be a lot simpler this way.

    Taking the above into consideration, I would like to say that when I’ve started with this setup I was using Linux with 8G of RAM. One of my colleagues even wished me good luck on installing Magento2 on a ultraportable 8Gb RAM system. He wasn’t even sarcastic, more like pitying me for my bad workstation selection.

    One of the requirements was that I needed some isolation and configuration between projects, I couldn’t just install a server and be done with it.

    Previously I’ve been using Vagrant and VirtualBox, a great fit, very easy to use (most of the time). However, for Magento2 I’ve realised that it was heavy enough on its own, it was making me run out of resources fast.

    Also, I wanted it to be easy to use, I don’t like to have to remember and type out a 3 word command, I just want to press some tabs and get it over with.

    The requirements

    There were some specific requirements:

    • nginx config – should work out of the box, Magento configuration isn’t very small, I wanted to make use of it with ease;
    • SSL – the domain has to also work with HTTPS, mostly because some APIs require it, the certificates don’t need to be valid;
    • bash – the Magento command should work as the system user, not as root (as containers usually do). This is required, because I don’t want the files generated by Magento to be generated as root (and therefore only removable with root rights);
    • xdebug – must work out of the box and be easily integrated with an IDE.

    The implementation and usage

    Magento2 offered a Docker container to work with. I will not say anything about it, since it wasn’t at all something I needed.

    My main source of inspiration was: https://github.com/markoshust/docker-magento. The project changed a lot since I’ve started, so I definitely think you should check it out.

    The starter point is: https://github.com/claudiu-persoiu/magento2-docker-compose

    The relevant files are:

    • magento2 – it should contain a folder html with the project
    • dkc_short – it can reside anywhere, but it should be added to the files ~/.bash_profile or ~/.bashrc, this file contains shortcuts, it’s not necessary, but I like it because it make my life easier;
    • docker-compose.yml – it contains all the mappings and relevant containers.

    NOTE: I think I should point out that the commands on the PHP container run in two ways, as the system user or as root. This is a limitation of the Linux implementation, please make a note of it, as I will refer to it later.

    Step 1:

    What you should do when starting a new project with an existing Magento2 repository:

    1$ git clone https://github.com/claudiu-persoiu/magento2-docker-compose.git project_name
    2$ cd project_name
    3$ git clone your_own_magento2_repository magento2/html
    

    Step 2 (optional):

    Copy the shortcuts to your bash console:

    1$ cp dkc_short ~/
    2$ echo ~/dkc_short >> ~/.bash_profile
    3$ source ~/.bash_profile
    

    NOTE: If you don’t have the file ~/.bash_profile on your computer, just use ~/.bashrc

    Step 3:

    Start the setup:

    1$ dkc-up -d
    

    It will take a bit of time the first time, but it will be a lot faster next time you run it.

    Step 4:

    Run composer install:

    1$ dkc-php-run composer install
    

    That’s about it.

    What is this dkc stuff?

    Well, I like to use tabs when running a command, so I added some aliases that allow me to run a Magento command without typing everything, I just type dkc[tab]p[tab]-[tab] and the command. I just love bash autocomplete.

    The command list is very simple:

    • dkc-up -d – start the containers in the background
    • dkc-down – stop all containers
    • dkc-mag [command] – run a Magento2 command
    • dkc-clean – clear the cache
    • dkc-php-run – run a bash command inside the php container, like composer in the previous example. NOTE: This command is running as the system user, not as root.
    • dkc-exec phpfpm [command] – this is same as above, but running as root. You should almost always use the command above.
    • dkc-exec [container] [command] – this command needs a bit more explanation:
      • container can be:
        • app – for Nginx server,
        • phpfrm – for php container,
        • db – for database,
        • cache or fpc – for cache containers;
    • the command can be anything that applies to that container, like “bash” or “bash composer”, etc.

    I know the commands seem like “one more thing to learn”, but most of the time you will only use the first 4 commands.

    How does the magic work?

    Well, to see what the above commands translate to, just check the “dkc_short” file.

    There are only 2 other interesting repositories:

    • https://github.com/claudiu-persoiu/magento2-docker-php – that contains phpfpm,
    • https://github.com/claudiu-persoiu/magento2-docker-nginx – that contain the nginx server.

    The repositories are pretty small and not very hard to understand.

    If you need to modify anything, just feel free to fork the repositories.

    The conclusion

    That’s about all you need to know about it, I’ve been using this setup for almost 2 years.

    For me, it’s working as a charm and I was able to use Magento2 on ultraportable laptop with 8Gb RAM without any issues.

    The (happy) end!

Claudiu Perșoiu

Programming, technology and more
Read More

Recent Posts

  • Adding a slider to Tasmota using BerryScript
  • The future proof project
  • Docker inside wsl2
  • Moving away from Wordpress
  • Custom path for Composer cache
  • Magento2 and the ugly truth
  • A bit of PHP, Go, FFI and holiday spirit
  • How to make use of the Xiaomi Air Conditioning Companion in Home Assistant in only 20 easy steps!

PHP 49 MISCELLANEOUS 46 JAVASCRIPT 14 MAGENTO 7 MYSQL 7 BROWSERS 6 DESIGN PATTERNS 5 HOME AUTOMATION 2 LINUX-UNIX 2 WEB STUFF 2 GO 1

PHP 35 JAVASCRIPT 15 PHP5.3 11 MAGENTO 7 PHP6 7 MYSQL 6 PHP5.4 6 ZCE 6 CERTIFICARE 5 CERTIFICATION 5 CLOSURES 4 DESIGN PATTERNS 4 HACK 4 ANDROID 3
All tags
3D1 ADOBE AIR2 ANDROID3 ANGULAR1 ANONYMOUS FUNCTIONS3 BERRYSCRIPT1 BOOK1 BROWSER2 CARTE1 CERTIFICARE5 CERTIFICATION5 CERTIFIED1 CERTIFIED DEVELOPER1 CHALLENGE1 CHM1 CLASS1 CLI2 CLOSURES4 CODE QUALITY1 CODEIGNITER3 COFFEESCRIPT1 COLLECTIONS1 COMPOSER1 CSS1 DEBUG1 DESIGN PATTERNS4 DEVELOPER1 DEVELOPMENT TIME1 DOCKER2 DOCKER-COMPOSE1 DOUGLAS CROCKFORD2 ELEPHPANT2 FACEBOOK2 FFI1 FINALLY1 FIREFOX3 GAMES1 GENERATOR1 GO1 GOOGLE1 GOOGLE CHROME1 GOOGLE MAPS1 HACK4 HOMEASSISTANT2 HTML2 HTML HELP WORKSHOP1 HTML51 HUG1 HUGO1 INFORMATION_SCHEMA1 INI1 INTERNET EXPLORER3 IPV41 IPV61 ITERATOR2 JAVASCRIPT15 JQUERY1 LAMBDA1 LINUX1 MAGENTO7 MAGENTO22 MAP1 MINESWEEPER1 MOTIVATION1 MYSQL6 NGINX1 NODE.JS2 NOSQL1 OBSERVER3 OBSERVER PATTERN1 OOP1 OPERA1 OPTIMIZATION1 ORACLE1 PAGESPEED1 PAIR1 PARSE_INI_FILE1 PHONEGAP2 PHP35 PHP ELEPHANT2 PHP FOR ANDROID1 PHP-GTK1 PHP42 PHP53 PHP5.311 PHP5.46 PHP5.53 PHP5.61 PHP67 PHP7.41 PROGRAMMING1 REVIEW1 ROMANIAN STEMMER2 SAFARY1 SCALAR TYPE HINTING1 SCHEME1 SET1 SHOPPING CART PRICE RULE1 SINGLETON1 SOAP1 SPL2 SQLITE1 SSH1 STACK TRACE1 STDERR1 STDIN1 STDOUT1 SUN1 SYMFONY2 TASMOTA1 TEST TO SPEECH1 TITANIUM2 TRAITS1 TTS1 UBUNTU1 UNICODE2 UTF-82 VECTOR1 WEBKIT1 WINBINDER1 WINDOWS2 WORDPRESS1 WSL21 YAHOO3 YAHOO MAPS1 YAHOO OPEN HACK1 YSLOW1 YUI1 ZCE6 ZCE5.31 ZEND3 ZEND FRAMEWORK3
[A~Z][0~9]

Copyright © 2008 - 2024 CLAUDIU PERȘOIU'S BLOG. All Rights Reserved