How to Configure Webserver on top of Docker Using Ansible

Chetna Manku
5 min readFeb 7, 2021

What is Docker?

Docker is a container-based open platform for developing, deploying, and running applications.

There many benefits of using Docker. One of main advantage is you can quickly create new containers if demand for your applications requires them. They start instantly.

Refer to this article for more information on Docker.

However, launching Docker containers manually on multiple systems… still so much work to do... So, to make this process more easy, we can automate this process by using Ansible.

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.

Ansible is an agentless automation tool that by default manages machines over the SSH protocol.

You only need to install Ansible on Controller Node. Controller Node then uses SSH (by default) to communicate with Managed or Target Nodes (those end devices you want to automate).

Refer to this article for more information on Ansible.

In this article, we are going to learn how Ansible can be used to Configure Docker and a Webserver on the top of Docker.

🔰Pre-requisites🔰

  • Ansible should be installed on your system.
  • Managed Nodes (systems on which you want to Configure Docker) should have Internet Connectivity.

To know more about installation of Ansible, refer to this article.

I am going to create Ansible Playbook following these steps:

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Pull the httpd server image from the Docker Hub

🔹 Run the docker container and expose it to the public

🔹 Copy the html code in /var/www/html directory and start the web server

🔰Ansible Document🔰

  • Ansible provides documentation of each module.
  • Use “ansible-doc” command to see any module’s description and required options.
ansible-doc <module_name> 
  • Ansible has several modules for managing Docker; a few of these are docker_image, docker_container, and docker_service.

🔰Creating Ansible Playbook🔰

Step-1 : Create Docker Repository

  • To install docker, first we need to configure yum.
  • Create “Docker repository” in hosts using “yum_repository” module.
- yum_repository:
name: "docker"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable"
description: "Docker Repo"
gpgcheck: no

Step-2: Install Docker

  • You can install Docker using “command” module or “package” module.
  • “Command” module directly runs the given command in hosts.
  • “Package” module will automatically
- command: yum install docker-ce --nobest -y

Step-3: Install Docker SDK

  • To handle docker containers from ansible, Docker SDK is needed to be installed in managed nodes.
  • To install Docker SDK using pip module, first you need to install Python on hosts.
  • Install python using “package” module.
  • Then install docker SDK using “pip” module.
- package:
name: python3
state: present
- pip:
name: docker

Step-4: Start and Enable Docker

  • Start and enable the Docker using “service” module.
- service:
name: docker
state: started
enabled: yes

Step-5: Pulling httpd image from Docker Hub

  • Pull “httpd” image from docker hub using “docker_image” module.
- docker_image:
name: "httpd"
source: pull

Step-6: Launching Docker Container with httpd image and Exposing it

  • For launching container, use “docker_container” module
  • For “volumes” option, first we need to create the folder in host using “file” module, then mount this folder within the container.
  • Use “exposed_ports” option, to expose the container.
  • Use “ports” option to publish exposed container port to Docker host (Host_Port:Conatiner_Port)
- file:
name: "/webfolder"
state: directory
- docker_container:
name: webserver
image: httpd
state: started
exposed_ports: "80"
ports: "8080:80"
volumes: "/webfolder:/usr/local/apache2/htdocs/"

Step-7: Copy webpages to /var/www/html directory in Conatiner

  • Copy the webpage to the folder that is mount within the conatiner.
- copy:
dest: "/webfolder/index.html"
content: "Webserver Configured on Docker Using ANSIBLE!!!"

Step-8: Setting Firewall Rule

  • Disable the firewall in the hosts so that client can connect to the webserver.
  • For disabling firewalld, you can either use “service” module or “firewalld” module.
- service:
name: firewalld
state: stopped

🔰Ansible Playbook🔰

  • While writing playbook, its a good practice to give “name” to every tasks.
- hosts: <IP_of_Host>
tasks:
- name: Creating Docker Repo
yum_repository:
name: "docker"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable"
description: "Docker Repo"
gpgcheck: no
- name: Installing Docker
command: yum install docker-ce --nobest -y
- name: Installing Python for Docker SDK
package:
name: python3
state: present
- name: Installing Python Docker SDK
pip:
name: docker
- name: Starting and Enabling Docker
service:
name: docker
state: started
enabled: yes
- name: Pulling httpd Image from Docker Hub
docker_image:
name: "httpd"
source: pull
- name: Creating folder for Mounting Docker Conatiner
file:
name: "/webfolder"
state: directory
- name: Launching Docker Container with httpd Image & Exposing it
docker_container:
name: webserver
image: httpd
state: started
exposed_ports: "80"
ports: "8080:80"
volumes: "/webfolder:/usr/local/apache2/htdocs/"
- name: Copying webpage to Docker Container
copy:
dest: "/webfolder/index.html"
content: "Webserver Configured on Docker Using ANSIBLE!!!"
- name: Setting firewalld Rule
service:
name: firewalld
state: stopped

🔰Running Ansible Playbook🔰

  • To run ansible playbook, lets first check the connection to the managed nodes.
ansible <IP_of_managed_node> -m ping
  • Before running playbook, you can also run the following command for syntax checking
ansible-playbook <Playbook_name> --syntax-check 
  • Run the playbook using the following command
ansible-playbook <Playbook_name>
  • Playbook run successfully!

🔰After Running Ansible Playbook🔰

  • After playbook run successfully, you can go to managed nodes and verify the results.
  • You can run these following docker commands
  • Docker is Installed and Service is Started.
  • Docker “httpd” image is downloaded.
  • Container named “webserver” is launched.
  • Docker Container is exposed to port number “8080” of Docker Host.
  • Webserver is connecting on 8080 port of host.

🔰Connect to Webserver🔰

  • Client is able to connect to Webserver!

To sum up, using Ansible with Docker can significantly simplify your processes by allowing you to work with containers and to automate all that work! It’s no wonder the Ansible-Docker combination is so popular.

--

--