Loading...

Use Travis CI in Github to build and deploy to dockerhub

I love reveal.js - The HTML Presentation Framework. Attending at the Javaland 2016 Conference I saw a awesome usage of reveal.js within a docker container in the Docker Patterns Talk by Roland Huß. Curious and eager to know I explored his github account. Mr. Huß offers the basics in the docker-reveal repository. Using github for docker builds is a great idea. Then I started to play around with docker myself, mostly to maintain and ease administering multiple elasticsearch nodes in a cluster. I felt using github offers me the opportunity to use Travis CI to build the docker image and deploy it to dockerhub - the docker image storage. Is was easier than I thought and is much better than building it manually everytime. This post covers the progress and results.

Basic Steps

The general roadmap:

  • Create a public docker repository at dockerhub, to push the docker images to it :-)
  • Create a github repository, to maintain the Dockerfile and source
  • Setup continous build, use in github Travis CI to build and push the docker image to dockerhub
  • Run (pull the docker image from dockerhub) and have fun.

Dockerhub

Dockerhub offers unlimited public repositories free of charge for storing the docker images of your software projects. If your don’t want to expose your software to the public choose a private repository.

Github

GitHub is a Git repository hosting service that provides free repositories for the public, mostly Open Source development. I simply take my existing docker project, improved-docker-elasticsearch, a custom tailored elasticsearch instance.

Travis CI

The Travis CI integration is very simple. You only need to create .travis.yml file, that contains the build definition and will be explained in detail.

Prior conditions

We want use the docker service in Travis CI. Docker runs as root, so you need sudo permissions.

sudo: required
services:
  - docker

Build the docker image

I put the build instruction into before_install and check if the build was correctly build in the script section.

before_install:
  - docker build -t cinhtau/elasticsearch .
script:
  - docker images cinhtau/elasticsearch

Deploy to Dockerhub

The last section, contains the push instruction to dockerhub, only if the image was correctly build.

after_success:
  - if [ "$TRAVIS_BRANCH" == "master" ]; then
    docker login -e="$DOCKER_EMAIL" -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
    docker push cinhtau/elasticsearch;
    fi

The environment variables are setup in the repository settings within Travis CI (see screenshot). travis-ci-docker-variables

Run it anywhere

Having the docker image in dockerhub, I can use my elasticsearch image on any computer that can pull the image from the dockerhub repository and has the right requirements to run the image. No need to build it locally anymore :-) .

tan@omega:~$ sudo docker pull cinhtau/elasticsearch
Using default tag: latest
latest: Pulling from cinhtau/elasticsearch
8ad8b3f87b37: Already exists
751fe39c4d34: Already exists
b165e84cccc1: Already exists
acfcc7cbc59b: Already exists
04b7a9efc4af: Already exists
b16e55fe5285: Already exists
8c5cbb866b55: Already exists
e4412b99da57: Pull complete
60fa44913e1f: Pull complete
593bcc8c9106: Pull complete
b065e784dc32: Pull complete
10cc1e0e4dd9: Pull complete
093a531dbb6f: Pull complete
Digest: sha256:c90986a7f3799cdabc7c62ef7f576ed97a3d6648fb5c80a984312b26ec0375ea
Status: Downloaded newer image for cinhtau/elasticsearch:latest