Ansible Docker Container Management Playbooks
Ansible can be used to manage your Docker container environment and other infrastructure. Let’s see how that can be done.
Table of contents
What is Docker?
Docker has become the standard for running containers. It is a great way to run applications without the complexity of running VMs in the environment.
Installing Docker in Ubuntu 22.04
First run the update to pull the latest updates
sudo apt-get update
Next, install the required dependencies:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to your Ubuntu system:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the package manager again:
sudo apt-get update
Finally, install Docker:
sudo apt-get install docker-ce
To verify that Docker has been installed successfully, run:
sudo docker --version
What is Ansible?
Ansible is a type of infrastructure as code tool that can be used for a lot of different things. You create your Ansible files using YAML configuration language for your config files used for your Ansible instructions. These are called playbooks in Ansible.
When you run an Ansible playbook, it runs through the YAML code and configures the infrastructure you want it to based on these playbooks that can do many differen things.
Why automate Docker containers with Ansible?
It brings a lot of benefits to managing Docker, such as:
Simplicity: YAML can be tedious but it is actually human-readable so it is fairly simple compared to other languages
Consistency: Infrastructure as code makes things consistent and repeatable.
Scalability: Ansible makes using this for large environments very easy
Flexibility: Ansible has a lot of modules you can use so it is very modular and can do just about anything you need it to do across all types of systems, including Docker
Managing Docker Containers Using Ansible Modules
There are Ansible modules for Docker containers and images. You can use modules like docker_container, docker_image, and docker_network. These modules can allow you to create, start, stop, and remove containers. You can also manage Docker networks.
Below is a look at the official page for docker_container Ansible module. You can find that here: docker_container โ manage docker containers.
Playbooks to install packages
Installing packages is a common use for Ansible automation with your hosts in the environment. Below is an example of installing packages using apt and you can use other package installers like yum as well.
- name: Install required packages
apt:
name: "{{ item }}"
state: present
loop:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
Docker build with Ansible
To build a Docker image using an Ansible playbook, use the docker_image module.
- name: Build Docker image
docker_image:
name: my_image
path: /path/to/dockerfile
state: present
Of course you need to change out the /path/to/dockerfile with the path to your Dockerfile location, and my_image with your name for your container for the environment.
Docker Installation Tasks
WE looked earlier at manually running an install of Docker on your host. You can also use Ansible to do this task as well and have it automate the process to install Docker installation.
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present
- name: Update apt cache and install Docker
apt:
name: docker-ce
update_cache: yes
state: present
Adding Docker Image to playbooks
Add tasks that use the docker_image and docker_container modules to manage Docker images and containers using Ansible. For example:
- name: Pull Docker image
docker_image:
name: my_image
source: pull
state: present
- name: Create and run Docker container
docker_container:
name: my_container
image: my_image
state: started
Running Ansible playbook
You can run your new Ansible playbook you created with the command that is simple like this:
ansible-playbook my_playbook.yml
Replace my_playbook.yml with your playbook file name.
Run your Docker image with Ansible
To run a Docker container from a specific image using Ansible, use the docker_container module. Add the following task to your playbook:
- name: Run Docker container
docker_container:
name: my_container
image: my_image
state: started
Ansible Modules for Docker management
Once you have Ansible installed, you can install Ansible modules. Some of the most commonly used Docker modules in Ansible include the following:
docker_container: Manages Docker containers, including creating containers and running containers
docker_image: Manages Docker images from which you create containers
docker_network: Manages Docker networks.
docker_volume: Manages Docker volumes.
The Docker module examples provide a robust set of tools for automating the management of your Docker infrastructure.
Scaling containers
To scale your containers there is a scale attribute you can use with the docker_container module. For example:
- name: Scale Docker containers
docker_container:
name: my_container
image: my_image
state: started
scale: 5
This task would ensure that there are five instances of the my_container container running based on the my_image image.
Also, you can use the docker_container Ansible module config resource options, such as ones like memory, cpus, or blkio_weight. For example:
- name: Increase container resources
docker_container:
name: my_container
image: my_image
state: started
memory: 2g
cpus: 2
It will configure my_container container to use 2 GB of memory and two CPU cores.
Updating Docker Images
Ansible makes it easy to create a playbook with tasks that can pull the latest images down and restart your containers in the environment using the new images that were downloaded to the environment: For example:
- name: Update Docker images
docker_image:
name: my_image
state: latest
- name: Restart Docker containers
docker_container:
name: my_container
image: my_image
state: started
recreate: yes
This code will look at your my_image image and make sure it is updated to the latest version. Also, it will make sure that the container called my_container container is recreated (respun) using the updated image.
Wrapping up
Ansible is really arguably the leader in configuration management tools. Using it you can keep your Docker container environment updated and configured as you want it to be. As mentioned too, you can use it in your CICD pipelines to run IaC against your infrastructure including your Docker containers.