Hi everyone, today we’ll learn how to install Docker Compose and deploy WordPress in Ubuntu 18.04 LTS “Bionic Beaver”. Docker is a free and open-source project that provides an open platform to pack, ship, and run any application anywhere. It automates the deployment of applications inside the containers by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux.
Docker uses the isolation features of the Linux kernel such as cgroups and kernel namespaces to allow multiple containers to run within a single Linux instance independently of the Linux distribution. It can be run anywhere, anytime from small home computers to high-end servers.
Docker Compose is great for development environments, staging servers, and CI. It provisions a platform for defining and running multiple container applications with Docker. In Compose, we define a multi-container application in a single file, then it spins our application up with a single docker-compose command.
Here, in this tutorial, we’ll be first installing the latest version of docker and compose then we’ll go for deploying a WordPress installation. Here are some easy and simple steps that we need to follow.
1. Installing Docker Engine
First of all, we’ll gonna install the latest release of Docker Engine in our linux machine. As we are running Ubuntu 18.04 LTS “Bionic Beaver” in our box, we’ll need to run the following command in a terminal or a shell under sudo or root access.
$ sudo wget -qO- https://get.docker.com/ | sh
Then, we’ll create a docker group and add our user into it so that our user will have full permission to execute docker commands.
$ sudo usermod -aG docker arun
After the above process is completed, we’ll wanna check whether docker engine is installed correctly or not. To do so, we’ll run a hello world container out of the box.
$ docker run hello-world
2. Installing Docker Compose
After Docker Engine is successfully installed in our Ubuntu box, we’ll go for the installation of the latest release of Docker Compose in our machine. To do so, we’ll need to run the following commands in a terminal or a shell.
$ curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Note: While running the above command, if you get an error similar to “bash: /usr/local/bin/docker-compose: Permission denied”. We’ll need to run the above commands from root mode. To get into root mode, we’ll need to run sudo -i then enter the above two command. After those two commands are executed, we’ll run exit command to exit from root mode.
As Docker Compose is available as python package, we can also install it by running the following pip command.
$ sudo pip install -U docker-compose
Next, we’ll check whether Docker Compose is successfully installed in our machine or not. To do so, we’ll run the following command to check the version of the compose installed.
$ sudo docker-compose --version
docker-compose version: 1.3.1
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
3. Downloading and Extracting WordPress
As docker engine and docker compose are successfully installed in our Ubuntu box, we’ll now go for the deployment of WordPress. Here, we’ll download and extract the latest version of WordPress by running the following command.
$ curl https://wordpress.org/latest.tar.gz | tar -xvzf -
The above command will download the latest build of wordpress and extract it under the same directory where the command is executed.
4. Creating Docker and Compose files
After we have our wordpress extracted, we’ll need to get into the wordpress directory. Then, we’ll gonna create a Dockerfile which will build an image defining a container with php and wordpress. To do so, we’ll run the following command.
$ cd wordpress; nano Dockerfile
Then, we’ll add the following lines into the file.
FROM orchardup/php5
ADD . /code
After done, we’ll save and exit the file.
Next, we’ll create an yml file named docker-compose.yml which will contain the configuration to run a web service and a separate MySQL instance. So, we’ll use nano text editor to create that docker-compose.yml file.
$ nano docker-compose.yml
Then, we’ll add the following lines into the file.
web: build: . command: php -S 0.0.0.0:8000 -t /code ports: - "8000:8000" links: - db volumes: - .:/code db: image: orchardup/mysql environment: MYSQL_DATABASE: wordpress
5. Creating WordPress Config files
Now, we’ll create two configuration files for WordPress. First file wp-config.php is the standard configuration file to connect WordPress database with the db container. And the second file router.php is the configuration file that will tell PHP’s built-in web server how to run WordPress. So, first we’ll create the wp-config.php file using a text editor.
$ nano wp-config.php
Then, we’ll add the following lines into the file.
<?php define('DB_NAME', 'wordpress'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_HOST', "db:3306"); define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); define('AUTH_KEY', 'put your unique phrase here'); define('SECURE_AUTH_KEY', 'put your unique phrase here'); define('LOGGED_IN_KEY', 'put your unique phrase here'); define('NONCE_KEY', 'put your unique phrase here'); define('AUTH_SALT', 'put your unique phrase here'); define('SECURE_AUTH_SALT', 'put your unique phrase here'); define('LOGGED_IN_SALT', 'put your unique phrase here'); define('NONCE_SALT', 'put your unique phrase here'); $table_prefix = 'wp_'; define('WPLANG', ''); define('WP_DEBUG', false); if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/'); require_once(ABSPATH . 'wp-settings.php');
Next after saving the above file and exiting the text editor, we’ll create our another file router.php using our favorite text editor.
$ nano router.php
Now, we’ll gonna add the following lines in it.
<?php $root = $_SERVER['DOCUMENT_ROOT']; chdir($root); $path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/'); set_include_path(get_include_path().':'.__DIR__); if(file_exists($root.$path)) { if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/') $path = rtrim($path,'/').'/index.php'; if(strpos($path,'.php') === false) return false; else { chdir(dirname($root.$path)); require_once $root.$path; } }else include_once 'index.php';
Then, we’ll save this file also and exit.
6. Deploying Containers
After everything is done as expected. We’ll now go for deploying our wordpress instance using the above configuration files. To do so, we’ll need to run the following command inside the wordpress directory where our newly created 4 config files are located.
$ docker-compose up
This will pull and build the needed images, and then start a web and database containers. Next, we can visit WordPress site using our web browser at port 8000 as assigned by the above docker-compose.yml file.
After setting up the required information for our WordPress site, we’ll be welcomed by the WordPress home page.
Conclusion
Docker Compose is a part of the Docker Project which was previously known as Fig. Using Docker Compose, we can easily run WordPress in an isolated environment built with Docker containers. Compose makes the deployment and linking of multiple containers very easy and fast. It consists of many more options which will lead us for further advanced configurations. Here, in this tutorial, compose made WordPress deployment pretty fast. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you! Enjoy 🙂
Kool Arpan is a tech enthusiastic who loves blogging and writing articles on technology. He loves sharing knowledge to the others by any means and medium.
Great stuff. If you want to build a High Availability WordPress installation I would recommend installing the NGINX by Jwilder – https://registry.hub.docker.com/u/jwilder/nginx-proxy/
Hi Brtian,
Sorry for late response. I am happy that you liked our article. Thanks very much for your suggestion.
I hope you enjoyed our other articles too. If you liked them, please help us by sharing the posts in your circle.
Thanks. Stay Tuned with our upcoming articles 🙂