How to configure Docker using Ansible

Gaurav Tank
4 min readMar 12, 2021

In this blog, I am going to show you how to configure Docker container and deploy the web services. First of all, let me answer some questions like what is ansible and what is docker

What is Ansible?

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy — and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

What is docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Now, I am going to walk you through the whole process:

Step1: Update your inventory. In this case, I am running this playbook in the localhost.

- hosts: localhost

Step 2: Update your yum repo to download the docker-ce software and install all the dependencies like installing docker library using pip3 cmd.

- name: "Docker Repo"
yum_repository:
name: "Docker"
description: "Docker YUM REPO"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable"
gpgcheck: no
- name: "Docker Library"
pip:
name: "docker"
- name: "Checking for pre-installed docker-ce"
shell: "rpm -q docker-ce"
register: x
- name: "Docker Install"
shell: "yum install docker-ce --nobest -y"
when: x.rc != 0

Here, to provide the idempotency to the shell command I have first checked for the pre-installed docker-ce software If the software is not pre-installed it will install it.

Step 3: is that we have to start the docker services using the service module of ansible.

- name: "Docker services"
service:
name: "docker"
state: started

Step 4: Now we have to pull the docker image that we want to install. Here, I am going to pull the httpd image.

- name: "Pull Docker container"
docker_image:
name: "{{ image_name }}"
source: pull

To make this code more dynamic I have used the variables and mentioned it in the vars module.

Step5: Now we have to create a container of httpd image and with the additional -p or port option. This will do the PATing for us so that the DNATing is possible in the container.

- name: "Start Container- {{ container_name }}"
docker_container:
name: "{{ container_name}}"
state: present
image: "{{ image_name }}"
ports:
- "8080:80"
volumes:
- /var/www/html:/usr/local/apache2/htdocs/

Step 6: Finally we have to copy the web pages and start the httpd services.

- name: "Copying the web page......"
copy:
dest: "/var/www/html/index.html"
content: "It works all fine. Congrats!!!"
- name: "starting the httpd services"
shell: "/usr/sbin/httpd"

Congrats! Now you have launched a Docker container with the httpd services running using Ansible playbook. Now, I have attached some screenshots of the ansible-playbook for your reference.

Here, I have already installed docker-ce so it didn’t install it and skipped that part. And httpd image was not available in my OS so it pulled it.

Here, we can see that the httpd image has been pulled and the os named docker_os1 is launched.

It works fine. We configured the docker container using Ansible.

Hope you find this blog informative

Thanks for reading!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response