Building Docker Images on Travis CI

Date written Apr 6, 2016
Filed under Software Eng. in tech

  • Travis CI YAML
  • Notes
  • Assumptions

Docker has been the star of the recent times and I have recently been building a lot of Docker images, deploying them to both development and production (luckily yes!).

Travis CI is one of the most popular CI services and I have lately been using it extensively. It is awesome!

Travis CI YAML

Here is a simple .travis.yml to get started with Docker builds on Travis.

sudo: required
- docker
# IMPORTANT! Add your docker slug here (commit once)
- DOCKER_REPO_SLUG=activatedgeek/mariadb
# build latest image always
- docker build -t $DOCKER_REPO_SLUG:latest .
# build the tagged image
- if [[ $TRAVIS_TAG = $TRAVIS_BRANCH ]]; then docker build -t $DOCKER_REPO_SLUG:$TRAVIS_BRANCH .; else true ; fi
# IMPORTANT! Add the environment variables in Travis Build Environment (one time!)
# push to latest if master branch
- if [[ $TRAVIS_BRANCH = master ]]; then docker push $DOCKER_REPO_SLUG:latest; else true; fi
# push tag as well
- if [[ $TRAVIS_TAG = $TRAVIS_BRANCH ]]; then docker push $DOCKER_REPO_SLUG:$TRAVIS_TAG; else true ; fi

Here is how the build behaves under different scenarios:

  • Push to a branch other than master -> Builds image and exits

  • Push to branch master -> Builds the image tag latest

  • Push a tag -> Builds the image with corresponding git tag


As a reference project, have a look at

The corresponding Travis builds are available publicly at


  • The Docker build always uses the default Dockerfile present in the root of the project
  • The Docker image is always pushed to Docker Hub.

The implications of above two assumptions are easy to overcome if need be.