{"id":793,"date":"2019-01-19T18:50:07","date_gmt":"2019-01-19T18:50:07","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=793"},"modified":"2019-02-02T17:23:22","modified_gmt":"2019-02-02T17:23:22","slug":"docker-docker-compose","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=793","title":{"rendered":"Docker: Docker-compose &#8211; A wordpress site"},"content":{"rendered":"<p>The\u00a0<strong>docker-compose<\/strong> is a\u00a0CLI tool that\u00a0automates some very long arguments we other wise will be passing to &#8220;docker run&#8221; command. This tool will also help us to start multiple containers at the same time.<\/p>\n<p>In real world deployments, there can be multiple containers working together for a result, and they will have to communicate to each other. To make this happen certain network settings have to be made, and can be a difficult task to set this up. This is where the docker composer comes.<\/p>\n<p>The command\u00a0<em><strong>docker-compose up<\/strong><\/em> is similar to\u00a0<em><strong>docker run &lt;image&gt;<\/strong><\/em>. The command\u00a0<strong>dock<\/strong><strong>er-compose<\/strong><em><strong> up &#8211;build\u00a0<\/strong><\/em>runs the image after\u00a0<em>re-building<\/em> the images.<\/p>\n<p>&nbsp;<\/p>\n<p>In this example we are going to use two servers to run a WordPress application<\/p>\n<ul>\n<li>A webserver having wordpress<\/li>\n<li>A MariaDB server hosting database<\/li>\n<\/ul>\n<p><em>(<strong>Assumption<\/strong> : You already know how to install and get a wordpress site running)<\/em><\/p>\n<p>Create a folder in your server hosting Docket named <em>\/sandbox\/wordpres<\/em>s.<\/p>\n<p>Download the latest wordpress application and store it in a folder named <em>\/sandbox\/wordpress\/wordpress<\/em><\/p>\n<p>Now go\u00a0to the following\u00a0 folder<\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# pwd<\/strong><\/em><br \/>\n<em>\/sandbox\/wordpress<\/em><\/p>\n<p>Create a file named <em><strong>Dockerfile<\/strong>\u00a0<\/em>with the following content<\/p>\n<p>======= ======= =<\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# cat Dockerfile<\/strong><\/em><br \/>\nFROM centos<br \/>\nRUN yum -y install httpd<br \/>\nRUN yum -y install php<br \/>\nRUN yum -y install php-mysql<br \/>\nRUN yum -y install net-tools curl<br \/>\nRUN yum -y install openssh-clients telnet<\/p>\n<p>RUN echo \u201cWelcome to Shiju webpage\u201d &gt; \/var\/www\/html\/index.html<\/p>\n<p>WORKDIR \/var\/www\/html\/<\/p>\n<p>COPY .\/wordpress .\/wordpress<\/p>\n<p>CMD [&#8220;\/usr\/sbin\/httpd&#8221;, &#8220;-D&#8221;, &#8220;FOREGROUND&#8221;]<\/p>\n<p>======= ======= =<\/p>\n<p>&nbsp;<\/p>\n<p>Create an image named <strong>wpdb\u00a0<\/strong>using Centos as a base image, and install\u00a0<strong>mysql or mariadb server<\/strong> that will have <strong>root<\/strong> at the username and <strong>root123<\/strong> as the password<\/p>\n<p>&nbsp;<\/p>\n<p>Now create a file named\u00a0<strong>docker-compose.yml<\/strong><\/p>\n<p>====== ====<\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# cat docker-compose.yml<\/strong><\/em><\/p>\n<p>version: &#8216;3&#8217;<br \/>\nservices:<br \/>\n<span style=\"color: white;\">$$$$$<\/span> wpdbserver:<br \/>\n<span style=\"color: white;\">$$$$$$$$$$<\/span> image: &#8216;wpdb&#8217;<br \/>\n<span style=\"color: white;\">$$$$$$$$$$<\/span> ports:<br \/>\n<span style=\"color: white;\">$$$$$$$$$$$$$$$<\/span> &#8211; &#8220;3306:3306&#8221;<br \/>\n<span style=\"color: white;\">$$$$$<\/span> wordpressserver:<\/p>\n<p><span style=\"color: white;\">$$$$$$$$$$<\/span> restart: always<br \/>\n<span style=\"color: white;\">$$$$$$$$$$<\/span> build: .<br \/>\n<span style=\"color: white;\">$$$$$$$$$$<\/span> ports:<br \/>\n<span style=\"color: white;\">$$$$$$$$$$$$$$$<\/span> &#8211; &#8220;80:80&#8221;<\/p>\n<p>====== ====<\/p>\n<p>The\u00a0<em><strong>restart<\/strong><strong>:always<\/strong><\/em> parameter ensures the container restarts if it crashes<\/p>\n<p>Run the following command to bring up the containers as per the Dockerfile, and enable them to communicate with each other<\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# docker-compose up\u00a0<\/strong><\/em><\/p>\n<p>Pay attention to the two lines in the output<\/p>\n<p><em>Starting wordpress_wpdbserver_1 &#8230; done<\/em><br \/>\n<em>Starting wordpress_wordpressserver_1 &#8230; done<\/em><\/p>\n<p>Open another window and run the following command to see the status of the containers:<\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# docker ps<\/strong><\/em><\/p>\n<p><em>(Verify if you can see the wordpress site via a browser. Now you have to update the wp-config.php file in the wordpress folder, and also create databases and db username to get the site completed.)<\/em><\/p>\n<p><em><strong>[root@gw20-lap-doc1 wordpress]# docker-compose ps<\/strong><\/em><\/p>\n<p>Run the above command from\u00a0the folder where you ran the\u00a0<em>docker-compose up<\/em> command.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The\u00a0docker-compose is a\u00a0CLI tool that\u00a0automates some very long arguments we other wise will be passing to &#8220;docker run&#8221; command. This tool will also help us <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=793\" title=\"Docker: Docker-compose &#8211; A wordpress site\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":730,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25],"tags":[],"class_list":["post-793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-docker"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=793"}],"version-history":[{"count":24,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/793\/revisions"}],"predecessor-version":[{"id":827,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/793\/revisions\/827"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/730"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=793"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}