Configuring Docker Containers using Ansible

Muhammad Tabish Khanday
3 min readJul 18, 2021

In this blog, I will demonstrate how I used Ansible to perform the following operations:

  1. Configure Docker on Centos 8 or RedHat 8
  2. Start and enable Docker services
  3. Pull the Docker Image from GitHub
  4. Run the Docker container
  5. Use this new container as a managed host.
  6. Configure apache2 web server in the container managed host and expose it to the public.

Step 1: Create a workspace for ansible control node

$ mkdir ansible-ws

$ cd ansible-ws

$ mkdir inventorydb/

Create ansible.cfg file and add these values to it

$ vim ansible.cfg

Now, create a static inventory file for our managed node

$ vim inventorydb/inventory

Step 2: Create an Ansible playbook for configuring Docker on the managed node.

$ vim docker_configure.yml

Run this playbook:

$ ansible-playbook docker_configure.yml

Step 2: Create an Ansible playbook that runs a container then updates the inventory with that container dynamically and then configures that container with Apache2 webserver

$ vim container_configure.yml

Run this playbook:

$ ansible-playbook container_configure.yml

Let’s check the managed hosts and our inventory:

As you can clearly see that a new managed node is added to the inventory. It is the Docker Container that we configured in this playbook.

Now, Let's check if the webserver running inside the Docker Container is running or not:

It’s working perfectly fine…

GitHub: https://github.com/mtabishk/ansible-playbooks/tree/main/docker_container_conf

That’s all for today! I’ll be back with some new articles very soon, thanks! 🤗

Muhammad Tabish Khanday

LinkedIn: https://www.linkedin.com/in/mtabishk/

--

--