Configure Docker using Ansible Playbook

Inshiya Nalawala
2 min readMar 31, 2021

This article will walk you through all the steps that you must do to configure docker on Ansible managed nodes. Further, it talks about how you can pull the httpd image from the public registry to configure a web server on a container and expose it to the public.

Start by setting up the Ansible Controller node — edit the hosts’ file to create appropriate host groups to include the managed nodes that are to be a part of this Configuration.

I have a [docker] host group as follows:

Now, we can begin writing the playbook.

Step 1: Installing Docker and Enable the services

To install Docker, we first configure the yum repository (Yum is a package manager for RedHat/Fedora/Centos distributions). After configuring the Docker Repo, one can use the yum command to install the community version of docker.

After docker is installed we enable its services.

Step 2:

Next, to configure and do something with containers with ansible, we require the following on the host that executes the docker modules:

1. Python >=2.6

2. docker python package (to be installed using pip)

Therefore, in this step, we will install the same on the host (here, node1) that will execute the docker_container module in later steps.

Step 3: Now, we will pull the image from the docker hub. This is a rather simple step.

Step 4: Now, we will create a directory that will serve as the mount point for the document root of the server launched in the container. We will additionally put some basic html files to verify, everything’s working as expected.

Step 5: The last step is to create a container, run it, expose it to a host port. Also, we will mount the volume we created in step 3 too.

Finally, connect to node1 on the port exposed to view the files we added step 4.

192.168.0.108 is the IP of node1 and 8080 is the host port on which the service running on port 80 inside the container is exposed.

--

--