{"id":135653,"date":"2019-06-24T23:37:39","date_gmt":"2019-06-24T23:37:39","guid":{"rendered":"https:\/\/inchoo.net\/?p=33040"},"modified":"2019-06-24T23:37:39","modified_gmt":"2019-06-24T23:37:39","slug":"development-environment-for-magento-2-using-docker","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/php\/development-environment-for-magento-2-using-docker\/","title":{"rendered":"Development environment for Magento 2 using Docker"},"content":{"rendered":"<p>When I first started with PHP a long time ago, I really had trouble setting up work environment.<br \/>\nI had no clue about what I was doing.<\/p>\n<p>Don&#8217;t ask me how, but I ended up with Apache Tomcat running PHP.<br \/>\nHonestly, I don&#8217;t know if I could even set up such a blasphemy today.<\/p>\n<p><span id=\"more-33040\"><\/span><br \/>\n<strong>EDIT NOTE:<br \/>\nDue the fact that udovicic\/echo:apache-php7.1 is updated, you no longer need apache config, so I edited this post and removed apache configuration.<\/strong><\/p>\n<p>Over the years I went the whole path from xampp, manual LAMP setup, vagrant&#8230;<br \/>\nAll those had at least 1 dealbreaker, especially when I started working with Magento.<\/p>\n<p>Finally, Docker came and made development much easier.<\/p>\n<p>At this point, it becomes a little bit tricky to explain what Docker is.<br \/>\nIt performs operating system level virtualization (containerization) which refers to the operating system feature in which the kernel allows the existence of multiple isolated user-space instances.<br \/>\nIf the sentence above doesn&#8217;t make any sense to you, then maybe it would be wise to visit <a href=\"http:\/\/www.docker.com\" rel=\"noopener\" target=\"_blank\">http:\/\/www.docker.com<\/a> and at some point come back here, otherwise, continue reading&#8230;<\/p>\n<p>What I&#8217;m going to share now is not some Docker wisdom. It is <strong>more about setup capable of running Magento 2 via Docker<\/strong>, so this article assumes that you are somewhat familiar with Docker.<\/p>\n<p>So, let&#8217;s begin&#8230;<\/p>\n<p>Let&#8217;s run Magento 2 on let&#8217;s say <a href=\"http:\/\/m2.docker\">http:\/\/m2.docker<\/a><\/p>\n<p>First thing you should do is add this line somewhere in your \/etc\/hosts file:<\/p>\n<pre class=\"ish\"><code class=\"language-bash\">127.0.0.1 m2.docker www.m2.docker<\/code><\/pre>\n<p>We will need some filesystem structure, so do the following:<\/p>\n<pre class=\"ish\"><code class=\"language-bash\">tomas ~ $ <span class=\"kw2\">mkdir<\/span> m2.docker\ntomas ~ $ <span class=\"kw3\">cd<\/span> m2.docker<span class=\"sy0\">\/<\/span>\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">mkdir<\/span> docker\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">mkdir<\/span> html\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">mkdir<\/span> <span class=\"re5\">-p<\/span> docker<span class=\"sy0\">\/<\/span>db\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">touch<\/span> docker<span class=\"sy0\">\/<\/span>xdebug.ini\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">touch<\/span> docker<span class=\"sy0\">\/<\/span>apache.conf\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">touch<\/span> docker-compose.yml\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ <span class=\"kw2\">touch<\/span> .env<\/code><\/pre>\n<h2>Now, let&#8217;s go file by file:<\/h2>\n<h3>.env<\/h3>\n<p>In this file you can write variables that will be used in docker-compose.yml<br \/>\nMine looks like this:<\/p>\n<pre class=\"ish\"><code class=\"language-bash\"><span class=\"re2\">CONTAINER_PREFIX<\/span>=m2docker\n<span class=\"re2\">SERVER_NAME<\/span>=m2.docker\n<span class=\"re2\">SERVER_ALIAS<\/span>=www.m2.docker\n<span class=\"re2\">DIRECTORY_NAME<\/span>=m2.docker\n<span class=\"re2\">WEB_USER<\/span>=inchoo\nWEB_ROOT = <span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>www<span class=\"sy0\">\/<\/span>html\nMYSQL_DB_HOST = <span class=\"co1\">${CONTAINER_PREFIX}<\/span>_db_1\n<span class=\"re2\">MYSQL_DATABASE<\/span>=inchoo\n<span class=\"re2\">MYSQL_ROOT_USERNAME<\/span>=root\n<span class=\"re2\">MYSQL_ROOT_PASSWORD<\/span>=inchoo\n<span class=\"re2\">MYSQL_USER<\/span>=inchoo\n<span class=\"re2\">MYSQL_PASSWORD<\/span>=inchoo\n<span class=\"re2\">DOCKER_EXEC<\/span>=docker <span class=\"kw3\">exec<\/span>\n<span class=\"re2\">DOCKER_EXEC_INTERACTIVE<\/span>=docker <span class=\"kw3\">exec<\/span> <span class=\"re5\">-i<\/span>\n<span class=\"re2\">DOCKER_EXEC_TTY<\/span>=<span class=\"co1\">${DOCKER_EXEC_INTERACTIVE}<\/span> <span class=\"re5\">-t<\/span><\/code><\/pre>\n<h3>For docker-compose.yml you will need code below.<\/h3>\n<p>I also added some comments with links to documentation.<br \/>\nNote that I&#8217;m using apache-php image created by my colleague <a href=\"https:\/\/inchoo.net\/author\/stjepan-udovicic\/profile\/\">Stjepan<\/a> and I highly recommend it for development.<\/p>\n<pre class=\"ish\"><code class=\"language-bash\"><span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file<\/span>\nversion: <span class=\"st0\">&quot;3.6&quot;<\/span>\n&nbsp;\n<span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#service-configuration-reference<\/span>\nservices:\n&nbsp; <span class=\"co0\">#custom name<\/span> apache-php: <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#image<\/span> <span class=\"co0\"># https:\/\/githheizenberg ub.com\/udovicic\/echo =&gt; https:\/\/hub.docker.com\/r\/udovicic\/echo\/<\/span> image: udovicic<span class=\"sy0\">\/<\/span>echo:apache-php7.1\n&nbsp; <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#ports<\/span> ports: - <span class=\"st0\">&quot;80:80&quot;<\/span>\n&nbsp; <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#expose<\/span> expose: - <span class=\"st0\">&quot;9000&quot;<\/span>\n&nbsp; <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#volumes<\/span> volumes: - .<span class=\"sy0\">\/<\/span>docker<span class=\"sy0\">\/<\/span>xdebug.ini:<span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>php<span class=\"sy0\">\/<\/span><span class=\"nu0\">7.1<\/span><span class=\"sy0\">\/<\/span>mods-available<span class=\"sy0\">\/<\/span>xdebug.ini - .<span class=\"sy0\">\/<\/span>html:<span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>www<span class=\"sy0\">\/<\/span>html\n&nbsp; <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#environment<\/span> environment: - <span class=\"re2\">TERM<\/span>=xterm-256color - <span class=\"re2\">APACHE_RUN_USER<\/span>=<span class=\"nu0\">1000<\/span>\n&nbsp; <span class=\"co0\"># https:\/\/docs.docker.com\/compose\/compose-file\/#network-configuration-reference<\/span> networks: default: aliases: - <span class=\"co1\">${SERVER_NAME}<\/span> - <span class=\"co1\">${SERVER_ALIAS}<\/span>\n&nbsp; db:\n&nbsp; <span class=\"co0\"># https:\/\/hub.docker.com\/_\/mysql\/<\/span> image: mysql:<span class=\"nu0\">5.7<\/span>\n&nbsp; volumes: - .<span class=\"sy0\">\/<\/span>docker<span class=\"sy0\">\/<\/span>db<span class=\"sy0\">\/<\/span>data:<span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>mysql\n&nbsp; environment: MYSQL_ROOT_PASSWORD: <span class=\"co1\">${MYSQL_ROOT_PASSWORD}<\/span> MYSQL_DATABASE: <span class=\"co1\">${MYSQL_DATABASE}<\/span> MYSQL_USER: <span class=\"co1\">${MYSQL_USER}<\/span> MYSQL_PASSWORD: <span class=\"co1\">${MYSQL_PASSWORD}<\/span>\n&nbsp; redis:\n&nbsp; <span class=\"co0\"># https:\/\/hub.docker.com\/_\/redis\/<\/span> image: redis:latest\n&nbsp; phpmyadmin:\n&nbsp; <span class=\"co0\"># https:\/\/hub.docker.com\/r\/phpmyadmin\/phpmyadmin\/<\/span> image: phpmyadmin<span class=\"sy0\">\/<\/span>phpmyadmin\n&nbsp; ports: - <span class=\"st0\">&quot;8080:80&quot;<\/span>\n&nbsp; environment: MYSQL_USERNAME: <span class=\"co1\">${MYSQL_ROOT_USERNAME}<\/span> MYSQL_ROOT_PASSWORD: <span class=\"co1\">${MYSQL_ROOT_PASSWORD}<\/span><\/code><\/pre>\n<h3>&#8230;and xdebug.ini<\/h3>\n<pre class=\"ish\"><code class=\"language-bash\"><span class=\"re2\">zend_extension<\/span>=xdebug.so\nxdebug.remote_autostart=<span class=\"nu0\">0<\/span>\nxdebug.remote_enable=<span class=\"nu0\">1<\/span>\nxdebug.remote_port=<span class=\"nu0\">9000<\/span>\nxdebug.remote_connect_back=<span class=\"nu0\">1<\/span><\/code><\/pre>\n<p>A this point we have working environment, so let&#8217;s try to run it:<\/p>\n<pre class=\"ish\"><code class=\"language-bash\">tomas ~<span class=\"sy0\">\/<\/span>m2.docker $ docker-compose up <span class=\"re5\">-d<\/span>\nCreating network <span class=\"st0\">&quot;m2docker_default&quot;<\/span> with the default driver\nCreating m2docker_apache-php_1 ... <span class=\"kw1\">done<\/span>\nCreating m2docker_redis_1 ... <span class=\"kw1\">done<\/span>\nCreating m2docker_phpmyadmin_1 ... <span class=\"kw1\">done<\/span>\nCreating m2docker_db_1 ... <span class=\"kw1\">done<\/span>\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $ docker <span class=\"kw2\">ps<\/span>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\ne3384b0eff8c mysql:<span class=\"nu0\">5.7<\/span> <span class=\"st0\">&quot;docker-entrypoint.s\u2026&quot;<\/span> <span class=\"nu0\">3<\/span> seconds ago Up <span class=\"nu0\">2<\/span> seconds <span class=\"nu0\">3306<\/span><span class=\"sy0\">\/<\/span>tcp m2docker_db_1\nd283bf018330 phpmyadmin<span class=\"sy0\">\/<\/span>phpmyadmin <span class=\"st0\">&quot;\/run.sh phpmyadmin&quot;<\/span> <span class=\"nu0\">3<\/span> seconds ago Up <span class=\"nu0\">2<\/span> seconds <span class=\"nu0\">9000<\/span><span class=\"sy0\">\/<\/span>tcp, 0.0.0.0:<span class=\"nu0\">8080<\/span>-<span class=\"sy0\">&gt;<\/span><span class=\"nu0\">80<\/span><span class=\"sy0\">\/<\/span>tcp m2docker_phpmyadmin_1\nded8bce1d993 redis:latest <span class=\"st0\">&quot;docker-entrypoint.s\u2026&quot;<\/span> <span class=\"nu0\">3<\/span> seconds ago Up <span class=\"nu0\">2<\/span> seconds <span class=\"nu0\">6379<\/span><span class=\"sy0\">\/<\/span>tcp m2docker_redis_1\nfe8a80763ac6 udovicic<span class=\"sy0\">\/<\/span>echo:apache-php7.1 <span class=\"st0\">&quot;\/start.sh&quot;<\/span> <span class=\"nu0\">3<\/span> seconds ago Up <span class=\"nu0\">2<\/span> seconds 0.0.0.0:<span class=\"nu0\">80<\/span>-<span class=\"sy0\">&gt;<\/span><span class=\"nu0\">80<\/span><span class=\"sy0\">\/<\/span>tcp, <span class=\"nu0\">9000<\/span><span class=\"sy0\">\/<\/span>tcp m2docker_apache-php_1\ntomas ~<span class=\"sy0\">\/<\/span>m2.docker $<\/code><\/pre>\n<h2>Now when it works&#8230;<\/h2>\n<p>Magento should be installed in &#8220;html&#8221; directory.<br \/>\nNote that you need to use &#8220;db&#8221; when referring to database address, and so on. Just check docker-compose.yml (service name).<br \/>\nAlso, relevant credentials are in .env file.<\/p>\n<p>PhpMyAdmin is available @ http:\/\/m2.docker:8080<\/p>\n<p>This is a very basic setup. I usually create git repository which initially ends up with 2 branches like for example m2-docker and master and then I copy .git directory into html and checkout master branch while in the base directory I checkout m2-docker branch.<\/p>\n<p>It would be tricky to write an article about all that, but if you want to play with it, check:<br \/>\n<a href=\"https:\/\/github.com\/tomasinchoo\/m2-sample.docker\/tree\/master\">https:\/\/github.com\/tomasinchoo\/m2-sample.docker\/tree\/master<\/a> (make sure to check m2-sample.docker branch too).<\/p>\n<p>There is also a repository for Magento 1 here: <a href=\"https:\/\/github.com\/tomasinchoo\/m1-sample.docker\">https:\/\/github.com\/tomasinchoo\/m1-sample.docker<\/a><br \/>\nIt&#8217;s still not 100% done, but feel free to post issue directly on github page.<\/p>\n<p>Thanks for reading.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/inchoo.net\/magento-2\/development-environment-magento-2-using-docker\/\">Development environment for Magento 2 using Docker<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/inchoo.net\">Inchoo<\/a>.<\/p>\n<h1><a target=\"_blank\" href=\"http:\/\/www.circulatetechnology.com\/\">Click here for more informations about top website development services <\/a><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>When I first started with PHP a long time ago, I really had trouble setting up work environment. I had no clue about what I was doing. Don&#8217;t ask me how, but I ended up with Apache Tomcat running PHP. Honestly, I don&#8217;t know if I could even set up such a blasphemy today. EDIT &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[256,8226,8227,8225,20,19,273],"tags":[],"class_list":["post-135653","post","type-post","status-publish","format-standard","","category-configuration","category-docker","category-environment","category-integration","category-magento-2","category-php","category-tips"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zhX","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/135653"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=135653"}],"version-history":[{"count":0,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/135653\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=135653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=135653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=135653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}