In my
last article I mentioned Dokku as an important component of our infrastructure and today I want to reveal this topic in more detail.
Dokku is a simple Ubuntu server transformation tool in mini-Heroku. After installing dokku, you get the opportunity to do:
$ git push production master
for many popular platforms (Node.js, Java, PHP, Python etc). As a result of the deployment process, there is a running application, which you can immediately access via
http/https
.
')
How it works?
If you open
the project repository , you can see in the description the line
“Docker powered mini-Heroku in around 100 lines of Bash” - about 100 lines of bash code that imitates the work of Heroku. This is a rather “easy” implementation, as for such a big problem that it solves.
Everything is explained by the fact that Dokku is standing on the shoulders of such technologies as:
Docker ,
Heroku Buildpacks ,
Nginx ,
Git .
Docker
The first and perhaps the most important component is Docker.
Docker solves the problem of containers.
Containers are a fresh look at application deployment, in which virtualization is considered a classic. Docker containers have a number of advantages compared to virtual machines, such as the machine's bootstrap time, the size of the image to run, the versioning of the images (with incremental changes), as well as the overall
index , with the images available for use.
Inside the container, you can start a process that will be completely isolated from the external environment, with its operating system, file system and network interface. At the same time, from the same image, you can run arbitrarily (as long as the machine has enough resources) processes. You can “inherit” images from each other, for example, if we have an Ubuntu server image, say 1GB, but we want to make our own with MongoDB, the size of the new image will not be 1.3GB but 300MB.
Docker files allow you to “construct” the desired image in a declarative form, which you can “assemble”, put into an index, then distribute it to the necessary machines and run them there. At the same time, Docker ensures that the processes launched inside the container will work in exactly the same way, regardless of the specific machine.
Docker images are immutable. Those. any, even the most destructive action of the type
rm -rf /
, will not bring him harm, if the changes are not zakomichny.
Docker gathered a very productive community around itself, as well as many large companies like
Yandex ,
Red Hat ,
Facebook ,
Spotify are adopting this technology.
How does Dokku use it?
First of all, Dokku uses its base image, the so-called.
Buildstep . From his
docker file , it is clear that this is
ubuntu:quantal
, on which a number of necessary
packages are installed, as well as
build packs mainly from Heroku, but also some from the community (for example, for Perl, Dart, Static Apache etc.)
Inside the base image there is a script
builder , whose task is to “search” for the appropriate build for your application and launch it.
Heroku Buildpacks
Build packs, this is a set of walking or ruby ​​scripts, typically consisting of several files,
detect ,
compile ,
release . The task of which is to detect whether the application corresponds to the specified type (usually according to the files in the project root, Gemfile - Ruby, package.json - Node.js, etc.), compile and release the application, respectively.
The build pack launched inside the container creates all the necessary runtime (downloads the correct Node.js version, or OpenJDK ..), installs all the dependencies needed by the application (npm, maven, pip ..) By the time it finishes, the application will be ready to start .
If you used Heroku, then all this output, which is visible on the screen when you push the application in Heroku, is the result of the buildpack.
Nginx
Docker has the ability to map network ports that the application uses inside the container to the machine’s external port. Dokku passes the port number 5000 into the application. The external one is taken from the port range above 49200.
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a60c2af71770 app/app:latest /bin/bash -c /start 30 hours ago Up 30 hours 0.0.0.0:49264->5000/tcp prickly_curie 9c3c58b649df app/likeastore.com:latest /bin/bash -c /start 47 hours ago Up 47 hours 0.0.0.0:49253->5000/tcp sad_lumiere 1b55d9087d23 app/tour:latest /bin/bash -c /start 8 days ago Up 8 days 0.0.0.0:49228->5000/tcp suspicious_wozniak f700b5db1100 app/demo:latest /bin/bash -c /start 2 weeks ago Up 2 weeks 0.0.0.0:49159->5000/tcp sleepy_heisenberg 4df87e09611d app/analytics:latest /bin/bash -c /start 2 weeks ago Up 2 weeks 0.0.0.0:49153->5000/tcp tender_curie
Traffic from / to the container proxies
Nginx . It does not need to be configured, Dokku will do it himself. For all applications, it creates a configuration file, something like this:
upstream app { server 127.0.0.1:49264; } server { listen [::]:80; listen 80; server_name app.likeastore.com; return 301 https://$host$request_uri; }
Those. uses the
upstream module for traffic from the container to port 80 of the server.
Git
Many people know Git as an excellent version control system. The range of using Git is wider than just version control, many people call Git a new FTP. It is git that is used as a source transport to the server.
Dokku uses
git-hooks
, and after the sources are “pushed” to the server, it runs the
dokku
script. This is actually the same “around 100 lines of bash” script, whose task is to create a new docker image from the base (buildstep), launch the “builder” script to initialize the environment, launch the application itself and restart nginx.
Paying attention to the fact that with each push the application is deployed in a “clean” environment, in a new container created specifically for it.
Installation
Everything is described in some detail in the project
documentation . The Bootstrap script will install the necessary dependencies - git, nginx, docker, etc ... And also install the base
buildback
image.
Configuration
After installing Dokku on the server, you need to do 3 things.
1. Upload your ssh public key to the server so that you can do git push.
$ cat ~/.ssh/id_rsa.pub | ssh root@yourserver.com "sudo sshcommand acl-add dokku progrium"
2. In the .git / config of the application you need to configure the remote brunch
[remote "staging"] url = dokku@stage.likeastore.com:app-stage.likeastore.com fetch = +refs/heads/*:refs/remotes/deploy/* [remote "production"] url = dokku@likeastore.com:app fetch = +refs/heads/*:refs/remotes/deploy/*
Note that the names - with a fully qualified name, such as
app-stage.likeastore.com
, the application will run on
app-stage.likeastore.com
app-stage.likeastore.com
, and the base name, such as
app
, on
app.likeastore.com
app.likeastore.com
.
3. In the project root, make a
Procfile
file that contains instructions for launching the application for Node.js
web: node app.js
After this, the application is ready for deployment.
Deployment
Everything is simple, as stated above:
$ git push production master
As a result, you will see what looks like the following output:
› git push staging development:master -----> Cleaning up ... -----> Building app-stage.likeastore.com ... Node.js app detected -----> Requested node range: 0.10.x -----> Resolved node version: 0.10.25 -----> Downloading and installing node -----> Restoring node_modules directory from cache -----> Pruning cached dependencies not specified in package.json -----> Installing dependencies -----> Caching node_modules directory for future builds -----> Cleaning up node-gyp and npm artifacts -----> Building runtime environment -----> Discovering process types Procfile declares types -> web -----> Releasing app-stage.likeastore.com ... -----> Deploying app-stage.likeastore.com ... =====> Application deployed: https://app-stage.likeastore.com To dokku@stage.likeastore.com:app-stage.likeastore.com 77008a6..99dfe55 development -> master
After the first deployment, on demand, you can set up
environment variables for the application and
support SSL .
Plugins
The basic Dokku can be considered rather limited, but it expands perfectly with plug-ins.
Plugins are divided into several types,
datastores - with ready-made storage deployment solutions (MariaDB, PostgreSQL, MongoDB etc.),
process managers - to support
process managers (Circus, Shoreman etc.) and
other - all sorts of useful things (Bower, Grunt, Elasticsearch, SSH Deployment keys).
Our experience of using
Dokku in
Likeastore turned out to be very positive, which I sincerely wish you!
Shl. The network has a
video of my speech on the topic of Dokku, there are some details.