Are you a developer who uses containers a lot and want to know if there is any way for configuring the containers without using Dockerfile? You’re reading the right blog. In this article, I will show you how to configure the containers using one of the great configuration management tools Ansible.
- Ansible should be installed on your host machine.
- Docker should be installed.
Step 1: Create a docker image with ssh enabled
I’m will be using Docker as a container engine for performing this experiment/demo. As I will use Ansible for configuring the containers, Ansible uses SSH for connecting securely to the managed nodes(in our case containers). So, we required the docker image that has SSH service enabled.
By default, containers don’t have a password enabled for login. As we are using Ansible, ansible always required a username and password or a private key. Here, we will set the password for the root user in the docker image.
I will use the following Dockerfile for creating the docker image with SSH service enabled and password set for the root user.
Build the docker image using the above Dockerfile.
$ docker build . -t cankush625/centos-ssh:v1
You can find this image on the dockerhub here.
Step 2: Write a Playbook for configuring the containers
Ansible uses Ansible playbooks for configuring the managed nodes.
This playbook is consists of two plays.
The first play is used to launch the docker container and configure the Ansible inventory with the IP of the container. I had used the docker image that I had created just before.
For configuring the Ansible inventory, I had created one template using the Jinja formatting. The template file will look like the following.
This template file will be updated with the IP of the container on the fly. We will place these IPs under the host group named docker. In the next play, we will use this host group to configure the container.
The second play will configure the httpd web server on the docker container. As we already had updated the Ansible inventory file with the container IP under the host group named docker, we will use that hostname to configure the container.
Finally, copy the website page to the httpd document root that is /var/www/html.
Step 3: Run the Ansible Playbook
Finally, run the ansible-playbook.
$ ansible-playbook configure_containers.yml
The image above shows that we had successfully configured everything.
Step 4: Check if container is configured successfully
As the playbook executed successfully, this means everything was configured successfully. We can check the same by accessing the webserver we had confirmed. As we had exposed the container on port 80, we can access the website at port 80 on the container IP.
Let’s go to the browser and see the page at URL http://CONTAINER_IP:80
We can access the webpage on the container IP on port 80.
We had configured the container successfully using Ansible.
You can use this approach to configure any of the services on the containers. Also, you can use any of the container engines of your choice for performing this experiment.