Configure Web Server inside Docker Container through Ansible
By: Vikas Verma
Tags: Ansible, Ansible Playbook, Docker

ansible.com

We will see how to 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 docker container and expose it to the public.

🔹 Copy the html code to the document root and start the web server.

So first, and obviously, ansible should be installed in the system (which is our contoller node), here I am using RedHat8 VM.

We should have a configuration file for ansible (/etc/ansible/ansible.cfg), where we have given the path of our inventory file (which contains information of our managed/target nodes). We added ‘host_key_checking = False’ so that when first time ansible connects to target it directly accept the connection without checking any key

[defaults]

inventory = /myhosts/hosts.txt

host_key_checking = False

An inventory can look like this, here I added one target node

Now lets see the tasks in the playbook that will do the complete setup,

first using the yum_repository module of anisible we created a repo for installing docker-ce.



yum_repository:

name: docker

description: repo for docker

baseurl:

gpgcheck: no - name: Adding docker repositoryname: dockerdescription: repo for dockerbaseurl: https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck: no

2. then with yum module we are installing docker-ce, skipping the unsopported packages.

- name: Installing docker-ce

yum:

name: docker-ce-18.09.1

state: present

skip_broken: yes

3. service module to start the docker services.

- name: Start docker service

service:

name: docker

state: started

enabled: yes

4. package module to install python36 which will provide us pip3.

- name: Install python36 to provide pip3

package:

name: python36

5. Now using pip module to install the docker library of python.

- name: Install python docker library

pip:

name: docker

6. We use docker_image module to pull httpd docker image.

- name: Pull httpd image

docker_image:

name: httpd

source: pull

7. Using file module to create a directory at target node

- name: Create folder to copy code

file:

state: directory

path: /container_code

mode: '0755'

8. copy module to copy some content in a file to the created directory in the previous task.

- name: Coping code

copy:

content: "<h1>Hello... This is new page</h1>"

dest: /container_code/index.html

9. firewalld module using which we are allowing a port 81 through the firewall

- name: Allow port through firewall

firewalld:

port: 81/tcp

state: enabled

permanent: yes

immediate: yes

10. docker_container to launch a container in detached mode, linking the folder that we created (containing our code) to the document root of apache and exposing the port 80 (on which httpd server is running) so can access it ussing port number 81 in base system (that’s why we added port 81 in the firewall rule)

- name: Launching a new container

docker_container:

name: WebServer

image: httpd

detach: yes

state: started

volumes: /container_code:/usr/local/apache2/htdocs/

ports:

- 81:80

Here is the complete playbook,

Now, running the created playbook

ansible-playbook your_playbook.yml

As we can see it is perfectly working (using the ip of base OS (host)on which docker is installed and with port number 81),

Thank you 😀 !