Persistent data containers: Option 2

I have previously spoken about how I created persistent data containers using scripts for the setup, but I figured I would also show you the other option. Rather than use scripts for the containers, use the docker-chef cookbook and store the data visibly on the VM. This won't be true data container that you can store on Dockerhub, but it will have a lot of the same benefits as running a data container.

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

Warning: This how-to assumes you have a basic understanding of the following before trying to run this. If you don't, then I would (not so) quickly read up on them before coming back.

  • Chef and cookbook layout
  • wrapper cookbooks
  • How to use LWRP's
  • understanding of Docker basics like the difference between images, containers, and a very basic understanding of the docker API

Start with the Chef docker cookbook
This is pretty straightforward like in the previous post. Simply include the docker cookbook in the default.rb recipe, and download the required images using the docker_image LWRP. 

Create directories to store your container data
Then, you will want to create your storage directories for where you are going to store your data. I have elected to store them in the /var/ section of the filesystem. You can set it to whatever permissions you would like, but I have set mine to 777 to avoid any permissions issues. It's not like Docker containers are inherently secure anyway...

Spin up your first containers

Next, you will use the docker_container resource to build the jenkins and Nexus containers as a starting point. These will spin up containers with the filesystems that you would like to copy for your "persistent" data 

(note the commented out text in the resource. This is an attempt to install maven on the container, but it was causing the container to exit prematurely, causing cascading issues. This is a non-automated step that will be fixed in later versions of the cookbook)

Copy from the container to the host

Next, you will copy the contents from the container over to the host VM and the previously mentioned locations. There is a :cp action as part of the Docker_container LWRP, but I have found that it more often than not doesn't copy recursively, just the first file alphebetically in the directory you are trying to move. Therefore, I have elected use the execute resource with the required Docker command. NOTE: if anybody has a solution to this problem, then feel free to let me know so I can adjust it.

Delete the first containers

Next, shut down and remove the existing containers. I originally tried separating out the steps into a stop, then remove process, but found that it left "zombie containers" that affected later steps. The best solution is to use the force true method. It makes the code a lot cleaner that way anyway. 

Spin up your "persistent" containers

Finally, spin up new containers mounting the filesystems on the new containers. 

Note that I have specified port-forwarding in the container resource so I can access the servers externally. To verify that your containers are working simply type in the <ip_address>:<port> fin your browser for each server to verify it is working properly (in my case 192.168.33.42:8080 and 8081 respectively).

So there you go. Voila! It really is that simple. This is not a perfect solution, but one I have found that would work. 

Keep calm, and Chef/Docker on