Using Jenkins to build and test a Docker Compose cluster (via Rails, Elasticsearch, and RSpec)
In this post I’ll share a working example that uses Jenkins to build and test your Docker Compose cluster. For this example I created a simple Rails project that connects to Postgresql and Elasticsearch. I used RSpec to test the connectivity of the container services, and then configured Jenkins to build the project and run the test suite.
I installed my Docker dependencies via Brew on OSX.
Add gems, edit file: Gemfile
Execute bundle install to install the new gems.
Executed rails g rspec:install to generate the default RSpec configuration.
Updated database configuration to use environment variables for connectivity. edit file: config/database.yml
Add a migration to create a table for a Person model (first and last name), new file: db/migrate/20170315211521_create_people.rb
Created the Person model with Elasticsearch integration, new file: app/models/person.rb
Added an RSpec model test file for the Person model. I purposely did not mock the backend to ensure the services are connecting properly. new file: spec/models/person_spec.rb
Add a simple Dockerfile for the Rails container that adds netcat to test for service connectivity, new file: Dockerfile
Created the Docker compose file to define the containers for Rails, Elasticsearch, and Postgresql, new file: docker-compose.yml
Added a shell script to start rails, new file: bin/docker-start-rails
I added a simple Ping controller which will be used by Jenkins to ensure the Rails container is up before starting the RSpec test suite. new file: app/controllers/api/pings_controller.rb
And the corresponding route for the ping request, edit file: config/routes.rb, added:
At this point the application can be functionally tested via: docker-compose build && docker-compose up
I added a bin script as an entry point for Jenkins. The script defines a docker machine called “jenkins”, ensures it is created, running, and active. It stops and removes any existing containers. It then builds the Docker compose cluster, waits for Rails to respond to the ping request, and executes the RSpec test suite. new file: bin/jenkins
The following images shows the settings I used for the Jenkins Docker Compose project. I integrated with my Github repo and set Jenkins to poll every 15 minutes for changes. I added a build step to timeout after 10 minutes, and execute the following: