Automate Docker With Ansible for launching WebServer

Automate Docker With Ansible for launching WebServer

Ansible is one of the most powerful tool for configuration management. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

This is a small task in which we learn to launch our own Webserver using Docker and Ansible that use the YAML file to automate the process.

Task Description:

Write an Ansible PlayBook that does the following operations in the managed nodes:

  • Configure Docker.
  • 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 webserver.

Installing Ansible in Linux:

I'm using RHEL 8 here. You only need to have Python3 software installed. Ansible uses ssh to go inside the host OS so we also need one more software sshpass. Run these two commands to install Ansible...

pip3 install ansible
yum install sshpass

Configuration of Ansible:

Now we need to create one file where we give IP of our host server...

No alt text provided for this image

Ansible uses this file for check hosts OS and after that Go inside the OS using username and password. This also known as Inventory File.

After that, create one config file for Ansible where we give the location of the host file which we created above. Go to /etc and create one folder named ansible. Go inside the folder and create one config file named ansible.cfg

mkdir -p /etc/ansible
cd ansible
gedit ansible.cfg
No alt text provided for this image

Now for check that Ansible is working fine, Run...

ansible --version
No alt text provided for this image

This is the output of the command.

For checking connectivity that all host node is properly connected, RUN

ansible all -m ping

Now let's come to the task...

We create one file named webserver.yaml which also known as Ansible PlayBook. In this, we write vars and tasks which we fill as we learn further.

- hosts: web
  vars:
    #varibles go there
  tasks:
    #tasks go there

Variables:

These are the variables which we use in our playbook.

  vars:
    - image_name: httpd
    - file: home.html
    - docker_volume: /webpage/
    - docker_host_port: 8080
    - docker_container_port: 80
  • Configure Docker:

For this, we need to Configure Docker Repo First...

    - name: Add Yum Repo
      yum_repository:
        name: docker
        description: Docker Yum Repo
        baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
        gpgcheck: no

Install Docker Task...

Redhat by default don't provide Docker Software now. So we need --nobest option in command for installing Docker.

In ansible, Yum module in Ansible doesn't have this argument till yet, so I use the command module for doing the installation.

    - name: Install Docker-CE
      command: yum install -y  docker-ce --nobest

Install Docker Requirement on hosts...

We need docker-py module to run docker from ansible tool.

    - name: Install Docker Requirement On Host
      command: pip3 install docker-py
  • Start and Enable Docker service:

This task start and enable the Docker Service in hosts.

    - name: start docker services
      service:
        name: "docker"
        state: started
        enabled: yes
  • Pull the Httpd Image from Docker Hub:

This task pulls the docker image to launch the Container.

    - name: pull an image
      community.docker.docker_image:
        name: "{{ image_name }}"
        source: pull
  • Task for Copy Html code from controller node to managed Node:
    - name: Copy HTML code to Hosts
      template:
        src: "{{ file }}"
        dest: "{{ docker_volume }}"

This code copy the HTML file to the managed Node.

  • Launch the Container and exposed it to outer world:
    - name: Run docker container
      docker_container:
        name: webserver
        image: "{{ image_name }}"
        interactive: yes
        volumes: 
          - "{{ docker_volume }}:/usr/local/apache2/htdocs"
        ports:
          - "{{ docker_host_port }}:{{ docker_container_port }}"
        command: httpd -D FOREGROUND

Final PlayBook:

When put the above code in one file...

- hosts: web
  vars:
    - image_name: httpd
    - file: home.html
    - docker_volume: /webpage/
    - docker_host_port: 8080
    - docker_container_port: 80
  tasks:
    - name: Add Yum Repo
      yum_repository:
        name: docker
        description: Docker Yum Repo
        baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
        gpgcheck: no
    - name: Install Docker-CE
      command: yum install -y  docker-ce --nobest
    - name: start docker services
      service:
        name: "docker"
        state: started
        enabled: yes
    - name: Install Docker Requirement On Host
      command: pip3 install docker-py
    - name: pull an image
      community.docker.docker_image:
        name: "{{ image_name }}"
        source: pull
    - name: Copy HTML code to Hosts
      template:
        src: "{{ file }}"
        dest: "{{ docker_volume }}"
    - name: Run docker container
      docker_container:
        name: webserver
        image: "{{ image_name }}"
        interactive: yes
        volumes: 
          - "{{ docker_volume }}:/usr/local/apache2/htdocs"
        ports:
          - "{{ docker_host_port }}:{{ docker_container_port }}"
        command: httpd -D FOREGROUND

Running the PlayBook:

Command for run playbook...

ansible-playbook webserver.yaml

No alt text provided for this image
No alt text provided for this image

Testing the results:

Run IP:port(192.168.43.19:8080) for checking that your webserver working fine or not...

No alt text provided for this image

Task Completed??

GitHub Repo: https://github.com/gaurav-gupta-gtm/Webserver-Docker-Ansible

Thanks for Reading...

For any Query or Suggestions, feel free to DM me...?

Akarsh Srivastava

Senior Software Engineer @Axtria

4 年

Interesting!!!

Anubhav S.

Graduate intern @ Dell | Mtech(CS) @ IIITL

4 年

Great work????

要查看或添加评论,请登录

Gaurav Gupta的更多文章

社区洞察

其他会员也浏览了