Configuring Docker container and launching HTTP Web-Server using Ansible

Nikhilgp
3 min readApr 8, 2021

Ansible

Ansible is an open source software Provisioning, Configuration Management and Application Deployment Tool enabling infrastructure as code. It runs on Unix like system and can configure both Unix as well as Microsoft windows Systems.

Docker

Docker is a software platform for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. Containers, by contrast, isolate applications’ execution environments from one another, but share the underlying OS kernel. They’re typically measured in megabytes, use far fewer resources than VMs, and start up almost immediately.

Objectives:

To 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 server image from the Docker Hub
  4. Run the docker container and expose it to the public
  5. Copy the html code in the document root directory
  6. Then start the web server

Step 1: Creating an Ansible configuration file, in this practical we are using Amazons ec2 Service.

Step 2: Set-up your Ansible Inventory file.

Step 3: Creating the Ansible Playbook with the following tasks.

  1. Create yum repo for docker
  2. Install Docker
  3. Start and Enable Docker Services
  4. Install Python and docker-py to Support Ansible Docker module
  5. Pull httpd image using docker module
  6. Run the Httpd container and start service
  7. Create an handler to copy the html code to docker container.

Here we have used command module of the ansible to install Docker, the reason is as we are using RHEL8 as our managed node and we are installing docker forcefully we use option called “nobest” and this option is not available in the package module.

Step 4: Executing the Playbook using command

ansible-playbook (name.yml)

THE HTTPD WEB SERVER IS PATTED TO THE PORT NUMBER 8080 SO WE CAN OPEN OUR WEB SERVER RUNNING ON DOCKER CONTAINER ON PORT 8080.

Thank you for reading.

--

--