Configuring docker web server using ansible

Arjun Chauhan
5 min readNov 28, 2020

--

What is Ansible?

Before I begin with the setup let’s discuss the importance of automation because that is what ansible is all about. Ansible is a tool developed in python that can be used to automate the tasks.

It is a very vital tool in today’s technical world . We can setup hundreds of systems all having different operating systems and technical details be it redhat, ubuntu or windows.

Ansible follows a declarative approach. We specify what we want to do and the ansible decided how to do.

In technical terms ansible is a software that allows software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

For example let’s say we need to download httpd server on our system. In RHEL7 we use yum command for this, in RHEL8 we use dnf, ubuntu uses apt, and apple users use brew command. If we have to setup the server manually then we have to know each command and how to execute it, but in case of ansible we just tell it that we need install a certain software and ansible understands how it has to do the action irrespective of the base OS it is working on.

So now we have the idea what ansible is. Today I would be setting up a docker on one of the target nodes. I would be creating a playbook. I would be explaining everything I do as I do

So before configuring anything let’s lay down some steps on what we got to do. The task is to configure docker.

  1. Configure the yum
  2. Setup the docker environment
  3. Install the docker image
  4. Start the docker container
  5. Setup the content of the website in the docker web server container

1. Mount the cdrom

This is a fresh new created RHEL8 system which we will use as target node. Before we do anything we would be configuring the yum repository since most of the softwares are available through yum.

For this we would create a new directory on which we will mount the optical drive of the RHEL containing all the softwares. The following commands in the playbook create a new directory /dvd1 and mount the cdrom on this directory.

The result is as expected. The green colour indicates that the task we wanted to do was already completed in the target node and the orange shade represents that the state of the system was successfully changed by our commands

2. Configure yum

Adding this piece of code to the existing yaml file will configure yum in the system. The “gpgcheck” is used to check the authenticity of the package which is being installed. I have made disabled this in my configuration.

To check if the yum is configured in the target node we can use the following command in the target node

yum repolist

The output would be something like this in the target node.

3. Configure the docker in the target

Now we would be downloading the docker software from the given url. Then we would be installing the docker image and starting the services

To check the services we can use following command in target node to check if the services are running. This command is exclusive to centos/ RHEL8 and depends upon the OS on the target node.

systemctl status docker

4. Install docker image

I would be downloading the centos image from docker hub.

Upon running this above playbook we get the below error.

By analyzing this error we come to know that we need to install the Python SDK for docker . So let us add this to the playbook too.

Now we used command module to install the docker SDK.

Now everything looks fine we can even check it by running the docker ps command on the target node which now shows the centos image that has been installed on the system.

Now the next task is to install the webserver on so for that we would be using an httpd image from the docker web.

5. Getting the content ready

Before we do anything we need to get our content ready in the server so that the client can view it.

I created a simple webpage for testing purposes. So now let’s send this content to the target node

6. Getting the Web Server ready

For getting the web server we would be using the httpd image i.e the apache webserver.

To see if it is running we can use the docker ps command in the target node.

So the services are up and running. So most of our work is done.

7. Hosting the content

We have transferred the content from to the managed node. Now the webserver needs to know the location of the these files . So let us launch a new container with all this taken into consideration.

The content that is hosted can be accessed using the ip address as the webserver is hosted using the httpd image.

The ansible has immense potential for making the work easier for anyone assigned to configuring and managing systems. Automation is not a technology that gets a boom and is unable to sustain on the long run in contrary it is a need of the industry that is only going to grow with time.

Thanks

--

--

Responses (1)