Ansible and Docker... Hosting Website on Docker Container using Ansible
Ansible is an Open-Source IT Automation Engine which is used to automate application deployment, infrastructure automation, cloud provisioning and many IT services. Ansible improves the scalability, consistency & reliability of the IT environment.
Docker is an OpenSource containerization platform for Building, Shipping and Running applications using Container Virtualization technology. Docker packages all the dependencies in form of a Docker containers to ensure that our applicaton works seamlessly in any of the environment.
Hosting Website on Docker Container using Ansible Playbook
1. Adding the Docker repo to yum
- yum_repository: name: "Docker" description: "Docker repository for RHEL8" baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/" gpgcheck: no enabled: yes
It will configure the yum on our device as we need to all the repository to yum before installing docker.
2. Installing the docker-ce into our system
- package: name: "docker-ce:18.03" state: present
It will install the docker-ce software in our system.
3. Starting the Docker services
- service: name: "docker" state: started
After installing the software, to use the services provided by the software, we need to start it. Following commands will enable the docker services in our device.
4. Installing python on our remote host
- package: name: "python36" state: present
Python needs to be installed as working with docker container and images via ansible requires some pip packages to be installed and pip is present inside the python software.
5. Installing pip modules
- pip: name: - "requests" - "docker"
Installing the requests and docker pacage from the pip software library as required by Ansible for proper functioning.
6. Pulling httpd image from DockerHub
- docker_image: name: "httpd" source: pull
Pulling the httpd image from the DockerHub to be used. The pull source stated to use the default source. i.e. DockerHub.
7. Copying website to the remote host
- copy: src: "index.html" dest: "/var/www/html"
Copying a single webpage from our local host to remote host. Complete folder can also be copied similarly.
8. Running the docker container services and attaching the website
- docker_container: name: "task_10" image: "httpd" ports: "82:80" state: started detach: yes volumes: "/var/www/html/:/usr/local/apache2/htdocs/"
Following command will run the httpd container and will expose the 80 port of our container to the 82 port of our device.
The complete playbook is available on my GitHub repository https://github.com/kshitizsaini113/Ansible-Playbooks/blob/main/docker.yml
Running the Ansible playbook
Verifying the status from remotehost