Configuring docker and launching a web container using Ansible

Anurag Mittal
6 min readAug 4, 2020

--

  1. For configuring any server/os there are two approaches-

(1) Traditional Approach
Manually go to server and enter the commands for configuring it.

(2) Automation Approach
Write a script that will configure the server when we run the script on it.
In Automation approach we have two ways -

a. Script with Imperative Language
In this script we have to tell what to do and how to do means if we want to install a package then we have to also write in script the command used for installing the package in that particular server.

b. Script with with Declarative Language
In the script that is written with declarative language , we have to only specify what to do there is no need to tell how to do.
It is so much intelligent that it automatically detect command used for installing the package in that particular sever.

2. Ansible is a tool/software . Ansible work on concept of declarative language. we have to tell Ansible what to do , it automatically detect the command for it that support that server.

4. Controller node — system where you write the code of Ansible and run the code of Ansible.

5. Managed node — System to whom you would like to configure by Ansible.

6. Inventory — Inventory is like a database having information of Managed Node/hosts IP’S. Provide Inventory to Ansible.

Note — That’s the reason why Ansible is known as “Agentless”. Because controller node is going to managed node so in this it don’t require any agents on manged nodes.

7. Idempotance-
Ansible first check the current state of package in managed node , if it is there then it will not install the package (that is our desire) . If it is not there then in this case it will install the package on managed node.

This concept is known as “idempotance”. Ansible works on “idempotance”.

8. Ansible use “ssh” protocol for maintaining connection between controller node and managed node only if both controller node and manged node have a Operating system “Linux”

9. Ansible intelligence comes from its module. There are many module available for Ansible for different different use case.

10. For automation through Ansible-
a. Command
Write the ansible commands one by one. These command sare known as Adhoc commands.
b. Script
Write all the command in a program file /script. This file is known as Playbook.

11. The language that is used to write commands in playbook is YAML.

12. In play book the commands we write are known as tasks. Task is for the host . Task is known as play. Multiple task/play we have for multiple hosts. That’s why file containing play/task is known as playbook.

13. For run the playbook of ansible , command is —

ansible -playbook “playbook-name”

< — — — — — — — — — — — — — — — — — — — — — — — — ->

❗ Ansible Task ❗

Write an 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 httpd container and expose it to the public

🔹 Copy the html code in /usr/local/apache2/htdocs directory and start the web server

Tools Required for the task-

  1. controller node configured with ansible
  2. Managed node configured with yum.

Lets start the process.

First let me show you that I don’t have docker installed and configured in managed node.

Now I am in my controller node from which I write code for Ansible.

/etc/ansible/ansible.cfg- is path of configuration file of ansible.

This config file contains the location of Inventory.

In inventory the IP, user name and password of manged node is written whom we want to configure.

I have created one directory in which I put my playbook for ansible and webpage that I have to copy in docker web container.

In playbook

  1. we have to first specify for which hosts/managed node you want to run this playbook .

For this “hosts” keyword is used. In hosts we have to assign IP of managed node or Hostgroup.

2. Now after this we have to specify what tasks we want to perform on hosts.

3. a. First we want to configure yum for docker. for this “yum_repository” module is used.

b. After this we want to install docker for this “package” module is used.

c. Now we want to start the services of docker . for this “service” module is used.

d. Now we have to run command on hosts for this “command” module is used.

e. After this we want that docker pull “httpd” image from docker hub for launching an os with this image . For this “docker_image” module is used.

f. After this we want to create one directory in hosts for copying webpage in it. For this “file “ module is used.

g. Now we want to copy the webpage from controller node to the directory in managed node . for this “copy” module is used.

h. Now after all this tasks we want to launch docker container . for this “docker_container” module is used.

The webpage that I have created is anurag.html

Now atlast we have to run one single command from which our managed node will be configure for the ansible task that is listed above.

“ansible-playbook task1.yml”

when you run this command . you will see this kind of output.

Now let me cross check that Ansible has performed all its tasks that is written for this managed node or not.

As you can see Ansible has performed all its task perfectly that is written in Playbook for this manged node.

Directory has been created, Webpage has been copied in this directory, Docker has been installed, Docker services has been started.

Yum has been configured for docker, Docker repo has been created.

This is the website that is running on docker container that has been launched by docker (in a managed node that has been configured by Ansible).

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Anurag Mittal
Anurag Mittal

Written by Anurag Mittal

Learning the new technologies like mlops, devops, hybrid multi cloud to enhance my skills.

No responses yet

Write a response