Continuous Deployment - Jenkins , Capistrano And Docker.

How to integrate Capistrano with Docker for deployment?

Lets start with small introduction about 'Capistrano' .
Capistrano is a framework for building automated deployment scripts on remote node.Its an open source tool which is written in Ruby, but it can easily be used to deploy any language.
Basically capistrano installed to and runs from your working computer. When you command Capistrano to do something on a server, it connects to the server via SSH and works there using shell commands.
Lets see how the directory structure of capistrano looks like.If you are running capistrano first time ,you run below commands to initialise it,Which will create directory structure for you.
  $ mkdir  capistrano/
  $ cd   capistrano/
  $ cap install
     - mkdir -p config/deploy
     - create config/deploy.rb
     - create config/deploy/staging.rb
     - create config/deploy/production.rb
     - mkdir -p lib/capistrano/tasks
     - create Capfile 
     - Capified
To understand more about directory structure refer online document
Now lets start with Integrating Capistrano with Docker deployment.I assume that you have installed and configured below softwares on your computer .
Ruby : Install and configure Ruby and install Bundle.
Capistrano : Install and configure capistrano .
VirtualBox : Install and configure VirtualBox.
Docker : Install and configure docker on your Local and Vagrant box
Here i'm deploying sample NGINX container on docker container which is running on VirtualBox.
If you would like to spin up pre-docker installed VirtualBox ,clone my repo https://github.com/Prashanth-Pullaikodi/vagrant_docker_ansible and run below command .
  git clone git@github.com:PrashanthPullaikodi/vagrant_docker_ansible.git
  cd vagrant_docker_ansible
  vagrant up 
  vagrant status 
  vagrant ssh-config   # Copy the output to clip board, we will use this later on.
  vagrant ssh  #Check ssh connection working.
  exit
Once your VirtualBox is ready, clone below repo.
  git clone git@github.com:Prashanth-Pullaikodi/capistrano.git
 cd  capistrano
Modify Port number and Keys line based on your 'vagrant-sshconfig' command output.This configuration will be used to connect your virtualBox.
   vi config/deploy/vagrantbox.rb

set :ssh_options, {
  user: 'vagrant', # overrides user setting above
  port: '2222',
  keys: "~/.vagrant.d/insecure_private_key",
  forward_agent: 'yes',
  password: 'please use keys', } 
Create your own dockerHUB account https://hub.docker.com/?ref=login to push the image you build .
Now modify below line in ' config/deploy.rb ' file.
   vi config/deploy.rb
    set :remote_repo, "your_docker_hub_account_name/#{fetch(:application)}"
IMP: Modify below line and provide your GitHub account and commit and push all the changes you made to your repo.This repo will be checkout by Capistrano during deployment.
   vi config/deploy.rb
   set :repo_url, ENV["REPO_URL"] || "git@github.com:Prashanth-Pullaikodi/capistrano.git"
Note: Before running deployment, make sure that you login to your dockerHUB repo on both LOCAL and VirtualBox node.Becasue it connects to dockerhub to push and pull your newly created image.
   docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head 
    over to https://hub.docker.com to create one.
    Username (xxxx):

Deployment:

Now ready to build ,push and deploy docker container on vagrant box.
Note: Make sure you have docker running on your local machine as well. Becasue capsitrano will try to build your image on your local meachine and push to repository.
Go to your Capistrano directory.
  cd capistrano
  bundle exec cap vagrantbox  build  # This will build your nginx docker image.
  bundle exec cap vagrantbox push    # This will push your nginx docker image to your repo.
  bundle exec cap vagrantbox deploy  # This will deploy your Nginx app in your vagrant box.
  bundle exec cap vagrantbox -T      #List all the capistrano commands.
If you would like to have all the entire process would automated with Jenkins ,You can use the Jenkins file included with this.
Note : In-order to use Jenkins file Make sure that Jenkins Git and Pipeline Plugins installed.

Comments

Post a Comment

Popular posts from this blog

Docker ,MakeFile and Jenkins pipeline

SAN and NAS Interview questions

Infrastructure As Code - Terraform and AWS.