Cloud

Platform as a Service (PaaS) is a good way to focus on the app, rather than the infrastructure. At Heroku you can deploy your application in a few steps, without touching and maintaining the Operating System. You can focus on developing your application: you push the app to your code repository, and when you are ready to deploy, you push it by using 1 command to your dev/staging/prod servers.

If you still want to have control over the infrastructure, but you want your developers to be able to deploy easily, you can set up your own Platform as a Service, using your own (cloud) infrastructure. It's also a good alternative to the free Heroku Dyno, which recently went through a change. Free applications on Heroku now need to sleep for 6 hours.

In this article, we'll set-up a PaaS using Dokku on a freshly build machine. We'll use Amazon AWS's Free Tier to show you how easy and cost effective (free during the first year) it is to set it up. Let's get started.

Step 1

If you don't have an account already, open an Amazon AWS account at http://aws.amazon.com. When creating a new account, you're eligible for the Free Tier. Once logged in, create a new user or use the same user to generate an access key (documentation). Download and setup the Amazon CLI (CLI quick-start)

Step 2

Now we're going to start a virtual machine. We'll launch a t2.micro, which is one of the smallest machines, but is covered by the Free Tier Usage. Let's create a key first:

aws ec2 create-key-pair --key-name MyKeyPair --region eu-west-1

Don't forget to save the private key of the keypair in MyKeyPair.pem (called "KeyMaterial" in the output). Let's launch a t2.micro Ubuntu EC2 instance:

aws ec2 run-instances --image-id ami-47a23a30 --count 1 --key-name MyKeyPair --instance-type t2.micro --block-device-mappings "[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"DeleteOnTermination\":true,\"VolumeSize\":8,\"VolumeType\":\"gp2\"}}]"  --region eu-west-1

Optionally, you can launch your EC2 machine in a VPC, using a security group, by specifying --security-group-ids and --subnet-id. Once the instance is launched, you can find the public IP address in the AWS console or by typing aws ec2 describe-instances.

Step 3

On our new EC2 machine, log in using ssh -i MyKeyPair.pem -l ubuntu <public ip>. Let's install Dokku:

wget https://raw.github.com/progrium/dokku/v0.3.22/bootstrap.sh
sudo DOKKU_TAG=v0.3.22 bash bootstrap.sh

You can now follow the instructions on the terminal or by going to http://your-ec2-public-ip

Step 4

Now we're ready to deploy. Go to your git project folder with the app that you want to deploy. In my case I want to deploy the in4it website:

git remote add dokku dokku@ec2-public-ip:in4it
git push dokku master

If you want to add a (sub)domain to your application, you can use enter the following command on the server:

dokku domains:add myapp example.com

Step 5

Now the only thing that is left is to test our website / application. When we browse to the website http://www.in4it.io, it now shows our deployed app. If we would like to redeploy are app after doing a commit, we can do so by typing git push dokku master again.

Extra's

You can install plugins to enable pre-post deploy hooks, or to add a PostgreSQL database. The major missing piece is to have it working using a HA setup, which is not easily configurable at the moment of writing. If you want a full HA solution, you might take a look towards other technologies, like Elastic Beanstalk or Heroku.

About the Author

Ward has been a system administrator for more than a decade and has been working as a Consultant and Trainer for the last few years. Besides DevOps he is also into the latest Big Data technologies. Originally Belgian, currently enjoying life in London.