Configure Docker Using Ansible Playbooks

Niharika Dhanik
5 min readAug 30, 2021

--

After learning the concepts of Ansible and Docker, it was now time to integrate these two technologies and ease the task. So here by I present my next task of “Integration of Ansible With Docker”.

What is Ansible?

Ansible is an open source automation engine designed for Information technology (IT) administrators and development and operations (DevOps) teams. It is a tool in the Server Configuration and Automation category of a tech stack. It is an IT automation tool.

What is Docker?

Docker is an open source platform for building, deploying, and managing containerized applications. It enables developers to package applications into containers with standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. Containers simplify delivery of distributed applications, and have become increasingly popular as organizations shift to cloud-native development and hybrid multi-cloud environments.

Now that the basic idea of these technologies are cleared, let’s jump to the agenda of this blog.

Basic Architecture

The following Plays are executed by the Ansible PlayBook in the Target Node —

➼ Create a yum repository to configure Docker
➼ Execute commands to install Docker
➼ Check for Docker Package
➼ Enable Docker Services
➼ Install Docker SDK
➼ Pull the required image (here — vimal13/apache-webserver-php:v1) from the Docker Hub
➼ Run the docker container and expose it to the public
➼ Initiate HTTPD on Docker (optional)
➼ Get Container Details (optional)
➼ Copy File To Docker Container
➼ Copy the html code from Controller Node to "/var/www/html" directory inside the newly launched Docker Container
➼ Access the webpage using “curl” command inside the container

The step-by step execution —

➼ It starts with installing ansible. This command gives an overview regarding the location of the configuration file, python version on which it is operating, etc.

ansible --version

➼ This is the inventory file which consists IP, Login and Password of target node. I have entered only 1 system information (multiple can be added).

# here, the name of my inventory file is "IP.txt" which contains the # information about the IP, user login and password of the system.cat IP.text

➼ The “ansible.cfg” file consists of the following information. When two systems are connected using the SSH protocol, the target node sends some host keys for authentication. To bypass this, the host key checking is set to false (no need to authenticate the SSH host keys manually).

# In my case, the inventory is saved at a location which is inside  # the "workspace" directory[defaults]
inventory = /root/workspace/IP.txt
host_key_checking = False
deprecation_warnings = False

➼ This command helps to check with the syntax and indentation error inside the YAML file in the CLI itself.

ansible-playbook --syntax-check playbookname.yml

➼ The following is the playbook which does the needful operations like making a repo for docker, running commands, installing and checking services, initiating a container, pulling images on container and exposing the html page from controller node to container which is running on a target node.

# this is a docker playboook- hosts: all
tasks:
- name: Create Yum Repo
yum_repository:
name: "docker"
description: "this is a docker repo"
reposdir: "/etc/yum.repos.d/"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck : "no"
enabled: yes
- name: Run Yum RepoList
command: yum repolist
- name: Run Docker CMD
command: yum install -y --nobest docker-ce
- name: Check Package
package:
name: "docker-ce"
state: present
- name: Check Service
service:
name: docker
state: started
enabled: yes
- name: Install Docker SDK
command: "pip3 install docker-py"
- name: Pull IMG From DockerHub
docker_image:
name: "vimal13/apache-webserver-php:v1"
source: pull

- name: Run Container
docker_container:
name: "container1"
image: "vimal13/apache-webserver-php:v1"
state: started
detach: yes
exposed_ports:
- 80
ports:
- "8080:80"
- name: Initiate HTTPD on Docker
command: "systemctl start httpd.service"
- name: Get Container Details
docker_container_info:
name: "container1"
register: result
- name: Copy File To TN
copy:
src: "demo1.html"
dest: "/root"
- name: Copy File To Docker Container
command: "docker cp /root/demo1.html container1:/var/www/html"
Docker Playbook

➼ The following is the execution of the playbook. The orange and green colors indicates changes made and already satisfied requirements respectively.

➼ The following images are from the target node after the playbook had successfully been executed.

# To check the docker repo inside target nodecd /etc/yum/repos.d/

➼ The following command helps to check whether the docker service inside the target node, is enabled or not.

systemctl status docker

➼ This command is used to check the list of currently running containers. As we can see, the container is running and the image name is also mentioned.

docker ps

➼ To run the container created using playbook, the following command is executed (here the name of my container is “container1”).

docker exec -it container1 bash

To check the IP address of the container, run the following command (the IP of my container is 172.17.0.2).

ifconfig ORifconfig enp0s3

➼ The curl command helps to connect to the webserver by displaying the output inside the HTML file on the CLI (the name of my html file is “demo1.html”).

curl 172.17.0.2/demo1.html

In this manner, I have finally accomplished the integration of Ansible with Docker.

( Edit: There’s an amazing article about ansible playbook which is easy to get started and includes a lot of hands-on. For someone who doesn’t want to deep dive into a lot of theory, this is a good read!! )

Thank you!!

--

--

Niharika Dhanik
Niharika Dhanik

No responses yet