Automating Docker Container Deployment with Ansible Playbook

Naveen paluri
3 min readAug 1, 2023

--

In this blog post, we will explore how to automate the deployment of a Docker container running an HTTP server using Ansible Playbook. Ansible is a powerful configuration management tool that enables us to easily configure and manage multiple remote machines. We will go through each step of the Ansible Playbook to achieve our goal of deploying an HTTP server in a Docker container and making it publicly accessible.

Step 1: Install Ansible on the Control Node We need to install Ansible on the control node, which will be the machine from where we will execute the Ansible Playbook. Ensure Python and pip are already installed, then run the following command to install Ansible:

pip install ansible

Step 2: Set Up the Inventory File Create an inventory file (e.g., inventory.ini) to define the managed nodes where we want to deploy the Docker container. For this example, we will assume there is a single managed node with the IP address 192.168.1.100. The inventory file should look like this:

[web_servers]
192.168.1.100

Step 3: Create the Ansible Playbook Now, let’s create the Ansible Playbook (e.g., docker_deploy.yml) to automate the deployment steps.

yamlCopy code
---
- name: Configure and Deploy Docker Container
hosts: web_servers
become: true
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
tags: [docker]
- name: Start and Enable Docker service
service:
name: docker
state: started
enabled: yes
tags: [docker] - name: Pull httpd server image from Docker Hub
docker_image:
name: httpd
state: present
tags: [docker] - name: Run the Docker container and expose it
docker_container:
name: my_httpd
image: httpd
ports:
- "80:80"
state: started
tags: [docker] - name: Copy HTML code to /var/www/html directory
copy:
src: /path/to/html_code/index.html
dest: /var/www/html/
tags: [docker] - name: Start the web server
service:
name: httpd
state: started
tags: [docker]

Step 4: Prepare HTML Code Create an HTML file (e.g., index.html) with your desired content. This file will be copied to the web server inside the Docker container. For example:

htmlCopy code
<!DOCTYPE html>
<html>
<head>
<title>Welcome to My Website</title>
</head>
<body>
<h1>Hello, Docker and Ansible!</h1>
<p>This is a sample web page deployed in a Docker container using Ansible.</p>
</body>
</html>

Step 5: Run the Ansible Playbook Now, it’s time to run the Ansible Playbook and deploy the Docker container on the managed node.

bashCopy code
ansible-playbook -i inventory.ini doker_deploy.yml

Conclusion: In this blog post, we learned how to automate the deployment of an HTTP server in a Docker container using Ansible Playbook. This allows us to easily configure and manage Docker containers on multiple remote machines.

Ansible’s declarative syntax makes it simple and efficient to automate such tasks. By following the steps mentioned above, you can quickly deploy your web server using Docker and Ansible.

--

--