Configuring Webserver Inside Docker Container using Ansible

Kalla kruparaju
6 min readMar 11, 2021

--

Hello connections !!!!

Iam back with another article… In this article i will give complete overview to Configuration of Webserver inside the Docker Container using ansible….

What is Docker??

Docker is a opensource product which used to launch operating system(container) with in one second. Docker is a CLI interface product. Docker works on existing container technology which it is minimize it to cli commands.

What is Ansible?

Ansble is a tool for configuration management of operating system. Python is the base language for Ansible. Ansible is a Declarative language product works with idempotent nature. Ansible works on ssh protocol.

To install docker using Ansible refer following article

Steps to configure Webserver inside the Container using ansible

👉 Launch a container using docker.
👉 Install ssh server in the docker container.
👉 Install net-tools in the docker container.
👉 Start the services of the ssh
👉 enable the ssh services permanetly by adding the bashrc file
👉 Install passwd in the docker container
👉 Add some password to the root user
👉 Commit the docker image
👉 Push the image to docker hub
👉 Run the ansible playbook

In the above command you can see that no containers are running

You can see that we have two images one is ubuntu:14.04 image and another is centos:7 image

lauching a container with port nating with port num 22 exposed port with 2222 and launched the container with dockerssh container name and with centos:7 image.

You can see that a new container was launched it is running 4 seconds ago with dockerssh container and 22 port num exposed.

Installing the ssh server in the container with following command

yum install openssh-server -y

To use the netstat command and ifconfig command in the docker container For that we have to install net-tools with following command.

yum install net-tools -y

After installing the ssh server starting the service of the ssh you can see that it was failed we have generate the key. This is the rule of encryption that is you want to encrypt between two system then you have to see the key.

For that generate the key with the following command

ssh-keygen -A

Now again started the services of the ssh now you can see that port 22 in the process.

To start the services permantely add the cmd in the bashrc file.

At last install the passwd to change the password of the root user. Install the passwd with folllowing command

yum install passwd -y

In my case it was installed !!!

To add the password of the root user with the following command

passwd root

check ssh working or not by login with the remote user of the docker container with the following command

ssh  username@ip_address

you can see that it was working great !!!!

Created docker image with the docker commit command

You can see that new image was updated in the docker images

To push the image into docker hub login into the docker hub account with credentials

Now you can see that image was sucessfully pushed to the docker hub

- name: pulling docker image
docker_image:
name: "kallakruparaju/docker-ssh:v1"
source: pull

Above task will pull the docker image from the docker hub which image is pushed above

- name: launching container
docker_container:
name: "webserver"
image: "kallakruparaju/docker-ssh:v1"
detach: yes
state: started
interactive: yes
tty: yes
exposed_ports:
- "80"
- "20"
ports:
- "8080:80"
- "2222:22"

Above task will launch a docker container with above pulled image in the detach mode and with interactive terminal and port 80 ans 22 are exposed

- name: Retriving ip address of running Container
docker_container_info:
name: "webserver"
register: container

Above Task will retrive the ip address of above launched conatiner to update the inventory and register the data in the variable conatiner

- name: Updating Inventory file
blockinfile:
path: /home/kruparaju/ipadr.txt
block: |
[docker]
{{ container['container']['NetworkSettings']['IPAddress'] }} ansible_ssh_user=root ansible_ssh_pass=kruparaju ansible_connection=ssh

Above task will update inventory with the ip address of the container and update the username and password of the container which is given in the docker conatiner

- name: Refresh the inventory file
meta: refresh_inventory

By default inventory not update on the fly so inventory updated with the above task.

- name: Installing httpd
package:
name: "httpd"
state: present

Till now configuration done outside the docker container. just we launched the docker container and update the inventory in this task we installed the httpd webserver software

- name: Installing php
package:
name: "php"
state: present

Now php will install with the above task

- name: Copying the php file
copy:
content: >
<pre>
<?php
print `/usr/sbin/ifconfig`;
?>
</pre>
dest: "/var/www/html/index.php"

In the above task some php content will update in the index.php file

- name: Starting HTTPD Service
command: /usr/sbin/httpd

At last we started the services of the httpd in the docker systemctl doesn’t support we started the sevices by internal loading the file.

- name: Dynamic Docker Configuration
hosts: localhost
tasks:
- name: pulling docker image
docker_image:
name: "kallakruparaju/docker-ssh:v1"
source: pull

- name: launching container
docker_container:
name: "webserver"
image: "kallakruparaju/docker-ssh:v1"
detach: yes
state: started
interactive: yes
tty: yes
exposed_ports:
- "80"
- "20"
ports:
- "8080:80"
- "2222:22"
- name: Retriving ip address of running Container
docker_container_info:
name: "webserver"
register: container
- debug:
var: container.container.NetworkSettings.IPAddress
- name: Updating Inventory file
blockinfile:
path: /home/kruparaju/ipadr.txt
block: |
[docker]
{{ container['container']['NetworkSettings']['IPAddress'] }} ansible_ssh_user=root ansible_ssh_pass=kruparaju ansible_connection=ssh
- name: Refresh the inventory file
meta: refresh_inventory
- name: Configuring Webserver inside Docker container
hosts: docker
tasks:
- name: Installing httpd
package:
name: "httpd"
state: present
- name: Installing php
package:
name: "php"
state: present

- name: Copying the php file
copy:
content: >
<pre>
<?php
print `/usr/sbin/ifconfig`;
?>
</pre>
dest: "/var/www/html/index.php"
- name: Starting HTTPD Service
command: /usr/sbin/httpd

Now you can see that webserver configured successfully with ansible palybook

you can see while running the playbook a warning was prompted that no hosted available after the container launched ansible playbook retrived the ip address of the container and updated it in the inventory file.

you can see that webpage accessed sucesfully and one container with above pulled image with webserver name and port 22 and 80 exposed.

Thank you for reading article !!!!

Happy Learning 😊😊

--

--

No responses yet