This is a guest blog from Himanshu Chhetri who is part of the DevOps and Atlassian Tools Implementation team at Addteq. He likes to discover interesting open source projects and occasionally shares memes on HipChat!

Bamboo is a strong tool for implementing a stable continuous integration and deployment pipeline, which is an essential practice for any team interested in devops. 

Docker describes itself as “An open platform for distributed applications for developers and sysadmins” and has rapidly gained popularity as one of the best tools to build, ship, and run software. Use of Docker allows replicating the exact environment of the builds locally. It also makes it possible to run deployments against different environments (i.e. QA or production) consistently.

Recent Bamboo releases have been using Docker very effectively for a variety of use cases, from provisioning Bamboo agents in Docker to providing tasks for building Docker images, pushing images to a Docker registry, and running Docker containers.

This post will go over a practical use case scenario that combines the power of Docker containers with Bamboo’s strong Docker features for building and deploying a web application — just one of the many ways the Bamboo-Docker duo works together! 

Two Docker containers to begin with

The goal here, as mentioned above, is building a web application. We’ll walk you through making a simple Node.js web app that uses Redis to maintain a counter for the number of times a page is visited. Every time the page is refreshed, the counter gets automatically incremented:

0

1

2

We’ll be using two Docker containers: One for Redis and one for the Node.js web app. The two containers will be able to communicate with each other once we utilize Docker’s linking ability.

You can find the source code of the app here: https://bitbucket.org/addteq/bamboo-docker-example/src. This repository contains a Dockerfile with all the steps you need to take for creating the web app’s Docker image:

FROM gliderlabs/alpine:3.1
RUN apk --update add nodejs
RUN mkdir /src
WORKDIR /src
COPY . /src
RUN npm install
EXPOSE 3000
CMD ["node", "/src/server.js"]

This container can be built with the following command:
docker build -t bamboo-docker-example.

We also need a Redis container to store the counter and keep track of the visits, for which we’ll use the official image from the Docker Hub:
docker run -p 6379:6379  -d --name redis redis

Use the following command to run the container for the web app:
docker run -p 3000:3000 --rm --name web --link redis bamboo-docker-example

But be prepared to see an error since the container relies on using a Docker link with a Redis container:
Error response from daemon: Could not get container for Redis

To avoid this issue, make sure you are running the command that starts the Redis container prior to the one starting the web app.

Now, when we try to run the container for the web app it can successfully establish a link with the Redis container:
docker run -p 3000:3000 --rm --name web --link redis bamboo-docker-example

If all goes well, the web app is accessible via port 3000 in the browser at: http://<HOST IP ADDRESS>:3000/

Note: If you’re running a docker-machine you can find the IP with:
docker-machine ip

And use the following command if you’re running boot2docker:
boot2docker ip

Winning the automation game with Bamboo

Now that we have used Docker to successfully build and run our application, let’s look at how we can utilize Bamboo for automation.

As a prerequisite, Docker should be installed on a Bamboo agent that’s running a supported installation platform. For Elastic Bamboo users, Docker already exists in the stock elastic image. The next step is to configure the Docker capability on the Bamboo agent in order to use the tasks:

bamboo-docker

We then create a Bamboo plan to use the tasks for building the Docker image from the Dockerfile and pushing it to the Docker Hub. Select “Create a new plan” from under the create menu in your Bamboo instance and use this repository: https://bitbucket.org/addteq/bamboo-docker-example.gitWe recommend making a fork of the repository if you would like to experiment with making changes to the code or run any tests!

bamboo-plan cropped

The Docker task for building the Docker image from the Dockerfile can be configured as shown below. The “himanshu/bamboo-docker-webapp” in the repository box is my username, himanshu plus the name of the repository, bamboo-docker-webapp, should be replaced with your Docker hub username and repository. Make sure to sign up for an account if you don’t have one already. We are also using the existing Dockerfile from the repo here.

build container

It’s now time to add another task and push the Docker image to Docker Hub. Another option we have here is to select a custom private Docker registry, but let’s not make things complicated and stay with the public Docker Hub repo for this example. We have included Docker Hub credentials in the task but you can also add them to the ~/.dockercfg file that sits on the Bamboo agent as outlined here: Setting up the Docker Task in Bamboo.

push container

If all goes well and the build is green we’ll see the Docker image successfully pushed to Docker Hub:

docker hub

Now, we can create deployment projects in Bamboo, define tasks to utilize Docker hub for the images, and deploy the container to target environments. In the deployment project, we’ll add a script as the first task to stop and remove any previously running instances of the Redis and bamboo-docker-webapp containers.

bamboo-docker-script

We’ll then add a Docker task to start the Redis container with the option to detach it along with exposing port 6379.

RedisFromTardigrade

We can also add a second Docker task to run the bamboo-docker-webapp container detached and select options to expose port 3000 as well as linking it to the redis container.

Note: Please make sure the “Container working directory” parameter of the bamboo-docker-webapp Docker task is empty as shown in the following screenshot.

FromJingWebApp

 

We hope you found this post helpful!

We’re all excited about what can be done with Docker and Bamboo and can’t stop sharing our successful experiments with you. Read more about Bamboo’s Docker features here, then grab an image and start a free trial to test what you’ve learned.

Happy Dockerizing!

About Addteq:

Addteq is a certified Atlassian Expert specializing in software configuration, build automation, and release management. The Addteq team has been providing business solutions for over nine years, and has delivered superior results to major companies such as Cisco, Wells Fargo, and Bank of America.

Fresh ideas, announcements, and inspiration for your team, delivered weekly.

Subscribe now