Building Docker images for Bitbucket Pipelines using Packer

 

From day one Hindsight has been practising Continuous Delivery (CD) and end-to-end performance has always a critical factor. In the last two years we have switched from running our own hardware for the CD infrastructure to using several different Cloud providers. We have never been satisfied with the performance of the various different Cloud CD providers so when Atlassian announced the beta of Bitbucket Pipelines we thought we would give it a try.

Our tech stack for Behave Pro has a mixture of Java, Scala, Node, JavaScript, MongoDB and ElasticSearch, and this is all built using SBT. Pipelines uses Docker containers for running builds, but the default image provider by Atlassian didn't include support for SBT or MongoDB (Required for our test suite) so we had to roll our own Docker image for our build.

Behave Pro is deployed by baking immutable virtual machine images, and we use a tool called Packer to do this. Without wanting to learn how to write Docker files we turned to Packer to build our docker images and then publish them to Bintray (All our artifacts are hosted on Bintray). The choice of Packer was made purely on the basis of we were already familiar with it but you still need to have docker installed locally.

Packer is configured through a simple JSON file that describe the builder to use (Docker in this case), any provisioning or configuration steps that needs to be done (e.g. Install SBT and MongoDB), and any post build steps like publishing the docker image to a repository.

This packer file will start a docker container using Atlassian's latest image, execute a series of shell scripts to install various packages we need in our build (You could use puppet, chef or ansible) and then publish the resulting docker image to bintray.

{
    "builders"    : [
  {
      "type"    :     "docker"    ,
         "image"  :   "atlassian/default-image"  ,
    "commit": true
  }
],
"provisioners": [
{
  "type": "shell",
  "scripts": [
    "sbt.sh",
    "packer.sh",
    "rpm.sh",
    "mongodb.sh"
 ]
}
],
"post-processors": [
  [
    {
    "type": "docker-tag",
    "repository": "hindsightsoftware-docker-docker.bintray.io/bitbucket-pipeline",
    "tag": "0.3"
   },
   "docker-push"
  ]
 ]
}

Just running “packer build build.json” will kick off packer and publish a new docker image ready for use in Bitbucket Pipelines. While you kick it off you might want to make a cup of coffee as the build will take some time.

The full project is available on Bitbucket.


You may also like…