0858.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Setting Up Shared Database Container in Docker</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta charset="UTF-8">
  7. <meta name="keywords" content="Docker Made Easy,Home Lab,Home Lab Ideas,Install Guide,Self-Hosted,Container,Containerization,Docker,Docker Host,Docker Made Simple,Linux,PHP,PHP Developer,System Administrator,Web Server Administration,Web Server,Web,Shared Database Docker Container,MariaDB,MySQL,How To,Tutorial,i12bretro">
  8. <meta name="author" content="i12bretro">
  9. <meta name="description" content="Setting Up Shared Database Container in Docker">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <meta name="revised" content="01/14/2023 09:50:29 AM" />
  12. <link rel="icon" type="image/x-icon" href="includes/favicon.ico">
  13. <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  14. <script type="text/javascript" src="includes/js/steps.js"></script>
  15. <link href="css/steps.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="gridContainer">
  19. <div class="topMargin"></div>
  20. <div id="listName" class="topMargin">
  21. <h1>Setting Up Shared Database Container in Docker</h1>
  22. </div>
  23. <div></div>
  24. <div id="content">
  25. <h2>The Objective</h2>
  26. <p>To setup a single database container that can be used with multiple applications and frontends.</p>
  27. <h2>Preparation</h2>
  28. <ol>
  29. <li>Pull the Docker images used in the examples by running the following commands in a terminal
  30. <div class="codeBlock"># mariadb<br />
  31. docker pull mariadb<br />
  32. # phpmyadmin<br />
  33. docker pull phpmyadmin<br />
  34. # wordpress<br />
  35. docker pull wordpress</div>
  36. </li>
  37. </ol>
  38. <h2>Method 1 - Container Links</h2>
  39. <p>This method utilizes the --link flag. Container links are a &quot;legacy feature&quot; of Docker and may become deprecated in a future release. Further reading <a href="https://docs.docker.com/network/links/" target="_blank">https://docs.docker.com/network/links/</a></p>
  40. <ol>
  41. <li>Run the following commands in a terminal to setup the container stack using --link
  42. <div class="codeBlock"># create working directories<br />
  43. mkdir ~/docker/mariadb -p &amp;&amp; mkdir ~/docker/wordpress -p<br />
  44. # set permissions on working directories<br />
  45. sudo chown &quot;$USER&quot;:&quot;$USER&quot; ~/docker -R<br />
  46. # run the mariadb docker container<br />
  47. docker run -d --name mariadb -e MYSQL_ROOT_PASSWORD=r00tp@ss -v ~/docker/mariadb:/var/lib/mysql --restart=unless-stopped mariadb:latest<br />
  48. # run phpmyadmin container linked to the mariadb container<br />
  49. docker run -d --name phpmyadmin --link mariadb -e PMA_HOST=mariadb -p 8080:80 --restart=unless-stopped phpmyadmin<br />
  50. # connect to mysql CLI inside the mariadb container<br />
  51. docker exec -it mariadb mysql --user root -pr00tp@ss<br />
  52. # create a new database and service account for wordpress<br />
  53. CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br />
  54. GRANT ALL ON wordpress.* TO &#39;wordpress_rw&#39;@&#39;%&#39; IDENTIFIED BY &#39;W0rdPr3ss!!&#39;;<br />
  55. FLUSH PRIVILEGES;<br />
  56. EXIT;<br />
  57. # run the wordpress docker container<br />
  58. docker run -d --name wordpress --link mariadb -p 8880:80 -e WORDPRESS_DB_HOST=mariadb -e WORDPRESS_DB_USER=wordpress_rw -e WORDPRESS_DB_PASSWORD=&#39;W0rdPr3ss!!&#39; -e WORDPRESS_DB_NAME=wordpress -v ~/docker/wordpress:/var/www/html --restart unless-stopped wordpress</div>
  59. </li>
  60. <li>Remove the created containers by running the following commands
  61. <div class="codeBlock"># remove the containers<br />
  62. docker rm wordpress -f &amp;&amp; docker rm phpmyadmin -f &amp;&amp; docker rm mariadb -f<br />
  63. # remove working directories<br />
  64. sudo rm ~/docker/mariadb -R &amp;&amp; sudo rm ~/docker/wordpress -R</div>
  65. </li>
  66. </ol>
  67. <h2>Method 2 - Docker Networking</h2>
  68. <p>Create a Docker network and connect each of the containers to it so they can communicate.</p>
  69. <ol>
  70. <li>Run the following commands in a terminal to setup the container stack using Docker networking
  71. <div class="codeBlock"># create working directories<br />
  72. mkdir ~/docker/mariadb -p &amp;&amp; mkdir ~/docker/wordpress -p<br />
  73. # set permissions on working directories<br />
  74. sudo chown &quot;$USER&quot;:&quot;$USER&quot; ~/docker -R<br />
  75. # create the docker network<br />
  76. docker network create containers<br />
  77. # run the mariadb docker container<br />
  78. docker run -d --name mariadb -e MYSQL_ROOT_PASSWORD=r00tp@ss -v ~/docker/mariadb:/var/lib/mysql --network containers --restart=unless-stopped mariadb:latest<br />
  79. # run phpmyadmin container<br />
  80. docker run -d --name phpmyadmin -e PMA_HOST=mariadb -p 8080:80 --restart=unless-stopped --network containers phpmyadmin<br />
  81. # connect to mysql CLI inside the mariadb container<br />
  82. docker exec -it mariadb mysql --user root -pr00tp@ss<br />
  83. # create a new database and service account for wordpress<br />
  84. CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br />
  85. GRANT ALL ON wordpress.* TO &#39;wordpress_rw&#39;@&#39;%&#39; IDENTIFIED BY &#39;W0rdPr3ss!!&#39;;<br />
  86. FLUSH PRIVILEGES;<br />
  87. EXIT;<br />
  88. # run the wordpress docker container<br />
  89. docker run -d --name wordpress -p 8880:80 -e WORDPRESS_DB_HOST=mariadb -e WORDPRESS_DB_USER=wordpress_rw -e WORDPRESS_DB_PASSWORD=&#39;W0rdPr3ss!!&#39; -e WORDPRESS_DB_NAME=wordpress -v ~/docker/wordpress:/var/www/html --network containers --restart=unless-stopped wordpress</div>
  90. </li>
  91. <li>Remove the created containers and network by running the following commands
  92. <div class="codeBlock"># remove the containers<br />
  93. docker rm wordpress -f &amp;&amp; docker rm phpmyadmin -f &amp;&amp; docker rm mariadb -f<br />
  94. # remove docker network<br />
  95. docker network rm containers<br />
  96. # remove working directories<br />
  97. sudo rm ~/docker/mariadb -R &amp;&amp; sudo rm ~/docker/wordpress -R</div>
  98. </li>
  99. </ol>
  100. <h2>Method 3 - Exposing Host Ports</h2>
  101. <p>Expose ports on the host into the container and connect other containers to the host&#39;s exposed port</p>
  102. <ol>
  103. <li>Run the following commands in a terminal to setup the container stack using exposed ports on the host
  104. <div class="codeBlock"># create working directories<br />
  105. mkdir ~/docker/mariadb -p &amp;&amp; mkdir ~/docker/wordpress -p<br />
  106. # set permissions on working directories<br />
  107. sudo chown &quot;$USER&quot;:&quot;$USER&quot; ~/docker -R<br />
  108. # run the mariadb docker container<br />
  109. docker run -d --name mariadb -e MYSQL_ROOT_PASSWORD=r00tp@ss -v ~/docker/mariadb:/var/lib/mysql -p 3306:3306 --restart=unless-stopped mariadb:latest<br />
  110. # run phpmyadmin container<br />
  111. docker run -d --name phpmyadmin -e PMA_HOST=$(hostname -f) -p 8080:80 --restart=unless-stopped phpmyadmin<br />
  112. # connect to mysql CLI inside the mariadb container<br />
  113. docker exec -it mariadb mysql --user root -pr00tp@ss<br />
  114. # create a new database and service account for wordpress<br />
  115. CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br />
  116. GRANT ALL ON wordpress.* TO &#39;wordpress_rw&#39;@&#39;%&#39; IDENTIFIED BY &#39;W0rdPr3ss!!&#39;;<br />
  117. FLUSH PRIVILEGES;<br />
  118. EXIT;<br />
  119. # run the wordpress docker container<br />
  120. docker run -d --name wordpress -p 8880:80 -e WORDPRESS_DB_HOST=$(hostname -f) -e WORDPRESS_DB_USER=wordpress_rw -e WORDPRESS_DB_PASSWORD=&#39;W0rdPr3ss!!&#39; -e WORDPRESS_DB_NAME=wordpress -v ~/docker/wordpress:/var/www/html --restart=unless-stopped wordpress</div>
  121. </li>
  122. <li>Remove the created containers and network by running the following commands
  123. <div class="codeBlock"># remove the containers<br />
  124. docker rm wordpress -f &amp;&amp; docker rm phpmyadmin -f &amp;&amp; docker rm mariadb -f<br />
  125. # remove working directories<br />
  126. sudo rm ~/docker/mariadb -R &amp;&amp; sudo rm ~/docker/wordpress -R</div>
  127. </li>
  128. </ol> </div>
  129. </div>
  130. </body>
  131. </html>