Webserver configuration over Docker using Ansible

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:

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


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 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
- 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 !!💫

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store