{"id":2347,"date":"2016-02-25T16:11:06","date_gmt":"2016-02-25T14:11:06","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/wordpress-with-docker\/"},"modified":"2016-02-25T16:11:06","modified_gmt":"2016-02-25T14:11:06","slug":"wordpress-with-docker","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/wordpress-with-docker\/","title":{"rendered":"How to install WordPress with Docker"},"content":{"rendered":"\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress is one of the most popular content management software (CMS) due to its multitude of features and ease of use. However, setting up a new web host environment can be time-consuming especially if you need to do it often. Simplifying the installation process to a few fast\u00a0commands greatly reduces the time and effort required, this is where Docker comes in. Installing WordPress with <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/deploy-wordpress-with-docker-compose\/\">Docker<\/a> is a breeze, read ahead to find out more.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/wordpress-docker-white.png\" alt=\"WordPress with Docker\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Docker is a container platform that allows simple and fast software installations on any system and OS. It wraps the piece of software in a complete file system that includes everything it needs to run such as code, runtime, system tools and libraries. This allows anyone to package an application with its dependencies into a standardized building block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Docker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Installing Docker itself is already easy. Firstly run the usual update command for your system to make sure you have the latest source lists.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Debian and Ubuntu\nsudo apt-get update\n# CentOS\nsudo yum update<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check that you have the&nbsp;curl command line utility.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -V<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It comes preinstalled with most Linux distributions, but&nbsp;if it can not be found,&nbsp;install it manually with the appropriate command for your OS.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Debian and Ubuntu\nsudo apt-get install curl\n# CentOS\nsudo yum install curl<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use the command below to download and install Docker. The process requires root privileges so you will be asked for your <em>sudo<\/em> password on any non-root user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -fsSL https:\/\/get.docker.com\/ | sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Towards the end of the installation process, you will see a suggestion&nbsp;to add your username to the Docker users group. Doing this allows you to run Docker commands without needing to invoke <em>sudo<\/em> every time.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo usermod -aG docker&nbsp;&lt;username&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Log out and back in again after adding yourself to the Docker users group before continuing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can check&nbsp;that the installation was successful with the following test program:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run hello-world<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should&nbsp;see an output similar to the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Unable to find image 'hello-world:latest' locally\nlatest: Pulling from library\/hello-world\n...\nHello from Docker.\nThis message shows that your installation appears to be working correctly.\n...<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the command does not work immediately, restart the Docker service with the following and try to run the hello-world app again.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart docker<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Docker should now be installed and working correctly. Continue on below with the rest of the WordPress setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MariaDB&nbsp;in a container<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before installing WordPress with Docker you will need to have somewhere to store the data. MariaDB&nbsp;is a community-developed relational&nbsp;database management system and a drop-in replacement for MySQL. It is <a rel=\"noopener\" href=\"https:\/\/hub.docker.com\/_\/mariadb\/\" target=\"_blank\">officially available on Docker<\/a> and provides easy instructions with up-to-date images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start off by making a new directory where you wish to store the files for WordPress and MariaDB for example in your home directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir ~\/wordpress &amp;&amp; cd ~\/wordpress<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Downloading and installing a new MariaDB&nbsp;container can&nbsp;all be performed with a single command. Before jumping in check the required parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MariaDB Environment variables are marked in the Docker command with <em>-e:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>-e MYSQL_ROOT_PASSWORD=<\/em> Set your own password here.<\/li>\n\n\n\n<li><em>-e MYSQL_DATABASE=<\/em>&nbsp;Creates and names a new database e.g. wordpress.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Docker&nbsp;parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>\u2013name wordpressdb<\/em> \u2013 Names the container.<\/li>\n\n\n\n<li><em>-v \u201c$PWD\/database\u201d:\/var\/lib\/mysql<\/em> \u2013 Creates a data directory linked to the container storage to ensure data persistence.<\/li>\n\n\n\n<li><em>-d<\/em> \u2013 Tells Docker to run the container in the daemon.<\/li>\n\n\n\n<li><em>mariadb:latest<\/em> \u2013 Finally defines what to install and which version.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then run the command below while replacing the &lt;password&gt; with your own.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -e MYSQL_ROOT_PASSWORD=&lt;password&gt; -e MYSQL_DATABASE=wordpress --name wordpressdb -v \"$PWD\/database\":\/var\/lib\/mysql -d mariadb:latest<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">...\nStatus: Downloaded newer image for mariadb:latest\n23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If Docker was successful at creating the container, you should see a code&nbsp;at the end of the output similar to&nbsp;the example above. You can confirm that the MariaDB&nbsp;container is running by using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker ps<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check the status for your MariaDB&nbsp;install, it should show&nbsp;\u201cUp\u201d and the time it has been running like in the example output below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CONTAINER ID IMAGE          COMMAND                CREATED        STATUS        PORTS      NAMES\n14649c5b7e9a mariadb:latest \"\/docker-entrypoint.s\" 12 seconds ago Up 12 seconds 3306\/tcp   wordpressdb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Other useful commands for working with containers are \u2018start\u2019, \u2018stop\u2019 and \u2018remove\u2019.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker start&nbsp;&lt;container name&gt;\ndocker stop &lt;container name&gt;\ndocker rm &lt;container name&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can find out more about available commands and options for specific commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker --help\ndocker &lt;command&gt; --help<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Full command-line documentation is also available over at the <a href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/cli\/\" target=\"_blank\" rel=\"noopener\">Docker support page<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">WordPress with&nbsp;Docker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Applications in containers run isolated from one another in the userspace of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. To allow applications within different containers to work with one another Docker supports container linking.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress is also made <a href=\"https:\/\/hub.docker.com\/_\/wordpress\/\" target=\"_blank\" rel=\"noopener\">officially available on Docker Hub<\/a>, pull the image using the command below. When the version to download is not specified Docker will fetch the latest available.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker pull wordpress<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress container also takes environment variables and Docker parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-e WORDPRESS_DB_PASSWORD= Set the same database password here.<\/li>\n\n\n\n<li>\u2013name wordpress \u2013 Gives the container a name.<\/li>\n\n\n\n<li>\u2013link wordpressdb:mysql \u2013 Links the WordPress container with the MariaDB&nbsp;container so that the applications can interact.<\/li>\n\n\n\n<li>-p 80:80 \u2013 Tells Docker to pass connections from your server\u2019s HTTP port to the container&#8217;s internal port 80.<\/li>\n\n\n\n<li>-v \u201c$PWD\/html\u201d:\/var\/www\/html \u2013 Sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed.<\/li>\n\n\n\n<li>-d \u2013 Makes the container run on background<\/li>\n\n\n\n<li>wordpress \u2013 Tells Docker what to install. Uses the package downloaded earlier with the <em>docker pull wordpress<\/em>&nbsp;-command.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Run the command below while replacing the &lt;password&gt; as you did for the MariaDB container.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=&lt;password&gt; --name wordpress --link wordpressdb:mysql -p 80:80 -v \"$PWD\/html\":\/var\/www\/html -d wordpress<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then open&nbsp;your server\u2019s&nbsp;domain name or IP address in a web browser to test the installation.&nbsp;You should be redirected to the initial WordPress setup page at http:\/\/&lt;public IP&gt;\/wp-admin\/install.php. Go through the setup wizard and you are done.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/wordpress-setup.png\" alt=\"WordPress initial setup page\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you get an error linking your server\u2019s public IP address to the WordPress container\u2019s internal address, remove the failed container using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker rm wordpress<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Restart Docker and the database container, and also make sure no other service is already bound to port 80.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart docker\ndocker start wordpressdb<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then try creating the WordPress container again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations, you should now have a simple WordPress with Docker running in a container and an easy way to configure another one if needed. Before continuing on building your new WordPress site, make sure to pay attention to the security on your server. To find out more, check out our article <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/secure-linux-cloud-server\/\">How To Secure Your Linux Cloud Server<\/a>.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[241,277],"class_list":["post-2347","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2347"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2347\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2347"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}