Using Bitbucket Pipelines to create a Docker image

Jonathan Zimros

An example of creating a Docker image using Pipelines and pushing the newly created Docker image to AWS ECR.

Scenario: You are using an AWS container service and need to build a Docker image to deploy it to.

Starting off, we’ll create a blank file bitbucket-pipelines.yml in the root of our project and copy in the below template.

Template breakdown:

From here we can add our first build step and trigger. Our first build step will handle the building of the image and pushing it to AWS ECR. Next, we will set up our trigger, so when changes are pushed/merged into the master branch it will run the build image step.

Build step breakdown (lines 6 to 18):

Build step script breakdown (from line 12):

At this point, you may be thinking wait where are the variables DOCKER_IMAGE_URL and BITBUCKET_BUILD_NUMBER defined?

Bitbucket pipelines provides a set of default variables, those variables start with BITBUCKET, which makes it easy to differentiate them for user-defined variables. DOCKER_IMAGE_URL, on the other hand, needs to be defined within Bitbucket along with 3 other variables, this can be done by:

The 4 user-defined variables:

Now that you have completed all that, you are ready to go on and add additional build steps to have a streamline build and deploy workflow.

As a bonus, I’ve included an example pipelines file that will:

Below that is a link to my example repository.