Docker as you know is the most popular containerization platform and Ansible is the leading automation tool. In this post we will see how we can automate / manage docker images with Ansible.
Module:
Ansible provides
docker_image module to pull / create docker image.
We will cover two areas of docker image
(a) Pulling images from repository using Ansible
(b) Creating images from dockerfile using Ansible
Pulling images from repository using Ansible:
Lets see how we can pull docker image by using docker_image module. Here I am pulling RabbitMQ image.
Source Code:
[root@test ansible_example]# cat docker-pull.yml
---
- hosts: localhost
tasks:
- name: Pull RabbitMQ Image
docker_image:
name: rabbitmq
source: pull
Lets run the above playbook.
Output:
[root@test ansible_example]# ansible-playbook docker-pull.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [localhost]
TASK [Pull RabbitMQ Image] *******************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Explanation:
This will download RabbitMQ image from docker repository. To verify use "docker images" command and we will find RabbitMQ images has been pulled from docker images.
Creating images from dockerfile using Ansible:
Docker provides another way to create images using
dockerfile. Ansible also provides way to automate creating docker images using
dockerfile.
Creating dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y rabbitmq-server
Now lets create
playbook to build docker my rabbitmq image from above dockerfile
Source Code:
[root@test ansible_example]# cat docker-build.yml
---
- hosts: localhost
tasks:
- name: Build RabbitMQ image
docker_image:
path: .
name: test/my-rabbitmq
tag: v1
source: build
In the above code snippet, we are trying to build a docker image located at current path.
Now lets run the playbook.
[root@test ansible_example]# ansible-playbook docker-build.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [localhost]
TASK [Build RabbitMQ image] *******************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Please note you can easily push the local image to repository using Ansible. Here is the playbook example:
[root@test ansible_example]# cat docker-push.yml
---
- hosts: localhost
tasks:
- name: Push image
docker_image:
name: test/rabbitmq:v1
repository: localhost:4800/test
tag: v1
push: yes
source: local
Conclusion and Best practices:
Basically you can replace your
docker-compose yml file with Ansible easily to manage containers even better. This way you can automate containerized application creation & deployment at one place and when the build triggers and completes, Ansible playbook can automatically deploy containers from one place to multiple node(s)
If you are looking for all the options of docker_image module, please visit the
official link here
Kubernetes is Docker orchestration engine and most devops team uses
Kubernetes for docker lifecycle.
This article can be helpful if you are looking for large number of container deployment using Ansible and Kubernetes.
That's all for building / managing docker images using Ansible. If you have any query, please mention in comment section. Thanks
Related Articles:
You may also like: