Configuring web server on Docker Via Ansible
--
Hello everyone, you all might have seen the power of docker, a container managing software by directly. But to use it with remote systems isn’t an tedious task when you have to configure same type of configuration in different-different flavours of OS’s
Yes it is tedious task so, in this article I would like to explain how to use ansible a configuration management tool, to manage docker.
The pre-requisites are that you should have ansible configured on the system and you are all set.
The task is as below :
🔹 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
Now, let’s write task for for configuring the docker,
- name: Docker repository conf
yum_repository:
name: dockername
description: desc for docker
baseurl: https://download.docker.com/linux/centos/8/x86_64/stable/
gpgcheck: no
Checking if docker is already present
- stat: path=/usr/bin/docker
register: docker_present
this task will install the docker library of python
- name: install python3 docker sdk
pip:
name: docker
Installing docker via yum it will not install if it is already installed
- name: install docker
command: "yum install docker-ce --nobest -y"
when: not docker_present.stat.exists
Now finally starting the docker service
- name: start service for docker
service:
name: docker
state: started
enabled: yes
Creating a folder on the host docker container so that later we can transfer the files from there to the container
- name: "Creating folder in docker host"
file:
path: "/web"
state: directory
Copying the files from the controller node to the managed node
- name: "Copying the files in the host system"
copy:
src: "index.html"
dest: "/web"
Finally creating and starting the container from the httpd image of docker
- name: "Creating OS from httpd image"
docker_container:
name: "ansible_apache"
image: "httpd"
state: started
exposed_ports:
- "80"
ports:
- "8844:80"
volumes:
- /web:/usr/local/apache2/htdocs/
Full playbook
We have now successfully launched the webserver on top of docker. to see whether it is a success or not go to the managed node and type the following command.
docker inspect <name of image> | grep IP to get the IP address of container
curl <IP> to access the content of web page
Thanks for reading 🔥🔥😊😊