The procedure described below is considered outdated. From now on, I recommend using the dockerspec
gem for testing docker images.
Docker image example using TDD.
The tests are implemented in Ruby, using the docker-api
and serverspec
gems.
The following features are currently implemented:
- Test the built image using
docker-api
. - Test the image running using Serverspec.
- Run the tests inside Travis CI. See the .travis.yml file.
- Check the tests with
should_not
gem to avoid redundant test descriptions. - Run the tests in random order.
Note: This example installs a simple Apache & PHP application, but could be anything else.
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.
├── 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.
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
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.
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
-
chef-local image (.travis.yml, spec/, Gemfile): Runs a Travis CI build for each image tag.
-
keywhiz image (.travis.yml, spec/, Gemfile): Runs one Travis CI build.
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!
This docker example does not contain anything new. It is based on multiple online sources. Mainly the following:
- https://github.com/tcnksm-sample/test-driven-development-dockerfile
- https://coderwall.com/p/5xylsg/tdd-for-dockerfile-by-rspec-severspec
- https://github.com/voidlock/travis-docker
- https://gist.github.com/masonforest/194e0cb6bb16c88e21a0
- https://robots.thoughtbot.com/tdd-your-dockerfiles-with-rspec-and-serverspec
- https://github.com/lukecyca/travis-docker-example
- https://github.com/mdarse/travis-docker-build
- https://github.com/d11wtq/dockerpty/blob/master/.travis.yml
- https://github.com/gap-system/gap-docker-travis
- https://github.com/rocker-org/rocker/blob/56cb59cc262e9d2c02f9e11cfacab65f0b659c5f/.travis.yml
Thanks to all of you ;-)