Is it possible to run a Docker container using Ansible playbook !?

Before getting started let’s get familiar with the above technical terms:

Docker is a containerization technology that allows to download, install and provision the Operating System in just a blink of an eye. Docker makes development efficient and predictable. Docker takes away repetitive, mundane configuration tasks and is used throughout the development lifecycle for fast, easy and portable application development — desktop and cloud. Docker’s comprehensive end to end platform includes UIs, CLIs, APIs and security that are engineered to work together across the entire application delivery lifecycle.

Ansible is an open source automation platform. It is a simple automation language that can perfectly describe an IT application infrastructure in Ansible Playbooks. It is also an automation engine that runs Ansible Playbooks. Ansible can manage powerful automation tasks and can adapt to many different workflows and environments. At the same time, new users of Ansible can very quickly use it to become productive.

Now, you might be thinking how these two technologies are related to each other. Both these tools are very important in the DevOps world and for building a pipeline, be it a CI/CD pipeline or DevOps Assembly Line, these tools are very important.

Without further ado, let’s directly jump to the practical:

I have assumed that ansible target node is not yet configured with the docker services. So listing down the steps we need to do:

  • 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 default webserver directory and start the web server

In the Inventory file inside the controller node, enter the target node’s IP with username, password and connection type.

We can check the connection between controller and target node using module.

Now, writing the ansible-playbook and I’ll break down the playbook into two parts:

Setting up docker services:

Here the host keyword is used to tell Ansible where the playbook is to be executed, as it can be seen it will be executed on the group “ansible_target” which was used before. Also, I have used “vars_prompt” to make the code more dynamic and allow the user to enter the path of directory where the webserver pages are to be stored.

Configuring the yum repository: Here as my target node is RHEL8 so I have to configure yum so that it can install docker. I have used the ‘yum_repository’ module of ansible to configure to the yum repository.

Installing Docker: Here I have used the ‘command’ module to install docker, this module will execute the command on the target node like we do on the terminal.

Starting the Docker service: Using the ‘service’ module to start the docker service.

Installing the ‘Docker’ module of pip: This is a dependency of the ‘docker_container’ module which will be used later in the playbook, so installing it using the ‘pip’ module.

Here I have created a directory on the node on location {{dvddir}} which will be entered by the user, this directory will be mounted onto the ‘/usr/local/apache2/htdocs’ directory from where the httpd container picks files files to be hosted on it.

Creating workspace for the files: Here I have used the ‘file’ module to create a directory which will be mounted will launching the container.

Copying the files: Now copying the files on the directory created using the ‘copy’ module. Here I have used the ‘src’ key where we have to give the location of the file from the controller node.

Launching the container: Now that everything is done, we will launch the container. Here as it can be seen the image that will be used will be used will be httpd and it will be downloaded from Docker hub if it is not present on the node. Here the ‘ports:8090:80’ signifies port forwarding(any request that will come on the 8090 port of the managed node will be forwarded to port 80 of the docker container). Files will be provided from the /usr/local/apache2/htdocs which is mounted with {{dvddir}} as explained before.

Now running the playbook using the ‘ansible-playbook <filename>’ command.

Here the yellow color signifies that there were some changes done by ansible while green that none were done, this shows the idempotent nature of ansible.

Now we can see that the managed node is configured and the container has been launched.

Output on the Web Browser:

AND THE PRACTICAL IS COMPLETE !!

I will discuss some technologies in the further blogs, till then have a read at this.

Signing off

Link for my LinkedIn profile: