Apache Webserver Configuration in Docker using Ansible
Hey everyone, this article shows show how to setup a docker container and configure webserver inside the container using ansible. Before we go further let us know about few terminologies
What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
Using Docker, you can spin up instances within seconds as per your requirements and run your applications.
What is Ansible?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually.
Ansible is agentless so we don’t have to configure any software on managed nodes.Ansible uses playbooks which are written in YAML language.
How Ansible works?
Ansible works by connecting the server of the user through the SSH keys and pushing the small programs called modules. With the use of modules, the playbooks help the ansible clients in performing all the specific tasks. The particular functions can include the service of restarting, installing packages, rebooting servers, and much more.
The machine on which Ansible is installed is known as controlled node. Playbooks and Ad-hoc commands can be run on controlled nodes to
Managed Node: The machines which are managed or configured by ansible are called managed nodes.
All the machines you’re using with Ansible are listed in a single simple file, along with their IP addresses, username, passwords etc. The inventory stores information of managed nodes.
Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. .Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.
Modules are like small programs that Ansible pushes out from a control machine to all the nodes or remote hosts. The modules are executed using playbooks and they control things such as services, packages, and files.
Now lets us start with our task of configuring apache webserver in docker using ansible. The tasks involves the following steps:
🔹 Start and enable Docker services
🔹 Pull the httpd server image from the Docker Hub
🔹 Run the httpd container and expose it to the public
🔹 Copy the html code in /var/www/html directory and start the web server.
I have already installed ansible on my controlled node. But if its not installed we can installed using:
pip3 install ansible
To see the version of ansible installed :
To see all the managed hosts:
ansible all --list-hosts
To check connectivity with managed hosts, type the following command:
The following code is written in the playbook for the configuration of webserver:
To run this playbook, we need to give : ansible-playbook playbookname.yml
To check that docker is configured succesfully, we can run the follwoing commands in managed node:
we can also check the IP of the container using the following command:
We can use this IP to connect to the webserver and see our webpage:
Hence task is completed successfully!!