Automate dockerization of webserver using Ansible
Ansible — Configuration Management Tool
Docker — Container Management Tool
Let’s integrate these two to come-up with a wonderful use-case!
What is Docker?
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. While containers as a concept have been around for some time, Docker, an open source project launched in 2013, helped popularize the technology, and has helped drive the trend towards containerization and microservices in software development that has come to be known as cloud-native development.
Why Docker?
Developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage creates enormous complexity. Docker simplifies and accelerates your workflow, while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project. Docker enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere. As Bottomley told me, “Containers gives you instant application portability.” In addition, Docker containers are easy to deploy in a cloud.
So, What are we going to deploy here?
We are going to write a playbook that’s gonna perform the following tasks:-
🔅 Configure Docker🔅 Start and enable Docker services🔅 Pull the httpd server image from the Docker Hub🔅 Run the httpd container and expose it to the public🔅 Copy the html code in /var/www/html directory and start the web server
First, let us configure Ansible in the controller node for workability.
- Setup ansible.cfg file
[defaults]
inventory=inventory
host_key_checking=False
command_warnings=False
- Create the inventory containing the IP of the managed node in the same directory as of ansible.cfg file.
<IP of managed node> ansible_ssh_user=root ansible_ssh_pass=redhat
Do check whether Ansible has been configured or not using the command —
ansible --version
Once Ansible is successfully configured, we can go on with creating the ansible playbook: —
---
- name: Setup docker on managed node
hosts: all
tasks:
- name: Configure docker repo
yum_repository:
name: docker
description: Yum repo for docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no- name: Install docker
command: yum install docker-ce --nobest -y
ignore_errors: yes- name: Install python3
package:
name: python3
state: present- name: Install docker SDK
pip:
name: docker-py- name: Start and enable docker service
service:
name: docker
state: started
enabled: yes- name: Pull docker image
docker_image:
name: httpd
source: pull- name: Create workspace directory
file:
path: /root/ansible-task-1
state: directory- name: Copy webpage content
copy:
content: "This webpage is configured inside a docker container deployed using Ansible."
dest: /root/ansible-task-1/index.html- name: Stop firewall services
service:
name: firewalld
state: stopped- name: Run a docker container
docker_container:
name: webos
image: httpd
state: present
exposed_ports:
- 80/tcp
- 80/udp
published_ports:
- 0.0.0.0:8081:80/tcp
- 0.0.0.0:8081:80/udp
volumes:
- /root/ansible-task-1/:/usr/local/apache2/htdocs/
Seems good to go :)
- Let’s deploy the playbook now —
ansible-playbook <playbook_name>.yml
From the playbook, its evident that we have exposed our docker container to port number 8081. So, let’s check whether the webserver has been containerized or not —
http://<IP of managed node>:8081
Voila! Deployment successful :D
That’s all for this practical guys, than you for a read!