Let’s Install and Configure Docker using Ansible

Ayush Garg
5 min readJan 13, 2021

Hey Guy’s In this article I am going to show you How you can install and Configure Docker using Ansible plus I am also going to demonstrate how you can run server inside Docker container.

Integration of docker and ansible

For the better understanding of this article you must have basic knowledge of any container technology and Ansible. But if you don’t have any idea what is container technology and docker don’t worry let me brief you in simplest way.

What is Container Technology and Docker?

If you are reading this article you are using some kind of web browser it might be Google Chrome, mozilla firefox, safari, Microsoft edge, opera browse rect. Web browser is software or you might say an application. To run browser or any application you need some operating system. Without Operating System you can’t do anything in your computer or laptop.

Now There are generally Four way of installing Operating System:-

  1. Bare metal
  2. Virtualization
  3. Cloud
  4. Container technology

Now let’s understand all the above Four terminology one by one:-

Bare metal:- In this way of installing Operating System. OS took Full control over all the resource or you might say hardware that our machine provides and won’t allow other OS to run. Resource like RAM, Hard Disk, CPU, Graphic card, NIC, etc.

Virtualization:- You might heard about the software like Virtual Box, VMware, Etc. By using these software you can install and run another Operating System over pre-installed operating system. In layman’s terms Our Base OS allocate some resource to these (VirtualBox, VMware)software like it base allocate resource to other software like browser, text editor, media player, etc.

Cloud:- You might heard about renowned public cloud provide like AWS or GCP or Azur. These cloud provider provide you a service by using which you can provision or in layman’s term install OS over cloud. If you don’t have any idea what is cloud I do recommend you to read my article Let’s understand Cloud Computing and Some Case Studies.

Container technology:- The OS we install using Bare metal or Virtualization look considerable amount of time, memory and space to install and run and with Virtualization you can only install and run some limited amount of Operating system like 2 or 4 depends on your RAM size. Now What if you need to install and run 10 or 15 Operating system within in a minute and have only 2 or 4 GB RAM. You can’t do this using Bare metal or Virtualization But with the help of Container Technology you can do all this. More over you can perform some testing without harming your base OS in anyway and destroy or delete that OS with in a second. Docker is Product of container technology.

By using docker which is product based on container technology you can lunch Operating system. And in docker Operating System is known as docker container.

NOTE:- Guy’s the above term I explained you in simple way so that you can easily understand. Remember they are not an actual definitions.

Before I move ahead if you don’t have any idea about ansible or are facing some problem in the installation and configuration of ansible I do recommend you to read my article Let’s configure Ansible on Local machine, Cloud and Container

Let’s install Docker and lunch web server.

With the help of Ansible you can automate the installation of Docker and web server inside container but You should know the manual step. Without the knowledge of Manual steps it will be harder to do automation. So what could be the manual steps?

Steps:-

  1. Configure the package manager Repository so that package manager can download docker.
  2. Download and Install docker.
  3. Start Docker or in simple term lunch Docker application.
  4. Because we are going to lunch container inside which web server will run and ansible developed using python so we are going need python in managed node so that can we interact with Docker using ansible. So Download and Install Python .
  5. Python have an API by using which python can interact with Docker. So Download and Install docker API.
  6. Now Download Docker Image called httpd which have web server already installed.
  7. Create Directory At managed node So that we can copy our web page there.
  8. Copy web Page
  9. lunch web Container.

That’s it these are the manual step you need to do but with the help of ansible you can automate this. In a single click you do all the above stuff.

PlayBook:-

- hosts: web
tasks:
#step:- 1
- name: configure docker repo
yum_repository:
name: DockerRepo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
description: Docker repository
#step:- 2
- name: install docker
package:
name: “docker-ce-3:18.09.1–3.el7”
state: present
#step:- 3
- name: start docker
service:
name: “docker”
state: started
#step:- 4
- name: install python
package:
name: “python36”
state: present
#step:- 5
- name: Install python Docker Api
pip:
name: “docker”
#step:- 6
- name: pull docker image
docker_image:
name: “httpd”
source: pull
#step:- 7
- name: create directrory to store web pages.
file:
path: “/root/webpages”
state: directory
#step:- 8
- name: copy web pages
copy:
src: “/root/ansibleWS/webpage/”
dest: “/root/webpages/”
#step:- 9
- name: lunch container
docker_container:
name: “serveros”
image: “httpd”
volumes:
/root/webpages:/usr/local/apache2/htdocs/
exposed_ports:
- “80”
published_ports:
- “1234:80”

Playbook write in YML and in the YML indentation is really matters. So when you run the above playbook keep the indentation proper else you are going to face whole set of indentation error.

To Run Playbook run following command:-

ansible-playbook <playbookname>.yml

When run above playbook you will see output something like this:-

Output

To check if the web page you uploaded and web server running properly or not run ifconfig command at managed node to get the IP.

ifconfig 

In Web Browser type

<ip>:1234

and you will see web page whatever you have created like I create web page shown below.

Sample Web Page

And Voila you have deployed a web page just in Single click.

You might come across some more error or exception which you may not able to troubleshoot own your own or might have some more Queries, Suggestion’s so Feel Free to Connect to me On Linkedin or comment below.

If you like it then Please Clap & Share ..

Thank you EveryOne For reading .!!

--

--

Ayush Garg

I am Engineer. I believe in simplicity. Life and stuff are already complicated so Why make it more complicated. I try to make things simple as simple as I can.