Deploy your application to Digital Ocean using Docker and Bitbucket Pipelines

Agyenim Boateng
4 min readMay 19, 2020

--

credit: https://blog.inedo.com/best-deployment-automation-tools

In this article, I’ll show you how to build a CI/CD pipeline for your application using bitbucket pipelines to deploy to a digital ocean droplet.

what we’ll be using.

Quick Workflow Overview

When our team of developers proposes a change to our application, they submit a pull request, that pull-requests get tested, reviewed, and then merged to the master branch. whenever a merge to the master branch is done,

  1. we’ll build the docker image tagging it with the latest commit id.
  2. we’ll push the latest image to docker hub
  3. deploy the latest image to the droplet.

DIGITAL OCEAN SETUP

Before we get started, you’ll have to create a digital ocean account using this link and get $100 free credit. (I also get something in return).

  1. create a project.
  2. add a docker droplet to the project you just created.
create a docker droplet on digital ocean

BITBUCKET SETUP

before we start, let’s enable pipelines on our repo.

  1. click on pipelines.
  2. choose docker.
  3. click Commit file to enable pipelines.
enable pipelines on your repo.

Dockerfile

this is a basic Dockerfile to build and run our node app.

next, create a bitbucket-pipelines.yml file in the root of your project.

Let’s examine the file above.

  1. Whenever a new commit is made (to any branch), we install npm packages and run tests.
  2. when a new commit is made to master branch, we execute 2 steps.

you can validate your pipeline config using this validator.

Step 1

named Deploy to Registry,this will build the docker image using the docker file above and push it to your docker hub account under the repository your_name/project_name with a tag of the commit id. This becomes useful when a particular commit causes an issue in production, we can quickly deploy the last working commit of the application.

Step 2

named Deploy to Droplet , this step is responsible for deploying the Docker image to the digital ocean droplet. This will ssh into your droplet and run the commands listed under the COMMAND variable.

Repository Variables.

Bitbucket gives us a way to set variables that act like environment variables for our pipeline. This allows us to securely store sensitive variables our pipeline will need. Bitbucket provides a set of default variables.

In the pipeline above, we set the following variables.

  1. DOCKER_HUB_USERNAME: your docker hub login name
  2. DOCKER_HUB_PASSWORD: password for docker hub
  3. SSH_SERVER: IP address of the droplet we created earlier on digital ocean
  4. SSH_USER: the ssh user to the digital ocean droplet. usually root
  5. SERVER_PORT: the port which our node application will be running on. this should be the same as the port exposed by the docker file. 3030 in this example
  6. CONTAINERS_TO_STOP

to set repository variables, click on Repository Settings > Repository variable on the repository. lean more about repo variables here

set repository variables

SSH AUTHENTICATION

This final step will allow the pipeline ssh successfully into your droplet.

  1. go to Repository settings > ssh keys
  2. copy and paste your droplet IP into the Host address input under the Known host section and click on fetch
  3. the Fingerprint will be fetched automatically.
  4. Once the fingerprint is added, click on Add host.
adding your server ip

5. generate a new ssh key on the same page.

6. copy the public key, ssh into your droplet and add the public key to the authorized_keys file located at ~/.ssh/authorized_keys in your droplet.

That’s it. anytime you push to any branch on your repository, the Build and Test step of your repo should run.

build and test.

Create a pull request and merge to master. if everything worked,

  1. you should see the image pushed to your docker hub account
  2. the image deployed to your droplet on http://droplet_ip_address:3030
deploying to master

find full source code in the repo here

--

--