Skip to content
Logo Theodo

Set up MongoDB on a Travis container

Thibaut Cheymol2 min read

mongodb

The new Travis Docker infrastructure

Late December 2014, Travis announced the implementation of their new infrastructure based on Docker containers in order to improve build capacity, build start time, and resources usage. You can learn more about it on this very detailed article: Faster Builds with Container-Based Infrastructure and Docker

As we can read on the Travis documentation about how to use this infrastructure, adding one line only on our .travis.yml is necessary :

  sudo: false

Why simply not adding this line to my .travis.yml file?

As I had to use a specific MongoDB version that I couldn’t choose on the Travis apt-source-white-list, I was forced to install MongoDB as I found on this great article from Maxime Thoonsen and then fated to run my tests on the Travis legacy infrastructure

before_script:
  - sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  - echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  - sudo apt-get update
  - sudo apt-get install -y mongodb-org=2.6.6 mongodb-org-server=2.6.6 mongodb-org-shell=2.6.6 mongodb-org-mongos=2.6.6 mongodb-org-tools=2.6.6
  - sleep 15 #mongo may not respond immediatly
  - mongo --version

How to install it without sudo?

First, as we can find on the Travis documentation, we have to add this line at the beginning of our .travis.yml.

sudo: false

services:
  - docker
  - mongodb

Then, we specify the MongoDB version we want to use for our tests.

env:
  global:
    - MONGODB_VERSION=2.6.10

Finally, we can download the MongoDB archive we want and install it in a specific directory.

 before_install:
  - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-$MONGODB_VERSION.tgz
  - tar xfz mongodb-linux-x86_64-$MONGODB_VERSION.tgz
  - export PATH=`pwd`/mongodb-linux-x86_64-$MONGODB_VERSION/bin:$PATH
  - mkdir -p data/db
  - mongod --dbpath=data/db &
  - sleep 3

Congrats, you are done with it !

If you want to check if your build has been successfully executed on the container infrastructure, look for the following lines on your Travis build logs.

check

The directs improvements we have seen

First, as advertised by Travis, our builds that sometimes needed several minutes to start, now systematically start within less than 10 seconds.

Secondly, the build speed itself has also increased sharply as you can see on the samples above.

time

Liked this article?