Building Docker Images Using Ansible Provisioning

Development May 5, 2021
How to build docker images

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.


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


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.  

ansible installation

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.

Ansible inventory

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

Ansible test

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

ansible ad hoc commands

Create an ansible-playbook for make a docker image

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

Ansible playbook

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

Docker image

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


Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.