Building Docker Images Using Ansible Provisioning


How Ansible work in this scenario. Firstly, ansible create a one intermediate container build docker images as per the need of the developer. After this image will be push to the container registry. And it will use in any location what we need.

Prerequisites

  1. PC or server.
  2. Basic knowledge of Ansible.

Setup

Let's jump to installation of Ansible.

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

After installation, verify the Ansible path and version with the help of command.  

Setting Up the Inventory File

To show the contents of your default Ansible inventory or we can make our own ansible inventory, for default inventory path is /etc/ansible/ansible.cfg or we can make our custom path.



Testing Connection

After configure a inventory file, we check the connection is working or not with the help of root user. applying command to the terminal.

ansible all -m ping -u root

Running Ad-Hoc Commands (Optional)

After successful connection, you can implement ad-hoc  commands and playbooks on your servers. let's we check the running simple command with ansible to check the server disk spaces.

ansible all -a "df -h" -u root

Create an ansible-playbook for make a docker image


Create a playbook for making a docker images and synchronize app with code.

run a playbook, which we created above with the command of ansible-playbook playbooks/hostname.yml.

At the end of the output you show that docker image is build and run the container base on the docker images.

To learn more about Ansible , please check the documentation. To get started with building playbook.

Read this Article also

Elasticsearch + Fluentd + Kibana Setup (EFK) with Docker