Over a million developers have joined DZone.
Platinum Partner

Playing with Dokku on Vagrant

· DevOps Zone

The DevOps Zone is brought to you in partnership with Librato.  Check out Librato's whitepaper on Selecting a Cloud Monitoring Solution.

As I said previously, it’s very easy to turn a Linux machine into Heroku-like server. But, before setting up a paying account on Amazon or Digital Ocean, it’s nice to just play with it locally. We will do just that by running Dokku on a virtual machine. We will set up a development environment and do a local deployment first, just to see some real features.

It does not require a complex environment to run Dokku locally. All you need is Git and Vagrant.

Preparing the Environment

Your box should contain a few things: Git (a GitHub account), VirtualBox and Vagrant. If you don’t have Vagrant installed, please do so now. It makes a lot of sense to keep that kind software on your machine.

Here you can find some instructions on how to do that.

Cloning Dokku

You should clone a Dokku repo locally. In your development folder, run:

> git clone git@github.com:progrium/dokku.git
> cd dokku

The Dokku repository already contains the Vagrant file, with all required configuration data.

Local Networking

Just for the sake of convenience, we will map the IP address (the one that the Vagrant machine is assigned to), to the dokku.me DNS name, which will be handy for testing.

› sudo nano /private/etc/hosts

Put the last two lines, as shown in my example:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##       localhost broadcasthost
::1             localhost
fe80::1%lo0     localhost        dokku.me        node-simple.dokku.me

Fire Up the Virtual Machine

In the dokku folder, you should run:

> vagrant up

It will start to prepare a new virtual environment for you:

 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 80 => 8080 (adapter 1)
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.

I had a problem with this step a few times, so if your machine could not be booted, try to run vagrant reload, which should help.

It will take about up to 20 minutes to fire up the virtual machine, install Git on it, and install Docker and Dokku. As soon as it’s done, it’s possible to access the machine with ssh.

The last thing you need to do is upload your ssh key to the Dokku server so you will be able to git push code there.

> cat ~/.ssh/id_rsa.pub | ssh root@dokku.me "sudo gitreceive upload-key alexanderbeletsky"

You can use the default Vagrant root password, which is "vagrant".

Now, just to check that everything is fine, simply access your machine with ssh:

> vagrant ssh

Then, check the version of Docker:

> docker -v
Docker version 0.6.1, build 5105263

The instance is ready for deployment.

Deploy to Dokku

If you're still there, you can just quit the Vagrant ssh session, and go into your folder with Node.js. I’ll be using a really simple app called Node-simple, an Express.js-based app that serves one HTML file that shows the NODE_ENV variable.

So, what you need to set up is a remote repository to push to:

> git remote add local-deploy git@dokku.me:node-simple

That’s all. You're ready for your first deploy, just push code to the machine with Dokku:

git push local-deploy master
› git push local-deploy master --force
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
-----> Building node-simple ...
       Node.js app detected
-----> Resolving engine versions
       Using Node.js version: 0.8.25
       Using npm version: 1.2.30
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
       npm WARN package.json application-name@0.0.5 No repository field.
       npm WARN package.json application-name@0.0.5 No readme data.
       npm WARN package.json send@0.1.0 No repository field.
=====> Application deployed:

To git@dokku.me:node-simple
   dd05aae..ac5b6da  master -> master

Setting Up the Environment

Every application requires an environment. It’s common practice to set up at least a NODE_ENV variable for Node.js applications. To do that, you need to create an ENV file inside the /home/git/node-simple folder.

› ssh root@dokku.me "echo export NODE_ENV="development" > /home/git/node-simple/ENV"

Now, let’s redeploy the application, change the version in package.json and push the code again.

› git push local-deploy master

Now, the application is ready to be accessed.

Accessing the Application

Open Chrome and hit http://node-simple.dokku.me, and you will see this response:

You can play with it a bit more by just changing some Node.js end-points code and redeploying. Each time, the new Docker instance is started and served on http://node-simple.dokku.me. The experience of deployment is like something you would have with Heroku.

Just looking at logs while the new application is deployed would give you a pretty good idea of what’s going on there.

So, your local Vagrant image will be at a good starting place, before you're ready to use Dokku on the cloud.


Published at DZone with permission of Alexander Beletsky , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}