Configuring Docker using Ansible

Nityachawda
4 min readJan 24, 2021

--

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

What is Ansible ?

Ansible is an open source automation platform. It is very, very simple to set up and yet powerful. Ansible can help you with configuration management, application deployment, task automation.

So, Let’s Get started with our task.

Task Description

🔅 Configure Docker through ansible.
🔅 Start and enable Docker services.
🔅 Pull the httpd server image from the Docker Hub
🔅 Run the httpd container and expose it to the public
🔅 Copy the html code in /var/www/html directory and start the webserver.

Firstly you have to update our inventory. Inventory is a file in which we specify all the IP addresses of the nodes to be managed.

Let’s ping and check the connectivity.

Now, let’s start doing our task.

In First Step we will create a repo and configure yum. To download Docker we will specify the baseurl.

Code to do the same:

- name:
hosts: all
tasks:
- name: Docker Repo
yum_repository:
name: docker_repo
description: Repo for Docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: false

After configuring yum now we will download the Docker community in the managed node.

Code to download the Docker CE version.

- name: docker installation
package:
name: "docker-ce-18.09.1-3.el7.x86_64"
state: present

Here we have specified the version of docker to be downloaded.

In SECOND STEP we will start the docker services.

- name: Start docker
service:
name: "docker"
state: started
enabled: yes

To use ANSIBLE DOCKER MODULE we need docker sdk. To download the same

- name: docker sdk
command: pip3 install docker

We have installed and started our docker service in the managed node.

Now, In the next step, we will configure the webserver. To do this we first need to pull the httpd image from the docker hub. To do this we will use the below code

- name: pull image
docker_image:
name: httpd
source: pull

This block of code will pull the httpd image. Now, we will copy the website from the controller node to managed node.

- name: copy
copy:
dest: "/var/www/html/"
src: Nitya.html

Nitya.html is the basic Html File I have created in the same folder where this playbook resides.

Now, we will launch a Container and expose the same container to the public so that data inside that can be accessed.

To launch the container and for exposing it code is

- name: launch container
docker_container:
name: httpd_webserver
state: started
exposed_ports: "80"
image: httpd
ports: 8080:80
volumes: /var/www/html/:/usr/local/apache2/htdocs
command: httpd -D FOREGROUND

Full PlayBook Code:

Now we have to just run the above playbook.

Command to run the Playbook:

ansible-playbook <Playbook_Name.yml>

That’s all. Now, let’s check that if the playbook has successfully worked or not.

Playbook has been successfully executed.
Playbook has been successfully executed.
Yum has been configured and also docker is installed.
httpd image is pulled.
docker container is also created.
Html file is copied from the source to destination.

To see the final output we have to use the public IP of the system along with the port number and file name.

Like this:

http://<IP Address>:8080/page_name

Final Output.

Github Link:

Thanks for reading this article.

--

--

No responses yet