Docker recipes- basic continuous integration

The process of building docker images should be integrated into your CI pipeline.

The obvious approach

A standard solution is to setup an automated build between your image registry and your source control. The docker hub registry currently supports this with bitbucket and github.

A different approach

A basic solution is to run docker build with these considerations:

  1. Use the local git repo HEAD's SHA as a tag.
  2. Ensure the tag correctly represents the repository state, done by verifying nothing is in staging area or is untracked.

Combining the above into a shell script:

1
2
3
4
5
6
#!/usr/bin/env bash
image_name="foo/boo"

[[ -z $(git status --porcelain) ]] &&
docker build -t "${image_name}":$(git rev-parse --verify --short HEAD) . ||
echo 'oops looks like you need to git status and clean up'

Now integrate this script with your local build tool. For example, in NodeJS package.json:

1
2
3
4
5
6
7
8
9
10
11
{
"author": "foo",
"name": "boo",
"scripts": {
"publish": "./publish.sh",
"build": "# build",
"test": "# test",
"prepublish": "npm run build",
"prebuild": "npm run test",
}
}

Why

This method is handy if either:

  • You want to work locally.
  • You're still figuring which image registry you want to work with.
  • You just want to test out a project with basic continuous integration before advancing to a complete solution.