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.
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:-
- Bare metal
- 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?
- Configure the package manager Repository so that package manager can download docker.
- Download and Install docker.
- Start Docker or in simple term lunch Docker application.
- 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 .
- Python have an API by using which python can interact with Docker. So Download and Install docker API.
- Now Download Docker Image called httpd which have web server already installed.
- Create Directory At managed node So that we can copy our web page there.
- Copy web Page
- 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.
- hosts: web
- name: configure docker repo
description: Docker repository#step:- 2
- name: install docker
state: present#step:- 3
- name: start docker
state: started#step:- 4
- name: install python
state: present#step:- 5
- name: Install python Docker Api
name: “docker”#step:- 6
- name: pull docker image
source: pull#step:- 7
- name: create directrory to store web pages.
state: directory#step:- 8
- name: copy web pages
dest: “/root/webpages/”#step:- 9
- name: lunch container
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:-
When run above playbook you will see output something like this:-
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.
In Web Browser type
and you will see web page whatever you have created like I create web page shown below.
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.
Hi I am Ayush garg.I am Engineer and I believe in simplicity. I alway try to make things simple as much as possible.
If you like it then Please Clap & Share ..
Thank you EveryOne For reading .!!