Webserver configuration over Docker using Ansible

Anshika Sharma
5 min readNov 28, 2020

--

Integrating Apache webserver with Docker through Ansible

In this article, we are going to setup Apache server over a docker container using Ansible.

Our Plan :

Write an Ansible Playbook that does the following operations in the managed nodes:

  1. Configure Docker.
  2. Start and enable Docker services.
  3. Pull the httpd(Apache) server image from the Docker Hub.
  4. Run the docker container and expose it to the public.
  5. Copy the html code in /var/www/html directory and start the webserver.

So lets begin the same, before starting we have some prerequisite setup to be done, that is :

  1. Two VM (In my case RHEL-8)
  2. Ansible setup (on the node that is going to be our Controller node).

Implementation

Lets begin the implementation with setup of Ansible.

Step 1. Setup Ansible

a). Install Ansible using command

pip3 install ansible
Installing Ansible

Note: In my case, it is showing requirement already satisfied because in my system Ansible is already installed.

b). Create an inventory file

vi hosts.txt

you can give any name to this file.

c). Configure the Ansible

To configure ansible, create a directory named ansible in /etc directory then create the ansible configuration file i.e., ansible.cfg

mkdir /etc/ansible
cd /etc/ansible
vi ansible.cfg
Configuring ansible

d). Add host to the inventory file, in the format

192.167.12.21 ansible_user=username ansible_ssh_pass=your_password ansible_connection=ssh

Replace the IP, username and password as per your target node configurations and check the list of hosts using

List of hosts available

After setting up the inventory, check the connectivity to the hosts using command

ansible all -m ping 
Connected to he target node

Step 2. Configuring yum

To configure yum, we need to create the ansible playbook

Code to configure yum

On execution through ansible-playbook command, it will be successfully created.

ansible-playbook -v dockerSet.yml

Here i am using single level verbosity to get some extra information.

Successful execution of yum repositories

Step 3. Configure the docker repository

Code to setup docker repository
successful setup of docker repository

Step 4. Install docker software

Installing docker
Successfully installed docker

Step 5. Start the docker service

Before starting the docker service, we need to install a python package named docker-py in the target node as python is the dependency for Ansible. By installing docker-py, the system will understand that docker is using the correct and same version of python as used by the system.

To install the python package, we will use pip module

installing docker
successfully installed docker package

Now, we will start the docker service

Code to start docker
Successfully started the docker service

Step 6. Pull the docker image, here we require httpd

code to pull docker image
Successfully pulled httpd docker image

Step 7. Create a docker container with Apache webserver over it

Code to create Apache webserver

Here i have linked the /webpages/ path to the main path or the default path for the server files of Apache web server.

Successfully configured httpd server over docker container

Step 8. Copy an index.html file to the /root/ directory

Code to copy the content to index.html file
Successfully stored the data

Step 9. Add a firewall rule

Here we need to add a firewall rule so that the port 85 that we have used above can be accessible to public.

code to set firewall rule
Successfully setup the firewall

Step 10. Go for the web page

Successfully Setup the web server

Hence, we have successfully setup a webserver on a docker container using ansible. Also the complete thing is being done with one single command.The final code will be :

- hosts: all
tasks:
- file:
path: "/dvd2"
state: directory
- mount:
path: "/dvd2"
src: "/dev/sr0"
fstype: "iso9660"
state: mounted
- yum_repository:
name: "yumrepo1"
description: "yum repo to access AppStream"
baseurl: "file:///dvd2/AppStream"
gpgcheck: no
- yum_repository:
name: "yumrepo2"
description: "yum repo to access BaseOS"
baseurl: "file:///dvd2/BaseOS"
gpgcheck: no
- yum_repository:
name: "dockerrepo"
description: "yum repo to install docker"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck: no
- yum:
name: "docker-ce-3:18.09.1-3.el7.x86_64"
state: present
enablerepo: "dockerrepo"
- file:
path: "/webpages"
state: directory
- copy:
dest: /webpages/index.html
content: '<html><body><h1>Task10 Completed Successfully !!!</h1></body></html>'
- pip:
name: "docker"
- service:
name: "docker"
state: started
enabled: yes
- docker_image:
name: "httpd"
source: pull
- docker_container:
name: "os32"
image: "httpd"
state: started
detach: yes
ports: "85:80"
volumes: /webpages:/usr/local/apache2/htdocs/
- firewalld:
port: "85/tcp"
permanent: yes
state: enabled
immediate: yes

To execute this code, we use the command :

ansible-playbook -v dockerSet.yml

Finally!! with this code we can setup hundreds or thousands of nodes within few minutes.

I hope this will be helpful to you to explore more with Ansible!!!🤗🤗

💫Keep learning, keep sharing !!💫

--

--

Anshika Sharma
Anshika Sharma

Written by Anshika Sharma

I am a tech enthusiast, researcher and work for integrations. I love to explore and learn about the new technologies and their right concepts from its core.

No responses yet