This content is part of the Essential Guide: Learn DevOps techniques in a home lab

Essential Guide

Browse Sections

DevOps lab: How to use Docker swarm mode to run containers in VMs

This article is part of a series to help IT ops professionals learn DevOps by building a home lab. In this final step, IT pros new to containers learn how to use Docker swarm mode.

With enterprises evaluating containers for production, Docker is an appealing part of building a DevOps lab.

While developers create microservices and containerize them, ops professionals should learn Docker swarm mode to run these encapsulated bits of code.

Author's note: Docker is not built into Ubuntu 16.04 and is a bit of a pain to deploy. I have built an Ansible playbook that takes care of some required manipulation of the advanced package tool management configuration, and then installs Docker on all three Ubuntu VMs. With three VMs, the setup allows for Docker swarm mode, so the playbook configures the swarm as well. The playbook is a little complicated, particularly the test for whether we are already in a Docker swarm and passing the swarm join token from one play to another. To deploy and configure Docker, run Ansible playbook /vagrant/DeployDocker.yml and watch the magic happen.

Create a DevOps lab

Before using Docker swarm mode to run containers in the DevOps lab, learn how to automate configurations with Ansible. Or start here to get the lab up and running.

Deploy on Docker

Once the playbook completes its deployment tasks, verify the swarm with the command Docker node list:

Verify a Docker swarm

The swarm is ready for an application to deploy. To learn how to use Docker swarm mode, it's best to practice with an application that has multiple services and containers. Docker's sample voting app fits the bill.

A single command deploys the app, thanks to a YAML file that tells swarm mode how to deploy it. Both the playbook to deploy Docker and the YAML file for the voting app are in my GitHub repository; copy these files into your DevOps lab directory:

Deploy Docker's voting app

The nodes will download the container images from the Docker hub and start up the services. Use docker service list to watch the progress of the services. 

Deploy Docker's voting app

Eventually, the containers will all start and the Replicas column will fill out:

Docker voting app starts

Remember to add the two new .yml files to Git and commit the changes. This exercise teaches an ops professional the first steps of how to use Docker swarm mode.

You now have a DevOps lab that uses Vagrant with Docker, Ubuntu and Ansible to create automated server builds, manage configurations and deploy containers on a Docker swarm. This is a good start to learn DevOps infrastructure tools and technologies that you can apply during your career.

Homework in the home lab

There is another file called in the GitHib repository for this project. This script runs all of the commands inside the master VM instead of manually: It sets up Ansible, then runs the Docker playbook and finishes by launching the voting app. This gets the whole lab up and running in less than five minutes. Create your own GitHub account and add a repository for your own copy of this home lab.

Try cloning your repository out on a different computer or just in a different folder. As long as you have Git and Vagrant you can git clone and vagrant up to be in the lab in no time.

This DevOps lab is a starting point. Now that you know how to use Docker, Ansible and Vagrant, you can further experiment. For ideas, take a look at the websites for Vagrant and Ansible:

  • Ansible uses the test playbook to retrieve quite a lot of information -- it could feed into documentation and update automatically in your setup.
  • Build a VM in VirtualBox that connects to the network and has a web browser, and use it to access the Docker voting app.

Share your experiments from the DevOps lab in the comments for others to try.

Next Steps

IT shops using Docker swarm mode can keep a secret

Docker zeros in on ease of use for swarm mode

Network Docker containers for production-ready workloads

Dig Deeper on Managing Virtual Containers