{"id":130,"date":"2018-02-04T22:37:19","date_gmt":"2018-02-04T21:37:19","guid":{"rendered":"http:\/\/blog.xoupix.fr\/?p=130"},"modified":"2018-02-05T23:14:02","modified_gmt":"2018-02-05T22:14:02","slug":"deploying-wordpress-using-docker","status":"publish","type":"post","link":"https:\/\/blog.xoupix.fr\/index.php\/2018\/02\/04\/deploying-wordpress-using-docker\/","title":{"rendered":"Deploying WordPress using Docker"},"content":{"rendered":"<p>Few days ago, I changed my personal server, with all my data, to another one. On this new one, I choose to use Docker to deploy all my old stuff, including this web site. When investigating throught Docker, I thought it was nice to explain the migration from my old server to my new one !<\/p>\n<p><!--more--><\/p>\n<p>When I installed my previous server, years ago, I did not have any idea and knowlegde about Docker. So, when I had to install my previous server, I went throught internet and check about installing mysql, apache, and deploying wordpress. Since I work with Docker, I have a whole new way to do that ! I will not explain how to install Docker, cause there is another post dealing with that part !<\/p>\n<h1>Retrieving and deploying mysql<\/h1>\n<h2>Retrieving mysql image to Docker<\/h2>\n<p>Knowing the way to retrieve images from external repository, just execute the pull command on mysql package (found it using\u00a0https:\/\/hub.docker.com\/) :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker pull mysql<\/pre>\n<p>Waiting from Docker to download and extract the image, then you are ready to play with mysql !<\/p>\n<h2>Adding user to store mysql data<\/h2>\n<p>I choose to create a user per application I will deploy using Docker. Let&#8217;s create a mysql user, which will store all data from mysql instance, and create associated directory :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">useradd mysql\r\nsu - mysql\r\nmkdir data<\/pre>\n<h2>Deploying mysql using Docker<\/h2>\n<p>Ok, we have the mysql image on our local repository, we have the directory to store data, all we need know is to deploy mysql using Docker ! To do that, just execute the &#8220;docker run&#8221; and specify the mysql image :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker run --name mysql-instance \\\r\n    -v \/home\/mysql\/data:\/var\/lib\/mysql \\\r\n    -e MYSQL_ROOT_PASSWORD=&lt;my root password&gt; \\\r\n    --memory=512m \\\r\n    -d mysql:latest<\/pre>\n<p>Command line explanations :<\/p>\n<ul>\n<li>&#8211;name : container name,<\/li>\n<li>-v \/\u00a0&#8211;volume : bind mount a volume. In this cas, Docker will mount automatically \/home\/mysql\/data server directory to \/var\/lib\/mysql container directory,<\/li>\n<li>-m \/ &#8211;memory : specifying container memory limit<\/li>\n<li>-e : from hub.docker.com documentation : &#8220;<em>This variable is mandatory and specifies the password that will be set for the MySQL\u00a0<code>root<\/code>\u00a0superuser account. In the above example, it was set to\u00a0<code>my-secret-pw<\/code>.<\/em>&#8220;,<\/li>\n<li>-d \/ &#8211;detach : &#8220;<em>Run container in background and print container ID<\/em>&#8220;.<\/li>\n<\/ul>\n<h2>Verify that mysql is running<\/h2>\n<p>Docker implements a command to know all processes actually running on Docker instance, by using the &#8220;docker ps&#8221; command. Let&#8217;s see what is returned by this specific command :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">CONTAINER ID    IMAGE           COMMAND                 CREATED       STATUS         PORTS       NAMES     \r\n902871f958c1    mysql:latest    \"docker-entrypoint.s    2 days ago    Up 34 hours    3306\/tcp    mysql-instance<\/pre>\n<p>Mysql is running, we will be able to query it using container ip or the link function !<\/p>\n<h1>Retrieving and deploying wordpress<\/h1>\n<h2>Retrieving wordpress image to Docker<\/h2>\n<p>Same way as mysql image, I used the pull command specifying wordpress :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker pull wordpress<\/pre>\n<h2>Deploying wordpress using Docker<\/h2>\n<p>WordPress image is retrieved, let&#8217;s deploy it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">docker run --name blog --link mysql-instance:mysql -p 9083:80 -d wordpress<\/pre>\n<p>Command line explanations :<\/p>\n<ul>\n<li>&#8211;link : from the official documentation (https:\/\/docs.docker.com\/engine\/userguide\/networking\/default_network\/dockerlinks\/#communication-across-links), &#8220;<em>Links allow containers to discover each other and securely transfer information about one container to another container. When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source.<\/em>&#8220;. With this parameter, my wordpress container will be able to consume data from my mysql container ! The mysql instance, in the wordpress container, will be knowned as &#8220;mysql&#8221;,<\/li>\n<li>-p \/ &#8211;publish : this argument specify which port I will query to query wordpress on port 80. I choose the port 9083 cause I have several applications using port 80.<\/li>\n<\/ul>\n<p>I just check my applications status using the &#8220;docker ps&#8221; command :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">CONTAINER ID    IMAGE           COMMAND                 CREATED              STATUS          \u00a0\u00a0\u00a0\u00a0PORTS               \u00a0\u00a0\u00a0\u00a0NAMES     \r\n6c70b48aef18    wordpress       \"docker-entrypoint.s    About an hour ago    Up About an hour\u00a0\u00a0\u00a0\u00a00.0.0.0:9083-&gt;80\/tcp\u00a0\u00a0\u00a0\u00a0blog \r\n902871f958c1    mysql:latest    \"docker-entrypoint.s    2 days ago           Up 34 hours       \u00a0\u00a03306\/tcp                mysql-instance<\/pre>\n<p>Both of them are running ! To finish the migration, I just have to take care about mysql datas from my old instance to my new one.<\/p>\n<h2>Migrating data from mysql<\/h2>\n<p>Before initiating the migration, I updated wordpress to the last version, same version deployed using Docker, so I was sure that the mysql tables will be identical. To export datas from my old mysql instance, I just used the mysqldump command :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">mysqldump --single-transaction -h localhost -u root -p  wordpress &gt; wordpress-dbbackup_20180203.bak<\/pre>\n<p>I have my backup, all I have to do is to inject it to my new instance :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">mysql -p -h localhost mysql-instance &lt; wordpress-dbbackup_20180203.bak<\/pre>\n<p>All my datas are up-to-date, accessing my web site on my new server adress giving me all my post and wordpress apparence, all is OK !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Few days ago, I changed my personal server, with all my data, to another one. On this new one, I choose to use Docker to deploy all my old stuff, including this web site. When investigating throught Docker, I thought &hellip; <a href=\"https:\/\/blog.xoupix.fr\/index.php\/2018\/02\/04\/deploying-wordpress-using-docker\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":11,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions\/144"}],"wp:attachment":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}