← All Articles

Habitus: A Build Flow Tool for Docker

Khash SajadiKhash Sajadi
Feb 23rd 16Updated Jun 13th 19
Open Source
Features

habitus-a-docker-build-flow-tool

Here's what it takes to build a Docker image: a Dockerfile!

Dockerfiles are simple...

Dockerfile has a simple and understandable format. Choose a base for your image, add files, run commands and you're good to go. Here's an example:

FROM ubuntu
RUN apt-get -y install python
ADD ./a-file /app/a-file

...perhaps too simple?

Often times building a real world application takes much more than following simple ADD and RUN steps in a step-by-step fashion. You might want to:

  • Pull code from a private git repository and therefore need your private SSH key in the image during build
  • Add compile-time libraries to your image but don't need them in run-time

Also, how about those "post-build" steps like uploading your compiled executables to S3 or resetting your exception handling system after a successful build and release?

Introducing Habitus

Habitus is our answer to these issues. We built it for ourselves and are releasing it as open source for everyone to enjoy, contribute and make their own.

What is Habitus?

Habitus is a build flow tool for Docker. It lets you combine multiple Dockerfiles into a complex build and deployment flow.

Combining your Dockerfiles with a single yaml file called build.yml gives you a lot of power and flexibility. Here's an example of a build.yml:

build:
  version: 2016-02-13 // version of the build schema. 
  steps:
    - builder:
      name: builder
      dockerfile: Dockerfile.builder
      artifacts:
        - /go/src/github.com/cloud66/iron-mountain/iron-mountain
        - /go/src/github.com/cloud66/iron-mountain/config.json
        - /go/src/github.com/cloud66/iron-mountain/localhost.crt
        - /go/src/github.com/cloud66/iron-mountain/localhost.key
      cleanup:
        commands:
          - rm -rf /root/.ssh/
    - deployment:
      name: ironmountain
      dockerfile: Dockerfile.deployment
      depends_on:
        - builder
    - uploader:
      name: uploader
      dockerfile: Dockerfile.uploader
      depends_on:
        - ironmountain
      command: s3cmd --access_key=_env(ACCESS_KEY) --secret_key=_env(SECRET_KEY) put /app/iron-mountain s3://uploads.aws.com

Now we can run this with Habitus:

$ habitus -f build.yml -e ACCESS_KEY=$ACCESS_KEY -e SECRET_KEY=$SECRET_KEY

Here's what's going to happen:

  1. Habitus runs each step with the Dockerfile specified in that step.
  2. If any artefacts are specified, they'll be copied from the built image onto the work directory, so they'll be available to the next steps.
  3. Any cleanup commands will run after build. This will result in 'squashing' the image, therefore removing any traces of the unwanted layers. This is particularly useful to get rid of compile time packages or private SSH keys.
  4. If a command is specified, it will run in the build container. This can be a step to upload build artefacts to a we server.

What else can it do?

Habitus can do more than just running build steps. It can:

  • Automatically detect that a step uses another step as the base image FROM and amend the image tags
  • Build complex build dependency tree and run independent steps in parallel
  • Pass environment variables into the build

Multiple instances of Habitus can run in parallel on the same build using unique session IDs. This is useful if you would like to use Habitus for your automated build server.

Where can I find out more?

You can read more about Habitus on its website.

Is it free?

Yes! Habitus is free and open source.

Who's behind Habitus?

Habitus is an open source project sponsored by Cloud 66. Fork, modify and conquer, then let us know what you think!

Watch our talk about Habitus at the DigitalOcean Meetup in New York.

IMAGE ALT TEXT HERE


Try Cloud 66 for Free, No credit card required