Over the holiday break, I was able to spend some time optimizing my homelab that runs my automation, entertainment and other household support functions. I had been managing most of the systems by hand. As I move to containerize some of my services for an upcoming system upgrade I took the opportunity to learn some Ansible and use that to begin managing my homelab.
My homelab consists of two hosts. One the storage host runs OmniOS, the other is a VMWare ESXi host that runs various linux and windows VMs.
Docker containers for services
I had been manually managing two docker services, SageTV and Unifi. I had struggled to find a reliable method to start these on boot, restart them when necessary, and in general management of these containers. Setting these containers up on my homelab was my initial exposure to docker and it showed.
systemd and Docker
I wanted to manage my containers as if they were system services. This would bring familiar start/stop actions, as well as ensuring they were started on boot. This article has good background information on how to manage docker containers with systemd.
systemD and Docker Playbook
- I removed the task where the service pulled latest using the command module on every run. I wanted to control the version installation of the docker container using the native docker_image module in ansible.
- I had to rename the “name” variable to “service_name”. In version 2.4 of ansible there is a defect that passes through the name of the included role rather than the variable.
- Create a generic docker role for my lab
- Create a role for each container I want to manage
I created a docker playbook that would configure my host system to support the docker containers I planned to manage. This playbook ensures the docker and nfs packages are installed, installs pip, and the docker-py python package and then ensures the containers storage directory on the storage host is mounted.
Configure and start docker container as a service
Several steps are necesary in the playbook to setup the docker service.
- Call our base docker role to setup docker
- Validate the storage directory exists for this particular container
- Pull a specific docker image
- Use mhutter’s modified ansible role to create the appropriate services, enable and start it.
Below the playbook that performs those steps for the SageTV docker container.
After the playbook executes, the SageTV container is ready to go.
Reusing the ansible service role and a few lines of YAML makes it easy to install, configure and run docker containers as services.