In today’s world of cloud-based infrastructure and microservices, containerization has become a popular approach for deploying and managing applications. Docker is one of the most widely-used tools for containerization, and it allows you to package your applications and their dependencies into self-contained units that can be easily deployed and managed.
While Docker provides a powerful platform for building and deploying containers, managing a large number of containers and ensuring their consistent configuration can be challenging. This is where Ansible comes in. Ansible is a powerful automation tool that allows you to manage and configure your infrastructure from a single, central location. With Ansible, you can automate the deployment and configuration of Docker containers, making it easier to manage and maintain your containerized applications.
Understanding Ansible and Docker
Before we dive into the details of using Ansible for Docker container deployment, let’s first provide a brief overview of Ansible and Docker.
Ansible is an open-source automation tool that allows you to manage and configure your infrastructure from a single, central location. It uses a simple, human-readable language called YAML to define the tasks and actions that you want to automate. Ansible is agentless, which means that it does not require any additional software to be installed on the managed machines, making it easy to use and deploy. You can read more about Ansible in our Ansible introduction.
Docker is a tool for building, deploying, and managing containers. Containers allow you to package your applications and their dependencies into self-contained units that can be easily deployed and run on any machine that supports Docker. This provides a consistent, portable, and scalable environment for deploying and running your applications. The Docker ecosystem is rich of tools and services for managing and orchestrating containers, such as Docker Swarm and Kubernetes.
Using Ansible with Docker allows you to automate the deployment and configuration of Docker containers for simple scenarios where Swarm or Kubernetes might be overkill. This makes it easier to manage and maintain your containerized applications, and ensures that they are consistently configured across your infrastructure. It also allows you to use Ansible’s advanced features, such as roles and variables, to make your Docker deployment tasks more modular and reusable
Installing Ansible and Docker
I will assume you have Ansible installed, after following my introduction to Ansible.
Next, you will need to have Docker installed on the control node and on any managed machines that will run Docker containers. Always install Docker using the official Docker installation instructions for your operating system. For example, you can find the instructions for installing Docker on Ubuntu here.
Once you have Ansible and Docker installed, you will need to create an inventory file that defines the managed machines and the connection details for each machine. The inventory file is a list of the managed machines and their connection details, such as their hostnames and IP addresses, and the username and password that Ansible will use to connect to each machine.
Here is an example of an inventory file for a Docker deployment:
[docker-hosts]
web1 ansible_host=192.168.1.100 ansible_user=admin ansible_ssh_pass=password
web2 ansible_host=192.168.1.101 ansible_user=admin ansible_ssh_pass=password
web3 ansible_host=192.168.1.102 ansible_user=admin ansible_ssh_pass=password
In this example, the [docker-hosts]
section defines a group of machines that will be managed by Ansible. Each machine is defined on a separate line, with the hostname, IP address, and connection details for each machine. You can add as many machines as you need to the inventory file.
Once you have configured Ansible and created an inventory file, you are ready to start running Ansible playbooks for Docker container deployment.
Running Ansible playbooks for Docker container deployment
Ansible playbooks are the core component of Ansible, and they are written in YAML format. A playbook consists of a series of tasks that are executed in order, and each task is defined as a dictionary with a name and a set of actions to be performed.
Here is an example playbook for deploying a Docker container:
---
- name: Deploy Docker container
hosts: all
tasks:
- name: Pull Docker image
docker_image:
name: my-image:latest
source: registry.example.com
- name: Run Docker container
docker_container:
name: my-container
image: my-image:latest
ports:
- "80:80"
volumes:
- /var/www:/var/www
In this example, the playbook has two tasks:
The first task pulls a Docker image from a registry and saves it to the local machine. The second task runs a Docker container based on the pulled image, and exposes port 80 of the container to port 80 of the host machine.
To run the playbook, you can use the ansible-playbook command:
$ ansible-playbook -i inventory my-playbook.yml
This will execute the playbook on the managed machines specified in the inventory file. You can verify the deployment by checking that the Docker container is running on the managed machines:
$ docker ps
This command should list the deployed Docker container, along with its status and other details.
Advanced Ansible features for Docker container deployment
Ansible provides a number of advanced features that can be used to make your Docker deployment tasks more modular and reusable. For example, you can use roles and variables to parameterize your playbooks and make them more dynamic.
Here is an example of using roles and variables in a playbook for deploying a Docker container:
---
- name: Deploy Docker container
hosts: all
roles:
- docker-deploy
vars:
container_name: my-web-app
container_image: https://registry.example.com/my-web-app:latest
container_ports:
- "80:80"
container_volumes:
- /var/www:/var/www
In this example, the playbook specifies a docker-deploy
role and defines a set of variables that customize the behavior of the role. The docker-deploy
role would contain the tasks for pulling and running the Docker container, and it would use the variables to customize the container’s name, image, ports, and volumes.
Conclusion
By using Ansible playbooks, you can automate the deployment and configuration of your containers, ensuring that they are consistently configured across your infrastructure. Ansible’s advanced features, such as roles and variables, make it easy to create modular and reusable deployment tasks. By using Ansible for your Docker container deployment tasks, you can save time, reduce errors, and improve the reliability of your containerized applications. We encourage you to try using Ansible for your own Docker deployment tasks and experience the benefits for yourself.