Day one @ dmalone.io

I launched this domain to host applications that I write, and to use it as a sandbox to learn new things. I’m using digitalocean.com for hosting, with Docker running on an Ubuntu host. My architecture is pretty basic:

malone_cloud_architecture

Data is being stored on a separate VM in order to keep things simple. Docker containers run my applications. An Apache HTTP server sits in front those apps, proxying ports so that all incoming traffic can come in on port 80, keeping things simple.

I will outline the steps that I took on DigitalOcean.com to stand up this environment. As indicated in the simple architecture diagram above, this environment will require two vms. I wanted to run WordPress in a Docker container, so for the purposes of this article let’s assume that’s what we’re configuring.

We will be creating two Droplets (vms); one for a MySQL server, and one to run applications in Docker. First, let’s create the MySQL vm by starting with a vanilla Ubuntu 14.04 Droplet:

Screen Shot 2015-07-09 at 4.18.54 PM

Once your new Ubuntu Droplet has been created, DigitalOcean will email you with root credentials. Once you get this, then you can ssh into your vm (ssh root@<your-ip-address>). After logging into your vm, execute the following commands to get mysql installed:

sudo apt-get update
sudo apt-get install -y mysql-server mysql-client

Once mysql has been installed, you’ll want to go ahead and set the root password for MySQL:

sudo mysqladmin -u root -h localhost password 'mypassword'

Now we need to create a database, a username, and a password for wordpress to use. For the purposes of this tutorial, we’ll use “wordpress” for all three of those. First, login via the mysql cli:

mysql -u root -p
*you’ll then be prompted to enter a password, which was created in the previous step

Next, create the wordpress database:

create database wordpress;

Now we need to create a user and grant that user the correct privileges on the new db:


CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'wordpress';
GRANT ALL PRIVILEGES ON wordpress . * TO 'wordpress'@'localhost';
flush privileges;

Now your database has been created, as well as a username and password to access it, but that username is restricted to accessing the database from the localhost only. We’ll need to create the Docker VM, get it’s IP address, and update the user info in MySQL when ready.

DigitalOcean streamlines the configuration of these vms through their Create Droplet interface. For the first vm, simply choose the Docker Application when creating your new Droplet instance:

Once your Docker Droplet has been created, DigitalOcean will email you with root credentials. Once you get this, then you can ssh into your vm (ssh root@<your-ip-address>).

The first thing that you’ll want to do is ensure that your Docker image is working correctly. Test your vm out by executing the following:
sudo docker ps

As long as you don’t see an error, your Docker server is ready to rock.

Now, before we configure out WordPress container, you’ll want to go back into the MySQL vm and update it with the IP address of your Docker server. ssh back into your MySQL VM and login to your mysql server as root or any user with appropriate privileges. Then, execute the following commands:

use mysql;
update db set Host='yourdockerhost-ipaddress' where Db='wordpress';
update user set Host='yourdockerhost-ipaddress' where user='wordpress';
flush privileges;
exit

This ensures that your WordPress app can actually login to the MySQL server on this VM. Now let’s ssh back into the Docker vm. Thankfully, there’s an officially supported WordPress Docker image, ready to download and launch. We’re going to pass in a few environment variables so that WordPress knows how to connect to the MySQL vm we created earlier. Execute the following command, replacing the placeholders with the values for your environment:

docker run --name wordpress -e WORDPRESS_DB_HOST=your-mysqlserver-vm-ip-address:3306 -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -p 8889:80 -d wordpress

If you noticed, I mapped the host port 8889 to the container’s port 80. This might seem a bit funny, but it makes sense once you want to run multiple applications, all on port 80, just with different paths. To make this possible, we’ll stand up a proxy server in front of the applications running in Docker. While still logged into the Docker vm, run the following commands to install the Apache HTTP server and the necessary Apache modules:

sudo apt-get install -y apache2 libapache2-mod-proxy-html libxml2-dev

Once those packages are installed, run the a2enmod command, which will ask you which Apache modules to load. I only enabled the following mods: proxy proxy_http rewrite deflate headers proxy_html. Once those are installed, we simply need to edit the file named /etc/apache2/sites-enabled/000-default.conf. This is what my file looks like:



ServerName dmalone.io
ServerAdmin admin@admin.com
DocumentRoot /var/www/html

ProxyPreserveHost On

ProxyPass / http://localhost:8889/
ProxyPassReverse / http://localhost:8889/

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

After updating your Apache site configuration you must restart the server for the changes to take effect: sudo service apache2 restart. Assuming everything went well, you should be able to access your WordPress instance at http://your-docker-host-ip-address/.

Adding new applications is as easy as configuring new Docker containers, and updating the Apache site configuration to forward the correct URIs into the correct ports. While not perfect for hosting more than one application that expects to run at the root context, this is a decent starting configuration for hosting your own apps in Docker.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.