Using Ansible to configure Docker

Automating containers — Apache 2 httpd Web Server

Bhavesh Kakrotra
2 min readDec 18, 2020

In this world of automation we often require to automate a lot of things and one of them could be a container! So here we are — automating a docker container by creating an Ansible playbook!

Here we integrate Ansible and docker to create a webserver using httpd image:

First we create an Ansible playbook:

mkdir ~/playbooks
touch ~/playbooks/docker.yml

Now set your host group in inventory:

[docker]
192.168.X.Y ansible_ssh_user=**** ansible_user_pass=********

Now edit your playbook and set the tasks you want to do, and put them under in a list under tasks key. Since we are setting up a web server, our playbooks skeleton looks like:

- hosts: docker
tasks:
- name: Add Docker Repo
- name: Install Docker
- name: Enable Docker service
- name: Install Docker-py
- name: Pull httpd server image
- name: Create Volume Directory
- name: Create Link Directory
- name: Create a container
- name: Allow port on firewall
- name: Add html file

Now write down each task… until the final playbook looks like:

Now its time to execute the playbook.

On the target note we can check if the port 8080 is on.

Now put some files in the linked directory… eg:

echo "hellooooo" > /var/www/html/index.html

Now check if the webserver is running on target node:

Feel free to contact on my linkedin.

--

--