Jenkins Running in Docker

1 minute read

I have a study Java project with CI running on a Jenkins on a AWS EC2 instance. I have been switching to Google Compute Engine lately so I had the chance to refactor it a little bit while I was doing it.

One main purpose for the refactoring is to put everything in Docker container. So if I ever need to move again, it would be easier.

My old plan was to write a docker file to build MySQL and Jenkins inside. But later I realized that was too coupling. The DB can be independent of the Jenkins. So my docker file extends the Jenkins image and only install mysql client in it, since I need to run some SQL script with it in my project.

On my GCE instance, the only thing I need is docker engine. I run a docker command to start a standard MySQL container, and start a Jenkins container from my own image and link it to the MySQL container. That’ it!

For continuous integration I always think it’s better to put everything inside the project on SCM. So I even put the scripts above in the project folder.

Once Jenkins up, all I need to do is:

  1. Change the password and create account, of course;
  2. Configure the JDK and Maven tool globally
  3. Define the project and point it to my Bitbucket project URL to poll changes

That’s it. The pipeline is defined in the Jenkinsfile of my project. It checks out the code branch, creates a temporary db and populates the db schema, modifies some resource file to point to the temp db, runs the junit tests and generates reports, sends the notification to my pushover app and cleans up the db. All is defined in the Jenkins and the scripts in the project.

If I need to move again, all I need to repeat is the steps 1-3 above.

Categories: Tech

Updated: