Deploying Docker with Ansible

Divya Kurothe
5 min readDec 28, 2020

--

In the competitive business environment and growing customer demands, companies strive to quickly deliver reliable software products to the market, as well as constantly improve them. In DevOps, as we know development and operations work is integrated. This integration is very important for modern test-driven application design. Hence, Ansible integrates this by providing a stable environment to both development and operations resulting in smooth orchestration.

What is Ansible? Ansible is an open-source IT automation engine, which can remove drudgery from your work life, and will also dramatically improve the scalability, consistency, and reliability of your IT environment. You can use Ansible to automate three types of tasks:

  • Provisioning: Set up the various servers you need in your infrastructure.
  • Configuration management: Change the configuration of an application, OS, or device; start and stop services; install or update applications; implement a security policy; or perform a wide variety of other configuration tasks.
  • Application deployment: Make DevOps easier by automating the deployment of internally developed applications to your production systems.

Ansible can automate IT environments whether they are hosted on traditional bare metal servers, virtualization platforms, or in the cloud. It can also automate the configuration of a wide range of systems and devices such as databases, storage devices, networks, firewalls, and many others.

Now the next question that might arise is why Ansible? There are many other IT automation tools available, including more mature ones like Puppet and Chef, so why would you choose Ansible? The main reason is simplicity. Michael DeHaan describes it as “a tool that you could not use for six months, come back to, and still remember.” It uses YAML, a simple configuration language. Puppet and Chef, on the other hand, use Ruby, which is more difficult to learn. This makes Ansible especially appealing to system administrators.

Here we will 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 in /var/www/html directory and start the web server

So let’s begin with the solution part…

First of all, we need to install Ansible in our rhel8. To install the ansible we use this command:

pip3 install ansible

We can check its successful installation by see the version using

ansible --version

After the installation we have to configure the ansible to use it and for that we have to create a file inside the /root/ folder to define the managed nodes. So I have created a ip.txt file inside which we have to mention-

<ip_of_managed_node> ansible_user=<name> ansible_ssh_pass=<password>

Now we have to create the file with the name of ansible.cfg inside /etc/ansible/ folder to use this file as a configuration file and we have to give the inventory name(ip.txt in my case) inside this file. Then we have to pass the SSH Key authentication , So we have to make false this option so that there will no problem while doing the authentication. After we have to install the SSH pass with help of yum command.

We can list the hosts and check the connectivity using following commands:

ansible all --list-hosts
ansible all -m ping

Now we are ready to write the code for the docker deployment.

In playbooks we have to perform set of tasks with different ansible modules to get the docker installed and deploy a webpage. For this first we will configure yum repository and then install docker with the help of package module.

Now we have to enable to docker services and install the sdk for docker using the pip3 and for getting pip3 command we have to install python3 using package module.

After the successful installation of docker & we have to pull to docker image from the Docker hub, the official repository for the docker images. Here we are pulling the official latest image of httpd to host our webpage. Then we create a folder in our remote system to copy the code from our controller node and mount it to the docker container .

At last we have to launch the docker container with the help of ansible playbook and expose the ports to access it from internet. We mount the volume to /usr/local/apache2/htdocs/ which is a default folder that will used to access by the webserver inside the docker container.

With this code writing part is done and now we have to run our playbook with the command:

ansible-playbook <file name>

We can now go to our target node and check whether all tasks are done successfully or not. Here ‘ls’ command listed ansiblews, the directory we have created in the code and ‘docker images’ and ‘docker ps’ cmd to show the pulled image and the running container in the node. Also here I’ve used ‘curl’ command to see the content of my html webpage.

That’s all folks, hope you enjoyed it. Thanks for reading :)

--

--

No responses yet