Home Build, Release, Dockerize and Run with Github, Travis CI and DockerHub
Post
Cancel

Build, Release, Dockerize and Run with Github, Travis CI and DockerHub

If you’ve read my previous post, Configuration Management for Microservices, I created two microservices one in Golang and the other in Java. Using the two, I’ll want to show how we can use Travis CI for the Build, Release and Run phase.

In a later article we’ll use our docker images with Kubernetes.

1. Building Docker Images From Github for Go and Java Repos

To build a docker image for a our source codes we’ll create Dockerfile. The Dockerfile is a set of instructions to build and properly describe a docker image. Here’s what some of the instructions mean :

Sample Dockerfile for Java
1
2
3
4
5
6
7
FROM openjdk:8
VOLUME /tmp
ADD target/config-server-1.0-SNAPSHOT.jar config-server.jar
RUN sh -c 'touch /config-server.jar'
ENV JAVA_OPTS="-Xms128m -Xmx256m"
EXPOSE 8888
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=docker -jar /config-server.jar" ]

The Go Dockerfile is a multi-stage approach to build light weight images explained into details here.

FROM: Image with its tag as build-base. This is mostly alpine linux because it’s light and allows debugging of containers. There are other options as well like scratch.

RUN: Is used to execute a shell command-line within the target system.

COPY: Used to copy files from the local file-system to target image.

ADD: Used to also copy files like COPY but with addition features.

ENTRYPOINT : Entrypoint sets the command and parameters that will be executed first in target system when it starts.

ENV : Used to define environment variables used in the target host.

CMD: Pass arguments to the ENTRYPOINT in the target system.

You can check out the docker documentation to read more on it and this on how to build build small size docker containers for Go

2. Setting up our CI server : Travis CI.

“Travis CI a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub”

Like the Dockerfile is a set of instructions to build a docker image, .travis.yml also contains a set of instructions for Travis CI to build and test the source hosted on Github.

In our case we’ll use Travis CI to test and compile our source and then package a docker image that can easily be run. Travis CI doesn’t store the docker images so we need to push to a docker repository. This can be a private one or a public one. For this example we’ll use the public DockerHub.

To build and test our project,which uses maven, we’ll add this.

1
script: cd $SERVICE_DIR && mvn clean verify

The maven goal test is intentionally left our because Travis CI does that by default once we specify java, I’m using the script because thats way let Travis CI building sub directories projects. This works because I’ve specified the sub directories as environment variables.

1
2
3
4
env:
  global:
  - SERVICE_DIR=config-server
  - SERVICE_DIR=message-summary

After successfully building the source we’ll need to package it using the docker configuration, Dockerfile. But first we’ll have to enable Docker for Travis CI

1
2
services:
    - docker

I wrote a bash script to handle the building and the uploading of the images. It uses the known docker build, docker tag and docker push commands to complete this task.

First of Travis CI, would need access to upload the packaged images. Travis CI has a simple system for not storing passwords in plain text which requires installing a travis gem sudo gem install travis. Encrypt your passwords and add them to your travis config file by cd-ing into the project folder then travis encrypt VARIABLE="secret-to-be-encrypted", confirm the repository name, it should return an encrypted text which you can add to your .travis.yml

1
2
3
4
env:
  global:
  - secure: "encrypted-dockerhub-username-1234"
  - secure: "encrypted-dockerhub-password-1234"

For more details on writing your travis configuration to read more on it

OK, now that we have a Docker image that is built by Travis, how do we use it? Well, we want to push the image to a Docker Registry for storage. Users can then pull the image from the registry to the machine where they will run the image.

Complete .travis.yml would look like this for a Java application
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sudo: required
language: java
jdk:
  - oraclejdk8
before_script:
  - mongo go_kafka_alert --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'  
services:
- mongodb
- docker
 
env:
  global:
  - secure: "encrypted-dockerhub-username-1234"
  - secure: "encrypted-dockerhub-password-1234"
  - SERVICE_DIR=config-server
  - SERVICE_DIR=message-summary

before_install:
  - chmod +x ./.travis/pushimage.sh

script: cd $SERVICE_DIR && mvn clean verify

after_success: 
  - bash ../.travis/pushimage.sh config-server
  - bash ../.travis/pushimage.sh message-summary
Check .travis.yml for the travis config for our Go application

Summary so we successfully setup an automated process using Travis CI, Dockerfile configurations to build an image and then successfully uploaded to DockerHub. Although the title is “Build, Release, Dockerize and Run with Github, Travis CI and DockerHub” we’re yet to actually run our service.
This would be continued in the next article with other things we can introduce in the pipeline to produce efficient results.



REFERENCES

https://12factor.net/

https://medium.com/@pierreprinetti/the-go-dockerfile-d5d43af9ee3c

This post is licensed under CC BY 4.0 by the author.