Public | Automated Build

Last pushed: 2 years ago
Short Description
Docker image example using TDD with Serverspec and Travis CI.
Full Description

Important Note

The procedure described below is considered outdated. From now on, I recommend using the dockerspec gem for testing docker images.


Docker Tests Example

Docker image example using TDD.

The tests are implemented in Ruby, using the docker-api and serverspec gems.

The following features are currently implemented:

Note: This example installs a simple Apache & PHP application, but could be anything else.

Run the Tests

You can use the bundler Ruby gem to install all the requirements to run the tests:

$ gem install bundler
$ bundle install

You can now run the tests running RSpec:

$ rspec

Output example:

Randomized with seed 52614

Docker image run
  installs PHP version 5.6
  returns my web page
  Process "apache2"
    should be running
  File "/var/www/html/index.php"
    should be file
  Package "apache2"
    should be installed

Dockerfile build
  has the website path as workdir
  has PHP installed
  creates image
  runs apache in foreground
  exposes the port 80

Finished in 1.26 seconds (files took 0.20144 seconds to load)
10 examples, 0 failures

Randomized with seed 52614

See also the Travis CI output as an example.

Directory Structure

├── Dockerfile
├── .dockerignore: Files to be ignored by docker copy instructions.
├── Gemfile: File to install the gems required to run the tests.
├── .gitignore: Intentionally untracked files to be ignored by git.
├── LICENSE: Software license.
├── README.md
├── spec/
│   ├── build/: Tests to verify the built image.
│   ├── run/: Tests to verify the image running (Serverspec).
│   ├── serverspec_helper.rb: Helper file to setup Serverspec.
│   └── spec_helper.rb: Helper file to setup all the tests.
├── .travis.yml: Travis CI configuration file.
└── webapp/: Example application to install.

How to Run Docker in Travis CI

You can use a .travis.yml similar to the following:

language: ruby

sudo: true

before_script:
- source <(curl -sL https://raw.githubusercontent.com/zuazo/docker-in-travis/0.2.0/scripts/start_docker.sh)

script:
- bundle exec rspec

after_failure: cat docker_daemon.log
Using the Official Travis CI Docker Stacks

Since August 2015 you can try to use the official Docker service provided by Travis CI workers using the following .travis.yml:

language: ruby

sudo: required

services:
- docker

script:
- bundle exec rspec

Please, let me know if this has worked for you.

Change Dockerfile Location

You can set the DOCKERFILE_LOCATION environment variable to change the Dockerfile subdirectory (defaults to .).

For example, to run multiple tests:

language: ruby

sudo: true

env:
- DOCKERFILE_LOCATION=directory1
- DOCKERFILE_LOCATION=directory2

before_script:
- source <(curl -sL https://raw.githubusercontent.com/zuazo/docker-in-travis/0.2.0/scripts/start_docker.sh)

script:
- bundle exec rspec

after_failure: cat docker_daemon.log

Real-world Examples

Questions and Improvements

This is proof of concept used by myself in my projects. If you encounter any problems or have ideas for improvements, please open an issue!

Acknowledgements

This docker example does not contain anything new. It is based on multiple online sources. Mainly the following:

Thanks to all of you ;-)

Docker Pull Command
Owner
zuazo
Source Repository