Setup Your Deployment Using Dokku
Setup Your Deployment Using Dokku
When you just need a single server or few of them, Dokku does a great job by making it possible to push code to production simply.
Join the DZone community and get the full member experience.Join For Free
A true open source, API-first CMS — giving you the power to think outside the webpage. Try it for free.
Production deployment environments come in all sorts of variations nowadays. The configuration architecture is mainly influenced by the size of the application and the budget but also by the process flow and how easy it is to do the deployment. Quite often, at the beginning phase of application/business, it is quite reasonable that you might not need a full-fledged cloud deployment setup. This post assumes that you have small infrastructure requirements but you want a smooth deployment process. For a simpler scenario when you need a single server or few of them, Dokku does a great job by making it possible to push code to production with a simple
Dokku is a Heroku on your own infrastructure. It allows for easy management of the deployment workflow of your application by managing applications in Docker containers. It also uses an Nginx reverse proxy but as its configuration is managed by Dokku itself, you barely notice it. After you deploy your application, you can scale your application up and down with a single command. Through Dokku plugins, you can also create and manage database instances (e.g. Postgres or MySQL), schedule automatic backups to AWS S3, create Redis instances, and configure HTTPS using Let's Encrypt.
Some cloud infrastructure providers like Digital Ocean offer a ready-made server image of Dokku which you can instantiate in minutes and start using it right away. In this post, I will go through the process of installing and setting up your own Dokku environment. For this article, I'll suppose you have an Ubuntu Server with SSH enabled already in place. For your information, I have a virtual machine (VM) on VirtualBox running Ubuntu 16.04, but you can do the same on any instance of Ubuntu running anywhere. Off we go...
Step 1: Installation of Dokku
To do the installation, we follow these commands:
wget https://raw.githubusercontent.com/dokku/dokku/v0.11.4/bootstrap.sh sudo DOKKU_TAG=v0.11.4 bash bootstrap.sh
The first line will download the installation script and the second line will actually do the installation. Downloading the script will be fast, but the installation itself will take a few minutes (depending on the performance of your server). Please check this page to find the most recent version.
After the installation is successful, we can continue configuring Dokku. To start the configuration, we type the address of the server in the browser and the SSH setup page will load. If you are using a VM like me then please set the network setting of the VM to the bridged adapter and then get the IP address of it by running the
ifconfig command. When you type this IP address in your browser window (in my case it is http://192.168.178.60) then you'll see a page like this:
Here in the "Public Key" field, we should paste our public key (mine is at ~/.ssh/id_rsa.pub) and then click the Finish Setup button. As of this step, our Dokku is set and ready.
Step 3: Creating and Deploying an Application
Now that we have Dokku ready, we want to deploy our first app. Setting up the application is a one-time thing which we need to do from the server's console. After we've connected to our server using SSH, we can create and configure the application in two steps:
dokku apps:create name_of_appto create the app. We can verify that the app is created by running
dokku domains:add hello hello-world-app.comto configure the domain of the app.
In order to serve the app, Nginx needs to have a domain configured for the app. If you have a domain to configure, go for it. For this post, I will use a fake domain which I will map in my /etc/hosts file to the IP address of my VM. So for this example, I will use the domain, hello-wold-app.com.
Next, we need to set the git remote for the Dokku environment. We do this by running
git remote add remote_name dokku@dokku_server:name_of_app. Here:
- remote_name is the name we want to give to our deployment environment, this could be anything you like, e.g. production, staging, etc.
- dokku_server is the IP address or the URL of the Dokku server we just configured.
- name_of_app is the name we specified when we created the app.
Now that we have it ready, we can deploy our app by running
git push dokku_server. When the deployment is finished, you can test your application by visiting http://hello-world-app.com.
p.s. if you don't have an application that's ready to deploy, go ahead and download a hello world web application from my GitHub account.
Step 4 (Optional): Scaling Your Application
Now that we have our web application deployed, it is running in a single instance of a Docker container. For a production deployment, we probably would want to scale it to more instances to have better performance. As an optimal configuration, we would probably want to scale the app to as many instances as the number of processor cores we have. Assuming we have an 8 core processor in our server, we could scale our app to 8 instances by running
dokku ps:scale name_of_app web=8
After that, we can check the instances by running
dokku ls or
sudo docker ps.
When setting up new applications, I try to take the pragmatic approach and keep it as simple as possible. In my opinion, Dokku is a good starting point. It makes the deployment process dead simple as well as gives us the flexibility to scale it as we need. As the application starts facing a lot of traffic and this infrastructure starts to have a hard time coping with it, then I think about more advanced deployment workflows.
Published at DZone with permission of Arian Celina , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.