Persistent data containers: Option 1

For a recent project, I was required to get some servers running and setup using Docker. As I have spent the last year on Chef, I was a bit scoffed at the idea of abandonding all that I learned. In the end I caved in, and found a nice middle ground that allowed me to use Chef and Docker in relative harmony.

One of the key issues surrounding Docker now is how to properly setup and store the data that runs on the containers. From what I have gathered there are two schools of thought on the solution

  1. data containers
  2. put the data on the host VM and just mount it onto the container

This how-to covers option 1; setting up a Docker data container setup with Chef. it is not a perfect solution, but a good starting point to experiment (and the exact system we are running SMOSLT on). I will explain how to do the volume mounting option in a separate how-to.

Full Disclosure: I am blatantly stealing these scripts from another online tutorial that accomplished what I was trying to do already. It just didn't use Chef to get Docker and the scripts onto the VM. You can visit the other tutorial here if you would like

For those of you who just want to look at my cookbook, here you go

In the following how-to, I am going to use Chef to setup a VM with the following pieces

  • Docker installed onto the VM
  • Use publicly available Docker images of:
    • jenkins server 
    • nexus server
    • tomcat server 
  • Create a persistent data container for portability and centralized storage of our other servers data

Okay, enough prefacing yet? Well lets get started then. 

Start with the Chef Docker Cookbook
The first step is making sure that Docker is installed onto your VM and is along with the base images you will need. These are essential first steps in the recipe. The easiest way to do that is to use the chef-docker community cookbook to install docker and download the base images. I normally follow the creed that one should not use community cookbooks, but in this case, please do. It is an well written cookbook that is easy to follow, and has good documentation.

For Jenkins and Nexus, we are using images that were used in the reference tutorial to make the use of scripts easier. For the tomcat server (bonus) we used a tomcat image from Tutum. They have several base images that are well done and a good starting point for Docker beginners. to pull the images, we are using the docker_image LWRP that comes as part of the docker cookbook.

Add the required scripts

If you have use Chef before, more than likely you have used the template resource to install config files onto the target VM. If not it is an excellent built-in resource to Chef that you should consider using. We will start by creating a directory where all of the scripts will be placed, then install the scripts themselves

(Note: Probably not a best practice to install this in the root directory, but for basic instruction we are sticking it here)

Run the script
This is the easiest part. Using the execute resource, set a command to run the script which will start the process.

I will skip the nitty gritty of the scripts, but in short it runs the following actions

  • pulls data off Jenkins and nexus servers
  • creates a backup container
  • pushes data onto backup container
  • makes it a restore container
  • pushes content from restore onto new official data container
  • starts up jenkins and nexus where it mounts data container points onto subsequent containers

If you want to look at the bare scripts for the nitty gritty, here you go.

Finally, start the Tomcat container
there is a you could just as easily run a command line call for this, but I have elected to use the docker_container LWRP instead. it has all the actions and attributes needed to perform this step.

In Conclusion

I now have 3 servers running on a single host VM that deploy and run all of our infrastructure. I still have to set up my Jenkins builds and Jenkins plugins by hand, but that is a relatively painless procedure that most of us have done before. For a disaster recovery situation all we have to do is run the backup script, push the data container into a remote storage of our choosing, and pull it as needed. This is something that could even be automated with more build scripts. As only 2 people are consuming the Webapp, I am prioritizing this as non-critical.

Things this how-to doesn't cover

  • Teach you advanced "docker-ing"
  • Use your existing cookbooks to configure containers
  • automating the commit and deployment of containers to a remote storage location like S3
  • Eliminate the need to put in new builds and configuration by hand. 

Keep calm, and Chef/Docker on