Configuring docker ๐Ÿณservices using ansible.

Harshil Shah
4 min readFeb 20, 2021

In this article I have used ansible playbook to configure docker services and apache web server inside docker container. Also, lastly I have updated playbook where IP of docker container will be dynamically added to inventory file.

If you want to learn about what is ansible and how to start with ansible, than you can read my below mentioned article

โ†’ How to set up ansible up and running

What I will be doing in this article:

Ansible PlayBook that does the following operations in the managed nodes:
๐Ÿ”น Configure Docker.
๐Ÿ”น Start and enable Docker services.
๐Ÿ”น Pull the httpd server image from the Docker Hub.
๐Ÿ”น Run the docker container and expose it to the public.
๐Ÿ”น Copy the html code in /var/www/html directory and start the web server.

Here is my complete playbook ๐Ÿ‘‡๐Ÿผ

โ†’ In first task I am configuring yum repo for docker services using yum_repository module

โ†’ Second task is command module with which I am installing docker-ce services with โ€” nobest option.

โ†’ In third task I have simply started docker services using service module.

โ†’ In order for ansible to work with docker, we have to have docker API for python installed. So in 4th task I have used pip module to install docker API.

โ†’ Then using docker_image module, I have pulled httpd image from docker hub.

โ†’ In 6th task , I have used copy module to copy webpage to manage node.

โ†’ finally using docker_container module I have launched a new container having httpd image. For accessing the webpage we have to expose the port 80,on which the apache server runs in docker and link it with some port of the base OS, which here is the managed node, such that when a client comes on the managed node IP on this port he is redirected to the port 80 and the webpage appears.

Finally our web page is visible. This web page comes from docker container OS, and docker container works on RHEL8 virtual machine instance. So for docker container, base OS is red hat linux 8 and for RHEL8 instance, windows OS is base OS

Now I will add some things more to this playbook which will do ๐Ÿ‘‡๐Ÿผ

  1. Gather IP address of newly launched container and update the inventory file with new IP.

Note: I have created my own image and pushed it to docker hub. This image I have used to launch container. This image contains ssh configured.

The image contains following steps: ๐Ÿ‘‡๐Ÿผ

1) yum install net-tools -y

2) yum install openssh-server -y

3) ssh-keygen -A

4) put /usr/sbin/sshd in /root/.bashrc file

5) set some password for root account, which is used in inventory file during ssh. For that โ†’ yum install passwd and than running โ†’ passwd user_name (refer book-4)

First my inventory file contains 2 hosts. Both are docker containers. ๐Ÿ‘‡๐Ÿผ

docker.yml file contains updated code as follows. ๐Ÿ‘‡๐Ÿผ

The part after red line is newly added.

First I am gathering info of container into some variable โ†’ result_container with help of register keyword. For this I used module โ†’ docker_container_info

This variable holds every information of the container we just launched. We need to extract IP address of this container and add it to the inventory file.

Next I have printed IP address of container with help of system facts. This is

{{ result_container.container.NetworkSettings.Networks.bridge.IPAddress }}

Since it is a variable, we are using curly braces.

Next is lineinfile module. With this module I am updating inventory file instead of overwriting it. So when I run this playbook, a new host will be added instead of replacing all existing hosts.

Running docker.yml ๐Ÿ‘‡๐Ÿผ

When I run playbook, first ansible will prompt me to give container name. Than it will proceed with this container name and configure further tasks.

As you can see in right image, second to last task has printed IP of currently launched docker container. Same host will be added automatically to the inventory file.

Now finally my inventory file ๐Ÿ‘‡๐Ÿผ

inventory file

Thank you for reading.

--

--