How to launch and manage Docker through Ansible playbook

Rishabh Arya
4 min readAug 3, 2023

Installing, configuring, launching, and starting docker services are the topics which are gonna covered in this article.

Lets quickly have a look on our agenda:

🔰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

All these steps will be performed on redhat linux.

Lets get started:

Hoping that you have already installed ansible on the host node.

Step 1: First create a inventory file inside which we will write the IP(s) of the target nodes i.e. IP(s) of the systems on which are gonna install docker through playbook.

_IP_ ansible_ssh_user=<user name> ansible_ssh_pass=<password>

Just create a simple yml file and inside it write the required information in a simple format as stated above.

And after adding the IP(s) in the inventory file try to ping, so that we can check that the connection between the host and the target nodes are proper or not.

As you can see everything is good.

The left one is the host while the right one is the target node.

Step 2: In this step we are gonna focusing on writing the playbook.

Now directly jumping inside the yml code of playbook. There are several blocks of code which needs to be added and will explain there roll here.

- hosts: all
tasks:
- name: "repository"
yum_repository:
name: "Docker"
description: "Docker Repo"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable"
gpgcheck: "no"

This block will configure the repository needed to install docker and the repository will get configured on all the target nodes as stated in the first line.

- name: "installing docker package"
command: "yum install docker-ce --nobest -y"

In this above block the command for installing docker is gonna run on all the target nodes.

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

Now, this block is gonna start the docker service.

- command: "pip3 install docker"

The above command is important to get run on the nodes so as to install some important libraries required for ansible to perform some further tasks.

- name: "Creating folder and copying html file"
file:
path: "/myfile"
state: directory
- name: "Copying local files"
copy:
src: "index.html"
dest: "/myfile/"

This block will copy our webserver code from the host’s directory to the location specified on the target nodes so that later on our website should get displayed.

- name: Creating container using HTTPD Image
docker_container:
name: "MyWebServer3"
image: "httpd"
state: started
exposed_ports:
- "80"
ports:
- "8888:80"
volumes:
- /myfile:/usr/local/apache2/htdocs/

And in this last block of code, docker container is gonna create with httpd image and the port number 80 will be exposed so that we can access our website from anywhere and the directory in which our webserver code was copied earlier has been mounted to the default directory of webserver.

Step 3: The playbook is ready and we need to run the playbook that’s it.

Before running the playbook you can see that the docker is not installed on the target node.

To run the playbook enter the command:

ansible-playbook _.yml

And you can see that our playbook has done everything smoothly without any error and the docker is now installed, so if now you try to access the website with the container IP followed by the port you exposed which is port number 80 in this case, out website is working fine.

And that’s it, this was a simple integration of docker with ansible, hope it was useful.

Thanks for reading…..

--

--

Rishabh Arya

I am an active learner who likes to challenge every problem with a can-do mindset in order to make any idea a reality.