{"id":26033,"date":"2015-09-03T10:03:43","date_gmt":"2015-09-03T04:33:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=26033"},"modified":"2017-01-23T09:45:00","modified_gmt":"2017-01-23T04:15:00","slug":"setup-wordpress-using-dockerfile-with-containers-on-two-different-host-machines","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/setup-wordpress-using-dockerfile-with-containers-on-two-different-host-machines\/","title":{"rendered":"Setup WordPress using Dockerfile with containers on two different host machines"},"content":{"rendered":"<p>This blog post\u00a0describes setting up LAMP stack using containers via docker on different servers. Both the MySQL and Apache docker containers will be hosted on different hosts. We will be using WordPress as the application on the\u00a0LAMP\u00a0stack.<\/p>\n<h3><strong>Use-case<\/strong><\/h3>\n<p>The containers will be created using Dockerfile. Dockerfiles are the easiest way to configure images, which will be further used to create the containers. Dockerfile can be created on any location. They do not require a specific path or folder structure to be followed. Here we will be using ~\/docker\/mysql for MySQL Dockerfile and ~\/docker\/apache for Apache Dockerfile.<\/p>\n<h3><strong>Mysql Dockerfile<\/strong><\/h3>\n<p>The Dockerfile used in this article is as below:-<\/p>\n<p>[js]FROM dockerttnd\/mysqlinfra:2.0<br \/>\nMAINTAINER AS Support rahul.jaiswal@tothenew.com<br \/>\nADD my.cnf \/etc\/mysql\/<br \/>\nADD mysql.sh \/<br \/>\nRUN chmod +x mysql.sh<br \/>\nADD setup.sql \/<br \/>\nADD run.sh \/<br \/>\nRUN chmod +x run.sh<br \/>\nExpose 3306<br \/>\nCMD [&amp;amp;quot;\/run.sh&amp;amp;quot;]<br \/>\n[\/js]<\/p>\n<p>Details regarding the options used in above Dockerfile :-<\/p>\n<p>1.\u00a0<strong>FROM <\/strong>: This specifies the base image from which the MySQL image will be built and further customized. The \u201cdockerttnd\/mysqlinfra:2.0\u201d image will be automatically downloaded. This image has MySQL installed on the container.<br \/>\n2.\u00a0<strong>MAINTAINER AS <\/strong>: This specifies the maintainer\/creator of the images created by the above docker file.<br \/>\n3. <strong>ADD<\/strong>: my.cnf \/etc\/mysql\/ : ADD option adds the files required for configuration. Here we are providing our my.cnf to be used by MySQL instance. You can use your own my.cnf. The my.cnf is added to \/etc\/mysql\/ location using the above command.<br \/>\n4.\u00a0<strong>ADD mysql.sh<\/strong> : Now, we are adding\u00a0mysql.sh to the final container. This script contains post install configurations to be applied. The mysql.sh script is as below :-<\/p>\n<p>[js]#!\/bin\/bash<br \/>\nset -x<br \/>\nexec &amp;amp;amp;amp;&amp;amp;amp;gt; test.txt<br \/>\nmysql_install_db &amp;amp;amp;amp;<br \/>\nsleep 5<br \/>\n\/usr\/bin\/mysqld_safe &amp;amp;amp;<br \/>\nsleep 6<br \/>\n\/usr\/bin\/mysql -uroot &amp;amp;amp;gt;test.txt<br \/>\n\/bin\/bash [\/js]<\/p>\n<p>5.<strong> RUN chmod +x mysql.sh<\/strong> : In this step we are setting up permissions for mysql.sh to executable.<br \/>\n6.\u00a0<strong>ADD setup.sql \/<\/strong> : This file contains DB and user settings which will be used by the Apache image to connect to MySQL container.<\/p>\n<p>[js]<br \/>\n&amp;amp;lt;pre&amp;amp;gt;&amp;amp;lt;pre&amp;amp;gt;use mysql;<br \/>\ncreate database wordpress;<br \/>\ngrant all on wordpress.* to &#8216;wordpress1&#8217;@&#8217;privateipofapachehost&#8217; identified by &#8216;password&#8217;;<br \/>\nflush privileges;[\/js]<\/p>\n<p><strong>privateipofapachehost<\/strong> is the private IP of host server on which Apache container will be running.<br \/>\n7.\u00a0<strong>ADD run.sh \/<\/strong> : Adding the run.sh script which will be our entry point script for initiating the configurations for MySQL container.<\/p>\n<p>[js]#!\/bin\/bash<br \/>\n \/mysql.sh &amp;amp;gt; test.txt [\/js]<\/p>\n<p>8.<strong> RUN chmod +x run.sh <\/strong>: Setting the permissions for run.sh script to executable.<br \/>\n9.\u00a0<strong>Expose 3306<\/strong> : Exposing the MySQL port of the MySQL container to the host server.<br \/>\n10.<strong> CMD [&#8220;\/run.sh&#8221;]<\/strong> : CMD command sets the initial command\/script which will be called when our container is started.<\/p>\n<p>The MySQL container Dockerfile is complete.<\/p>\n<p>Now, we need to create the image using the above Dockerfile. The following image will be used to create MySQL container :-<br \/>\n1. Build the image using the above\u00a0Dockerfile.<\/p>\n<p>[js]docker build -t mysql ~\/docker\/mysql\/. [\/js]<\/p>\n<p>2. Create the container using MySQL image built using the above command.<\/p>\n<p>[shell]docker run -itd \u2013restart=always \u2013name mysqldb -v \/data\/mysql:\/var\/lib\/mysql -p 3306:3306 mysql[\/shell]<\/p>\n<p>Explanation of above options :-<\/p>\n<p>1.\u00a0<strong>&#8211;restart<\/strong> : This option specifies the restart policies for the container. The restart policy has three options:-<br \/>\n<strong>no<\/strong>= Do not restart container on exit [default].<br \/>\n<strong>on-failure<\/strong>= restart only if container exits with non-zero exit status.<br \/>\n<strong>always<\/strong>=always restart container if the container is not running.<br \/>\n2. <strong>-v<\/strong> : This option mounts the host directory to container.<br \/>\n3. <strong> -p 3306:3306 <\/strong>: The -p 3306:3306 parameter in the docker run command maps the default MySQL port (3306) of the container with the IP of the host server, so that the Apache web container created in the further steps is able to connect to the database server.<\/p>\n<p>The MySQL container is up now. The same can be verified by using command \u00a0<code>docker ps<\/code>.<\/p>\n<p>Now, let&#8217;s create the Apache web container on the other server.<\/p>\n<h3><strong>Apache Server Docker file<\/strong><\/h3>\n<p>[js]FROM ubuntu:latest<br \/>\nRUN apt-get update<br \/>\nRUN apt-get -y install apache2 php5 php5-mysql<br \/>\nRUN cd \/var\/www<br \/>\nRUN apt-get install wget -y<br \/>\nRUN wget http:\/\/wordpress.org\/latest.tar.gz<br \/>\nRUN tar -xzvf latest.tar.gz -C \/var\/www\/<br \/>\nRUN cp \/var\/www\/wordpress\/wp-config-sample.php \/var\/www\/wordpress\/wp-config.php<br \/>\nRUN sed -i &#8216;s\/database_name_here\/wordpress\/&#8217; \/var\/www\/wordpress\/wp-config.php<br \/>\nRUN sed -i &#8216;s\/username_here\/wordpress1\/&#8217; \/var\/www\/wordpress\/wp-config.php<br \/>\nRUN sed -i &#8216;s\/password_here\/password\/&#8217; \/var\/www\/wordpress\/wp-config.php<br \/>\nRUN sed -i &amp;amp;quot;s\/localhost\/ip of the host server of mysql container\/g&amp;amp;quot; wordpress\/wp-config.php &amp;amp;lt;em&amp;amp;gt;\/\/***&amp;amp;lt;\/em&amp;amp;gt;<br \/>\nRUN cd \/etc\/apache2\/sites-enabled\/<br \/>\nRUN sed -i &#8216;s\/\\\/var\\\/www\\\/html\/\\\/var\\\/www\\\/wordpress\/&#8217; \/etc\/apache2\/sites-enabled\/000-default.conf<br \/>\nCMD [&amp;amp;quot;apache2ctl&amp;amp;quot; ,&amp;amp;quot; -DFOREGROUND&amp;amp;quot;] [\/js]<\/p>\n<p><em>** ip must be the private ip of the host server of containing the mysql container.<\/em><\/p>\n<p>Now, let&#8217;s build the image similar to as we build MySQL image with Dockerfile :-<\/p>\n<p>[js]docker build -t apache ~\/docker\/apache\/.[\/js]<\/p>\n<p>Running the apache container :-<\/p>\n<p>[js]docker run \u2013itd \u2013restart=always \u2013name web -p 80:80 apache [\/js]<\/p>\n<p>Now, the setup is up. We can test the setup by browsing the public IP of the Apache container.<\/p>\n<h4><em>References :-<\/em><br \/>\n<em> <\/em><br \/>\n<em> <\/em><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>This blog post\u00a0describes setting up LAMP stack using containers via docker on different servers. Both the MySQL and Apache docker containers will be hosted on different hosts. We will be using WordPress as the application on the\u00a0LAMP\u00a0stack. Use-case The containers will be created using Dockerfile. Dockerfiles are the easiest way to configure images, which will [&hellip;]<\/p>\n","protected":false},"author":563,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1],"tags":[2310,2311,2309],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26033"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/563"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=26033"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26033\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=26033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=26033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=26033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}