Automate Docker With Ansible for launching WebServer

kanav Gupta
2 min readDec 16, 2020

--

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.

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

nstall 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
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
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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response