I already spoke about Bitbucket Pipelines in a previous article (here it is, in french), and since few months, I’m having a lot of fun with a new feature of this product: we are now able to build our own Docker Images and push it to a Docker Hub repository thanks to pipelines scripts and your own Dockerfile.
I’m starting to use few Docker Repositories for my demos and addons developments, so it can become hard and difficult to manage. Some of my customers are also using Docker images and private repositories so I had to find a way to make my daily life easier; easier for me to maintain and easier for my co-workers to deal with.
It’s a very simple example showing how you can :
- build your own Docker Image from a dockerfile,
- use Pipelines to build it,
- and push it a public repository on Docker Hub.
Writing the dockerfile
This is the first basic step to start with: writing your dockerfile of course! For my own needs, I want to build an image based on Fedora, with Java installed on it. I’m doing this because I want to choose the version of Java that I want to deploy on my image, so I’m installing Java myself on my image.
Then I am also installing the Atlassian SDK on my image, and thanks to this I am now able to build and deploy Atlassian Addons using my own Docker Image. Push the file to your Bitbucket repository using Git and you are now ready for the next step.
Create your bitbucket-pipelines.yml file and use this new syntax to be able to execute Docker commands directly in Pipelines.
Few explanations :
- line 1: “options” is new and allow you to options to your script.
- line 2: the tag “docker” with the value to true is here to mention that you want to use Docker commands.
- line 8: I’m using the last Bitbucket commit ID thats to the environment variable provided by Bitbucket.
- line 10: I’m also using environment variables for my Docker Hub credentials.
- line 10 / 12 / 14: three Docker commands to build, login and push your image to Docker Hub.
Continuons Integration with Bitbucket Pipelines
Now it’s time to see your first Pipelines builds for your Docker Image :
You have access to the full logs of the build faster, so you can develop and debug your problem quickly, it’s also shared with your team and everybody can help you improving your image using branches and pull requests. You are also able to test your image because they are directly accessible on your Docker Hub (public or privates repositories), and using the commit ID in Bitbucket you have different image tags matching the commit ID in your code!
Last but not least: Bitbucket Pipelines now supports service containers, which brings the power of Docker to your test environment configuration. You can now run up to three background services in your pipeline, in addition to your build container, using your own Docker images or any of those available on Docker Hub. This makes it way much easier to set up integration testing with databases like MySQL or run other common services like ElasticSearch.