Node.js Deployment; Docker, Dokku & Digital Ocean


DigitalOcean is a really nice cloud hosting company that offers excellent performance at affordable rates

Docker is an abstraction on top of LXC Containers. Docker introduces a workflow that makes operating containers straightforward and lots of fun.

Dokku is a tiny program that allows you to easily deploy your applications. It was built by progrium (from localtunnel) on top of docker, Buildstep, gitreceive, pluginhook, nginx, Heroku BuildPacks,& sshcommand

DIY

Register with Digital Ocean and add your SSH key

Create a new droplet using the latest Ubuntu making sure you select your ssh key. Go for a 1GB+ droplet

As soon as your server is ready you can connect via SSH

ana@local: ssh root@123.456.678.9

If you get prompted for a password you forgot to add your SSH key to the server. You can still login in using a password (it’s in your email). Connect to your server using the root password and add your key to ~/.ssh/authorized_keys

Installing dokku is easy

root@dinospace: wget -qO- \\    
 https://raw.github.com/progrium/dokku/master/bootstrap.sh \\  
 | sudo bash

Dokku works best with a domain so applications can respond to foo.dinosaurspaceships.org instead of dinosaurspaceships.org:29842

Add these A records to your domain making sure they point to your server. You can even do this in the DigitalOcean Control Panel if you configured your domain to use their name servers

Type  Host                      Answer         TTL    
––––––––––––––––––––––––––––––––––––––––––––—–———–  
A     dinosaurspaceships.org    123.456.678.9  30    
A     \*.dinosaurspaceships.org  123.456.678.9  30

If you really want to have fun doing this check out the dnsd and geoip modules and build your own dns server for fun and profit!!11!one!

You can now connect to your server using your domain name

ana@local: ssh root@dinosaurspaceships.org

Dokku uses the /home/dokku/VHOST file to store your domain name so make sure dinosaurspaceships.org is in there

root@dinospace: touch /home/dokku/VHOST    
root@dinospace: echo dinosaurspaceships.org > /home/dokku/VHOST

To give yourself (and others) permission to deploy use the sshcommand program

ana@local: cat ~/.ssh/id\_rsa.pub | \\    
  ssh root@dinosaurspaceships.org \\  
  "sudo sshcommand acl-add dokku username"

Don’t use ssh-copy-id: This would give users full access to your server and they wouldn’t be able to git push because it uses the git user

Deploy your first application: Tiny; a node.js based url shortener

ana@local: git clone https://github.com/dscape/tiny.git    
ana@local: cd tiny

Tiny responds to two kinds of HTTP requests

#  
\# minimize a url  
#  
POST /create&url=http://minimize.me
#  
\# get the redirect for a minimized url  
#  
GET  /hN1x

We can deploy tiny by git pushing from our machine to the server

ana@local: git remote add dinospace \\    
  dokku@dinosaurspaceships.org:tiny  
ana@local: git push dinospace master

On the server you can use docker to check if the process is running

root@dinospace: docker ps -a    
ID           IMAGE    
9f033c42189e app/tiny:latest

Shorten your first URL. What better pick than dinosaurspaceships.org?

ana@local: curl -X POST \\    
  dinosaurspaceships.org/create?url\=http://dinosaurspaceships.org  
f0x

Resolving the shortened version

**ana@local**: curl -I dinosaurspaceships.org/f0x    
HTTP/1.1 302 Moved Temporarily    
Server: nginx/1.2.6 (Ubuntu)    
Date: Sat, 03 Aug 2013 01:54:40 GMT    
Connection: keep-alive    
Location: [http://dinosaurspaceships.org](http://dinosaurspaceships.org)

Voilá!

Docker provides you easy access to logs

root@dinospace: docker logs 9f033c42189e    
/create?url\=http://dinosaurspaceships.org  
/f0x

Environment variables are commonly used to store secrets such as api keys or passwords. Dokku support this functionality out of the box

root@dinospace: touch /home/git/tiny/ENV    
echo "export FOO=BAR" >> /home/git/tiny/ENV

This is all for now

Written by Nuno Job

Originally published at blog.yld.io on March 26, 2014.


Written by YLDMarch 26th, 2014


Share this article

Find us

London - HQ

9 Dallington Street

London

EC1V 0LN

+44(0) 203 514 4678

hello@yld.io

Lisbon

Rua Ramalho Ortigão 8

3º Esquerdo

1070-230

Lisboa

Porto

Rua Sá da Bandeira 819

2º Esquerdo

4000-438

Porto